2010-07-30 44 views
0

我有一個帶有2個子按鈕的窗口,當我將鼠標懸停在外面時我最初試圖使它們的文本顏色改變,但是當我把WM_MOUSEMOVE消息中的MessageBox()我發現當光標位於任一按鈕上時,我停止收到消息框。 MSDN說,WM_MOUSEMOVE被髮送到包含光標的窗口,所以..我一定是做錯了什麼。主窗口的Win32 API 2子按鈕沒有得到WM_MOUSEMOVE消息

HWND hparent; 
HWND hplaybtt; 
HWND hexitbtt; 
HINSTANCE hinstance; 

LRESULT CALLBACK MainProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 

void MakeWindow() { 
WNDCLASSEX wc; 
wc.cbSize  = sizeof (WNDCLASSEX); 
wc.style   = CS_HREDRAW | CS_VREDRAW; 
wc.lpfnWndProc = MainProc; 
wc.cbClsExtra = 0; 
wc.cbWndExtra = 0; 
wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); 
wc.lpszMenuName = NULL; 
wc.lpszClassName = L"Window"; 
wc.hInstance  = hinstance; 
wc.hIcon   = NULL; 
wc.hIconSm  = NULL; 
RegisterClassEx (&wc); 

hparent = CreateWindowEx (0, L"Window", L"Slot Machine", WS_OVERLAPPEDWINDOW, 0, 0, 300, 300, 
    NULL, NULL, hinstance, NULL); 
hplaybtt = CreateWindowEx (0, L"Button", L"Play", WS_VISIBLE | WS_CHILD | BS_OWNERDRAW, 110, 125, 80, 50, 
    hparent, (HMENU) 101, hinstance, NULL); 
hexitbtt = CreateWindowEx (0, L"Button", L"Exit", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 110, 175, 80, 50, 
    hparent, (HMENU) 102, hinstance, NULL); 

ShowWindow (hparent, SW_SHOW); 
} 

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 
hinstance = hInstance; 
MSG Msg; 

MakeWindow(); 

while (GetMessage (&Msg, NULL, 0, 0)) { 
    TranslateMessage(&Msg); 
     DispatchMessage(&Msg); 
}  

return Msg.wParam; 
} 

LRESULT CALLBACK MainProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { 
PAINTSTRUCT ps; 
COLORREF cr = RGB (255, 0, 0); 
COLORREF white = RGB (255, 255, 255); 
HDC hdc; 
LPDRAWITEMSTRUCT dis; 

    switch(Msg) { 
    case WM_DRAWITEM: 
    dis = (LPDRAWITEMSTRUCT) lParam; 
    FillRect (dis->hDC, &dis->rcItem, (HBRUSH) GetStockObject (BLACK_BRUSH)); 
    SetBkMode (dis->hDC, TRANSPARENT); 
    SetTextColor (dis->hDC, white); 
    TextOut (dis->hDC, 25, 15, L"Play", 4); 
    MessageBox (hWnd, L"hi", NULL, MB_OK); 
    break; 
    case WM_PAINT: 
    hdc = BeginPaint (hparent, &ps); 
    SetTextColor (hdc, cr); 
    TextOut (hdc, 105, 50, L"Slot Machine", 12); 
    EndPaint (hparent, &ps); 
    break; 
    case WM_MOUSEMOVE: 
    POINT p; 
    p.x = LOWORD (lParam); 
    p.y = HIWORD (lParam); 

    MessageBox (hWnd, L"This is the slot machine game.", L"About", MB_OK); 

    break; 
    case WM_DESTROY: 
    PostQuitMessage(WM_QUIT); 
    break; 
    default: 
    return DefWindowProc(hWnd, Msg, wParam, lParam); 
} 
return 0; 
} 
+1

WM_MOUSEMOVE被張貼到按鈕窗口,而不是你的主窗口;如果要截取此消息,您可以繼承按鈕窗口的子類。 – Luke 2010-07-30 01:12:41

回答

0

只要你有一個窗口上的控件 - 比如按鈕 - 如果你要正確地支持鍵盤焦點,您需要調整您的消息循環,以更多的東西是這樣的:

while (GetMessage (&Msg, NULL, 0, 0) >0) 
{ 
    if(!IsDialogMessage(hwnd,&msg)) 
    { 
    TranslateMessage(&Msg); 
    DispatchMessage(&Msg); 
    } 
} 

這提供瞭如何處理鍵盤(和鼠標)消息的例子,這些消息對主窗口有意義,而不是通常分派給它們的控件。 在消息循環中處理它們。在這種情況下,將IsDialogMessage()替換爲將處理鼠標懸停效果的函數(並調用IsDialogMessage()以確保TAB和ENTER將在控件之間選中並激活默認按鈕)來替換該對話框。