2010-07-30 4 views
0

두 개의 자식 단추가있는 창이 있으며 원래 마우스를 가져갈 때 텍스트가 변하도록 만들려고했으나 WM_MOUSEMOVE 메시지의 MessageBox() 내 커서가 버튼 중 하나에있을 때 메시지 상자가 표시되지 않는다는 것을 알았습니다. MSDN은 WM_MOUSEMOVE가 커서가 포함 된 창으로 전송된다고 말합니다. 그래서 나는 틀린 일을해야합니다.Win32 API 주 창의 하위 단추에 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

답변

0

은 때마다 당신은 창에 컨트롤이 - 버튼처럼 - 제대로 키보드 포커스를 지원하려는 경우, 당신은 더 이런 식으로, 당신의 메시지 루프를 조정해야합니다

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

이것은 일반적으로 발송되는 컨트롤보다는 주 윈도우에 의미가있는 키보드 (및 마우스) 메시지를 처리하는 방법의 예를 제공합니다. 메시지 루프에서이를 처리하십시오.

이 경우 IsDialogMessage()를 마우스 이동 효과를 처리 할 함수로 바꾸고 IsDialogMessage()를 호출하여 Tab 키와 Enter 키가 컨트롤 사이를 이동하고 기본 누름 단추를 활성화하는지 확인하십시오.

관련 문제