WinApi Problem z MessageBox

0

Witam mam pewien problem, mianowicie:

Gdy całego case WM_PAINT dam w komentarz to przy próbie zamknięcia programu ładnie wyskakuje MsgBox z odpowiednim komunikatem zaś gdy WM_PAINT jest włączony w program to tak jakby nie pokazuje się MsgBox a w okienku nic niemożna zrobić bo uruchomiliśmy MsgBox.

Oto kod:

#include <windows.h>

static TCHAR lpszAppName[] = TEXT( "API Windows v 1.0" );
static int windowWidth = 640;
static int windowHeight = 480;
HINSTANCE hInst;


LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	HDC hDC;
	HDC hDCII;
	HBITMAP hBitmap;
	switch (uMsg)
		{

		case WM_CLOSE:
			if(MessageBox(hWnd,L"Czy chcesz zamknąć aplikacje ?", lpszAppName, MB_YESNO) == IDYES)
				PostQuitMessage(0);
			break;
		case WM_PAINT:
			hBitmap = (HBITMAP) LoadImage(hInst, L"c:/images/test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
			hDC = GetDC(hWnd);
			hDCII = CreateCompatibleDC(hDC);
			SelectObject(hDCII,hBitmap);
			BitBlt(hDC, LOWORD(lParam), HIWORD(lParam), 192, 224, hDCII, 0, 0, SRCCOPY);
			ReleaseDC(hWnd, hDC);
			DeleteDC(hDCII);
			break;
		default:
			return (DefWindowProc(hWnd, uMsg, wParam, lParam));
	}
    return(0L);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG   msg;
    WNDCLASS wndclass;
    HWND   hWnd;

    wndclass.style  = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = MainWndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance  = hInstance;
    wndclass.hIcon  = NULL;
    wndclass.hCursor  = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = lpszAppName;
	hInst = hInstance;

    if(RegisterClass(&wndclass) == 0)
        return FALSE;

    hWnd = CreateWindow(
        lpszAppName, lpszAppName,
        WS_OVERLAPPEDWINDOW| WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
        100, 100, windowWidth, windowHeight,
        NULL, NULL, hInstance, NULL);

    if(hWnd == NULL)
        return FALSE;

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

Proszę o pomoc.

1

W obsłudze WM_PAINT powinno się używać BeginPaint i EndPaint. Może dlatego masz takie dziwne zachowanie.

0

A jak to mniej więcej zastosować ?

0
HBITMAP hBitmap;
 
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    HDC hDC;
    HDC hDCII;
    PAINTSTRUCT ps;
    switch (uMsg)
        {
 
        case WM_CLOSE:
            if(MessageBox(hWnd,L"Czy chcesz zamknąć aplikację ?", lpszAppName, MB_YESNO) == IDYES)
                PostQuitMessage(0);
            break;
        case WM_CREATE:
            hBitmap = (HBITMAP) LoadImage(hInst, L"c:/images/test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            break;
        case WM_PAINT:
            {
            hDC = BeginPaint(hWnd, &ps); 
            hDCII = CreateCompatibleDC(hDC);
            HBITMAP hBmpOld = (HBITMAP)SelectObject(hDCII,hBitmap);
            BITMAP bmp;
            GetObject(hBitmap, sizeof(bmp), &bmp);
            BitBlt(hDC, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCII, 0, 0, SRCCOPY);
            SelectObject(hDCII, hBmpOld);
            DeleteDC(hDCII);
            EndPaint(hWnd, &ps); 
            break;
            }
        default:
            return (DefWindowProc(hWnd, uMsg, wParam, lParam));
    }
    return(0L);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1