2010-11-29 48 views
5

我被困在一個奇怪的問題。我在VC++ 2008中創建了一個Win32應用程序,當調用MessageBox時,使得一個類可以封裝大部分工作,以便於重複。消息框`創建(我認爲),但不顯示,除非我按Alt鍵!Win32 MessageBox沒有出現

發生什麼事究竟是:

  1. 我運行程序

  2. 按Enter鍵

  3. 主窗口失去焦點

  4. 給蜂鳴聲時,我就點擊主窗口就好像模態消息框一樣

  5. 或者按下Escape ...獲得焦點或者按Alt,然後按Alt鍵出現MessageBox(即按住Alt鍵)。菜單將下降)!!!!!!

P.S.它工作正常,但突然發生了。我沒有發現任何區別 - 我甚至做了一個新項目!

這應該主程序:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int  nCmdShow) 
{ 
    MSG msg; 
    CWnd cMainWindow(TEXT("DentoMan"), TEXT("Bejkoman")); // pass The class name and window name to the constructor 

    cMainWindow.CreateDef(); //Create the Window 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return (int)msg.wParam; 
} 

雖然這是類文件

CWnd::CWnd() { 
}; 

CWnd::CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName) { 
    CWnd::lpszClassName  = lpszClassName; 
    CWnd::lpszWindowName = lpszWindowName; 
}; 

CWnd::~CWnd() { 
}; 

// Create the window with default parameters 
HWND CWnd::CreateDef(void) { 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = StaticWndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = (HINSTANCE)GetModuleHandle(NULL); 
    wcex.hIcon   = 0; 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 4); 
    wcex.lpszMenuName = 0; 
    wcex.lpszClassName = lpszClassName; 
    wcex.hIconSm  = 0; 

    RegisterClassEx(&wcex); 
    g_hWnd = CreateWindowEx(0,lpszClassName, lpszWindowName, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, this); 
    hInst = wcex.hInstance; //Store hInstance in the class hInst variable 

    if (!g_hWnd) return false; 
    ShowWindow(g_hWnd, SW_SHOW); 
    UpdateWindow(g_hWnd); 

    return g_hWnd; 
} 

LRESULT CALLBACK CWnd::StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { 
    /* The Only Message we take here so we store the 'this' pointer within the window to identify messages 
    comming from it by the 'this' pointer*/ 
    if (Message == WM_CREATE) { 
     SetWindowLong(hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams); 
    } 

    /* Store the window pointer in the class pointer we just created in order to run the right public WndPRoc */ 
    CWnd *Destination = (CWnd*)GetWindowLong(hWnd, GWL_USERDATA); 

    // If the hWnd has a related class, pass it through 
    if (Destination) { 
     return Destination->WndProc(hWnd, Message, wParam, lParam); 
    } 

    // No destination found, defer to system... 
    return DefWindowProc(hWnd, Message, wParam, lParam); 
}; 

LRESULT CWnd::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { 
    // Determine message type 
    switch (Message) { 
     case WM_LBUTTONDOWN: 
      { 
       /* this is a common trick for easy dragging of the window.this message fools windows telling that the user is 
       actually dragging the application caption bar.*/ 
       SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION,NULL); 
       break; 
      } 

     /*case WM_CREATE: 
      break; 
    */ 

     case WM_CLOSE: 
      PostQuitMessage(0); 
      break; 

     case WM_DESTROY: 
      UnregisterClass(lpszClassName, hInst); 
      PostQuitMessage(0); 
      break; 

     case WM_KEYDOWN: //KeyBoard keys 
      // Which key was pressed? 
      switch (wParam) { 
       case VK_ESCAPE: //close through escape key 
        PostQuitMessage(0); 
        return 0; 
       case VK_RETURN: 
        MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), NULL); 
        return 0; 
      } // End Switch 

      break; 

     case WM_COMMAND: 
      /*switch(LOWORD(wParam)) 
     { 
     }*/ 
     break; 

     case WM_PAINT: 
      break; 

     default: 
      return DefWindowProc(hWnd, Message, wParam, lParam); 

    } // End Message Switch 

return 0; 
}; 

類的頭:

class CWnd { 
    public: 
     CWnd(); 
     CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName); 
     virtual ~CWnd(); 
     virtual HWND CreateDef(void);   // Create the window with default parameters 
     virtual LRESULT  WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam); 

    private: 
     static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam); 
     HWND  g_hWnd;  //Global window handle for this window 
     HINSTANCE hInst;  //Global instance for this window 

     LPTSTR   lpszClassName; 
     LPTSTR   lpszWindowName; 
}; 

P.S.我包括所有所需的頭文件,一切都只是MessageBox的

罰款,這也是對代碼的鏈接here

+2

在此處張貼相關代碼。另外,在黑暗中拍攝:在顯示消息框時指定一個父窗口。 – 2010-11-29 00:12:05

+4

已編輯,鏈接已刪除。不要發佈不安全的.exe文件..只需粘貼相關的代碼。 – Ruel 2010-11-29 00:13:46

回答

8

Ohhhhhhh終於讓我找到這個問題的解決方案......併爲大家造福問題在WM_PAINT消息是的WndProc(......)我在裏面寫了一些代碼,並刪除所有的代碼以及BeginPaint和EndPaint函數,所以當程序包括那個MessageBox時,程序進入一個凍結期,但它只顯示當我按下Alt時,我認爲控制權轉移到系統中,顯示系統菜單(我認爲)

該解決方案要麼刪除WM_PAINT消息處理程序或添加正常的BeginPaint和EndPaint函數

感謝大家誰對我的問題通過

2

當您創建的MessageBox,你應該在CreateWindowEx通過WS_CHILD
編輯2:
好吧試試這個。

MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), MB_OK); 
3

如果有人仍感興趣,這種方法可行:

MessageBox(NULL,L"error",L"Error",MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY); 
2

我有一個類似的問題,通過WM_PAINT是原因如上文提到的人。在那裏加入return DefWindowProc(hWnd, Message, wParam, lParam);解決了它。