繁体   English   中英

使用WinAPI无法显示窗口

[英]Can't display window with WinAPI

我知道这是一个无聊的问题,而且我已经做过上百万次,但是无法在我编写的WinAPI程序中显示简单的窗口。 后来它使用DirectX在窗口上绘制,但是我已经调试了程序,即使在所有DirectX东西开始执行之前,它也无法显示窗口。 我只在任务栏上看到程序图标,但是没有窗口。

这是处理窗口创建的代码。

WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(wc));
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = title.c_str();
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = 0;
    wc.hIconSm = 0;
    wc.hbrBackground = 0;
    wc.lpszMenuName = 0;

    if(!RegisterClassEx(&wc))
    {
        debug << "Failed to register window class." << std::endl;
        return 0;
    }

    DWORD dwStyle, dwExStyle;
    dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

    AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

    int wwidth = windowRect.right - windowRect.left;
    int wheight = windowRect.bottom - windowRect.top;

    debug << "Screensize: " << wwidth << "x" << wheight << std::endl;
    debug << "Creating program window..." << std::endl;

    HWND hWnd = CreateWindowExW(dwExStyle, wc.lpszClassName, title.c_str(), dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, wwidth, wheight, NULL, NULL, hInstance, 0); 

    if(!hWnd)
    {
        debug << "Error occured while trying to display the window.\n";
        return false;
    }

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

我很绝望。 我开始发现我的编程环境,库等存在问题。我使用的是VS2012 / Win7,以及VS随附的标准c ++编译器。

编辑 WindowRect在这里

RECT windowRect;
    windowRect.left = 0;
    windowRect.right = g_engine->getScreenWidth(); //800
    windowRect.top = 0;
    windowRect.bottom = g_engine->getScreenHeight(); //600

这是DefWindowProc

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg == WM_DESTROY)
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

我遇到这个问题的大多数时候是WinProc返回值错误。

您的WinProc可能会调用DefWndProc而不返回其返回值。 这使得WM_NCCREATE被视为返回false(而不是应为true),从而导致创建过程停止。

正确的windowproc应该是

LRESULT CALLBACK WndProc (HWND h, UINT u, WPARAM w, LPARAM l)
{
  if(...) {}
  else if(...) {}
  else if(...) {} //prematurely return if you don't want the default behavior

  return DefWndProc(h,u,w,l);
}

您的wndProc看起来不对:

if(msg == WM_DESTROY) // <-- you should remove it and let system handle default messages 
    return DefWindowProc(hWnd, msg, wParam, lParam);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM