开发者

Win32 API 2 child buttons of the main window are not getting the WM_MOUSEMOVE message

开发者 https://www.devze.com 2023-01-09 06:20 出处:网络
I have a window with 2 child buttons on it and I was originally trying to make their text change color when I hover over and out but when I put the MessageBox () in the WM_MOUSEMOVE message I found ou

I have a window with 2 child buttons on it and I was originally trying to make their text change color when I hover over and out but when I put the MessageBox () in the WM_MOUSEMOVE message I found out that I stop getting message boxes when my cursor is on either of the buttons. MSDN says that WM_MOUSEMOVE is sent to the window that contains the cursor, so.. I must be doing something wrong.

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;
 LPDRAWITE开发者_开发知识库MSTRUCT 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;
}


As Luke said, you need to "subclass" the wndproc of the buttons.

http://msdn.microsoft.com/en-us/library/ms633570%28VS.85%29.aspx#subclassing_window


Whenever you have controls on a window - like buttons - if you want to support keyboard focus properly, you need to adjust your message loop, to something more like this:

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

This provides the example of how to deal with keyboard (and mouse) messages that have meaning to the main window rather than the controls they are usually dispatched to. Handle them in the message loop.

In this case, replace IsDialogMessage() with a call to a function that will handle mouse hover effects (and call IsDialogMessage() to ensure that TAB and ENTER will tab between controls and activate the default push button).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号