Win32 Why does MessageBox block the entire message

Viewed 34

I don't get why this program works when I use GetDC but not with BeginPaint. It's even more confusing that uncommenting the MessageBoxA will make the program freeze every time a line gets drawn. Can someone explain why?

case WM_KEYDOWN:
        {
            PAINTSTRUCT ps;
            HDC hdc; 
            hdc = GetDC(hWnd);
            //hdc = BeginPaint(hWnd, &ps);
            RECT rect;
            POINT p;

            if (!MoveToEx(hdc, x, 0, &p))
            {
                MessageBox(hWnd, L"MoveToEx error", L"MoveToEx error", MB_OK);
            }
            GetClientRect(hWnd, &rect);
            
            if (!LineTo(hdc, x, rect.bottom))
            {
                DWORD dw = GetLastError();
                LPSTR msgBuffer = nullptr;
                size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msgBuffer, 0, NULL);
                std::string poruka(msgBuffer, size);
                MessageBoxA(hWnd, poruka.c_str(), "LineTo error", MB_OK);
            }
            x += 5;
            //MessageBoxA(hWnd, std::to_string(p.x).c_str(), std::to_string(x).c_str(), MB_OK);

            //EndPaint(hWnd, &ps);
            ReleaseDC(hWnd, hdc);
        }
0 Answers
Related