簡體   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