无法调整win32窗口的大小



我想学习如何在win32中创建一个窗口。我就知道这么多了。我面临的问题是我无法创建一个可以由用户调整大小的窗口。我希望有人能帮我解决这个新手的问题。

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
std::string msg = "";
UINT width = 0;
UINT height = 0;
switch(uMsg)
{
case WM_SIZE:
width = LOWORD(lParam);
height = HIWORD(lParam);
if(width<(height*600)/800) SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE|SWPNOZORDER);
return true;
case WM_SIZING:
width = LOWORD(lParam);
height = HIWORD(lParam);
if(width<(height*600)/800) SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE|SWPNOZORDER);
return true;
case WM_DESTROY:
PostQuitMessage(0);
return true;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, INT nCmdShow)
{
WNDCLASSEX wnd = {0};
wnd.lpszClassName = "WindowLearn";
wnd.hInstance = hInstance;
wnd.lpfnWndProc = windowProc;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wnd);
HWND hWnd = CreateWindowEx(NULL, "WindowLearn", "WindowLearnChild", WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg = {0};
float pTime = 0.0f;
BOOL result;
while(msg.message != WM_QUIT)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

窗口可以创建,但是当我尝试调整窗口的大小时,窗口会卡在鼠标上。

看来您想要空闲处理,这意味着在事件循环中没有事件时为您的directX应用程序完成一些任务。

有两种不同的方法:

  • 指定一个单独的线程用于后台处理。它增加了多处理的复杂性,但允许将所有处理作为单个代码进行,并让系统影响事件循环和后台处理的时间片。

  • 使用修改后的事件循环,当没有事件存在时,执行的后台处理。PeekMessage是这里的关键:

    ...
    MSG msg = {0};
    for (;;)
    {
    if (PeekMessage(&msg, NULL, 0, 0, 0, 0) {
    if (! GetMessage(&msg, NULL, 0, 0, 0)) break; // msg is WM_QUIT
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    else {
    // do some idle processing for a short time
    // no event will be processing during that
    ...
    }
    }
    

    好的一点是,你不需要任何多线程,但你必须显式地将后台处理分成短时间片。

最新更新