Bitmap anzeigen, will net :(
-
Hi,
ich verzweifle langsam... wieso wird kein bild angezeigt?
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HBITMAP bmp_Background = NULL; switch (message) { case WM_CREATE: { bmp_Background = LoadBitmap(GetModuleHandle(NULL), "test.bmp"); RECT rc = {0, 0, 640, 480}; ::AdjustWindowRectEx (&rc, window_style, false, 0); int centerX = (::GetSystemMetrics(SM_CXFULLSCREEN)/2) - (abs(rc.left)+rc.right )/2; int centerY = (::GetSystemMetrics(SM_CYFULLSCREEN)/2) - (abs(rc.top )+rc.bottom)/2; ::MoveWindow (hWnd, centerX, centerY, (rc.right - rc.left), (rc.bottom - rc.top), true); } break; case WM_PAINT: { BITMAP bm; PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); HDC hdcMem = CreateCompatibleDC(hdc); HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall); GetObject(g_hbmBall, sizeof(bm), &bm); BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); SelectObject(hdcMem, hbmOld); DeleteDC(hdcMem); EndPaint(hWnd, &ps); } break; case WM_DESTROY: { if (bmp_Background != NULL) { ::DeleteObject (bmp_Background); bmp_Background = NULL; } ::PostQuitMessage(0); return 0; } } return ::DefWindowProc(hWnd, message, wParam, lParam); }
-
HBITMAP bmp_Background = NULL;Schreib mal ein static vor das HBITMAP. Ansonsten erzeugst du eine normale lokale Variable auf dem Stack, die bei jedem Aufruf wieder 0 ist.
-
hier ein Beispielcode:
HANDLE hBitmap
hBitmap = (HBITMAP)LoadImage(NULL,"test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
int width = 640;
int height = 480;switch(message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC;hDC = BeginPaint(hWnd, &ps);
{HDC BhDC = CreateCompatibleDC(hDC);
SelectObject(BhDC,hBitmap);
BitBlt(hDC,0,0,width,height,BhDC,0,0,SRCCOPY);
}
EndPaint(hWnd, &ps);
return 0;
}das muesste eigentlich einwandfrei funktionieren!!
nicht vergessen: den reservierten speicherplatz von hBitmap wieder frei zu geben