2010-08-20 71 views
2

我有一個控制檯應用程序,我從中創建一個窗口。C++ Win32窗口無響應

我可以在窗口中渲染東西就好了。但該窗口無響應/用戶無法控制。

只要將鼠標移到窗口上,就會看到沙漏光標,並且無法移動窗口。

什麼可能導致這種情況?

編輯:

WNDCLASSEX wndClass;   // Window class 
    ZeroMemory(&wndClass, sizeof(wndClass)); // Clear the window class structure 
    wndClass.cbSize = sizeof(WNDCLASSEX); 
    wndClass.style   = CS_HREDRAW | CS_VREDRAW | CS_CLASSDC; 
    wndClass.lpfnWndProc = DefWindowProc; 
    wndClass.cbClsExtra  = 0; 
    wndClass.cbWndExtra  = 0; 
    wndClass.hInstance  = nullptr; 
    wndClass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wndClass.lpszMenuName = NULL;//MAKEINTRESOURCE(IDR_MAINMENU); 
    wndClass.lpszClassName = _classname.c_str(); 
    wndClass.hIconSm  = 0; 

    if (RegisterClassEx(&wndClass) == 0)// Attemp to register the window class 
     throw std::exception("WINDOW ERROR: Failed to register the window class!"); 

    DWORD dwStyle = 0;    // Window styles 
    DWORD dwExStyle = 0;   // Extended window styles 

    dwStyle = WS_OVERLAPPEDWINDOW |  // Creates an overlapping window 
       WS_CLIPCHILDREN |   // Doesn"t draw within child windows 
       WS_CLIPSIBLINGS;    // Doesn"t draw within sibling windows 

    //adjust window size 
    RECT rMain; 
    rMain.left = 0; 
    rMain.right = width; 
    rMain.top = 0; 
    rMain.bottom = height; 

    AdjustWindowRect(&rMain, dwStyle, 0); 

    // Attempt to create the actual window 
    _hwnd = CreateWindowEx(dwExStyle,  
          className, 
          windowTitle,  
          dwStyle,   
          0, 0,   
          rMain.right - rMain.left, 
          rMain.bottom - rMain.top, 
          nullptr,    
          0, 
          nullptr, 
          0); 


    ShowWindow(_hwnd, SW_SHOW); 
    SetForegroundWindow(_hwnd); 
    SetFocus(_hwnd); 
+0

如果你有一個(最小的)代碼片段來演示這個問題,那將是非常有用的。 – 2010-08-20 09:46:46

+0

你在你的WindowProc中處理WM_MOUSE *消息嗎? – munissor 2010-08-20 09:49:12

+0

我不處理任何我只使用DefWindowProc。 – ronag 2010-08-20 09:58:53

回答

8

,因爲它是在註釋中已經提到的,我會讓這個社會的維基

您需要獲得該窗口的消息,並相應地調度它們。

/* 
* HWND hWnd: this is the handle to your window (that is returned from CreateWindow[Ex] 
*/ 
MSG msg; 
while (GetMessage(&msg, hWnd, NULL, NULL) > 0){ 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
} 

如果你不這樣做,你的wndProc功能將永遠不會得到任何消息,和Windows發現它反應遲鈍(因此沙漏)。

0

消息循環的一部分已經「暫時」離開評論,我錯過了這一點。