How can I capture the WM_MOUSEMOVE message for a button?

This is a discussion on How can I capture the WM_MOUSEMOVE message for a button? within the Windows Programming forums, part of the Platform Specific Boards category; I want to capture the WM_MOUSEMOVE message for a button so I can change the cursor when the mouse is ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    112

    How can I capture the WM_MOUSEMOVE message for a button?

    I want to capture the WM_MOUSEMOVE message for a button so I can change the cursor when the mouse is over the button. Anyone know how I could do this?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You can have some simple collision detection to check for this. Make a bounding box around your cursor and a box around the button. Then use the API IntersectRect() to determine if they collide with one another. It will return true if a collision took place. Alternatively you can just check whether the coordinates given from WM_MOUSEMOVE are inside the rect that forms your button. When your application receives a WM_MOUSEMOVE message you can obtain the cursor's position by checking LOWORD and HIWORD of lParam.

    Code:
    COORD Pos;
    Pos.x = LOWORD(lParam);
    Pos.y = HIWORD(lParam);
    Something like that. Hope this helped some.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    Yah, but the dialog won't send the WM_MOUSEMOVE message when it is over the button so I can't check if it is over the button or not. I don't know if I am missing something here cause this is what everyone keeps telling me to do and its not working. My code wich is posted below is giving me some wierd results. It changes the mouse cursor but not in the right spot.

    Code:
    case WM_MOUSEMOVE: 
    POINT mousePos;
    RECT imagePos;
    
    mousePos.x = LOWORD(lParam); 
    mousePos.y = HIWORD(lParam);
    
    GetWindowRect(GetDlgItem(hwnd, IDC_BANNERSITE), &imagePos);
    if(PtInRect(&imagePos, mousePos))
    	SetCursor(LoadCursor(NULL, IDC_HAND));
    break;
    Even if I put the following code in WM_INITDIALOG it changes the cursor but only when it is on the dialog , not over any controls.

    Code:
    Hand=LoadCursor(NULL,(IDC_HAND));
    SetClassLong(hwnd,GCL_HCURSOR,(long)Hand);
    SetCursor(Hand);
    I think if I can capture the WM_MOUSEMOVE message for the button then I could change the cursor and it would work.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Try

    Hand=LoadCursor(NULL,(IDC_HAND));
    SetClassLong(hWndButton,GCL_HCURSOR,(long)Hand);
    SetCursor(Hand);

    as a work around


    PS
    I think the problem is in how you created the button.
    Its msgs are passed to the callback if you click on it?
    Did you create it with the WS_CHILD?
    Or is it from a resource editor?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    Thank you so much, it worked except that the mouse cursor changes for every button. No, the messages are not sent to a callback, thats what I wanted to know how to do in the first post. The button is created with a resource editor.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try the following:

    Subclass the button control you want to change the cursor for. As you have noted SetClassLong will change the selected paramater for ALL windows of that class. Use SetWindowLong to change the WNDPROC for a specific control, handle the WM_SETCURSOR for that control and use SetCursor within that handler to attach any cursor you want to that control. All other messages should be sent to the default wnd proc for the control with CallWindowProc for default handling behaviour.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    ok, I looked into SetWindowLong but you can only change the wndproc for a dialog. Isn't there anyway to just change the cursor of the one button instead of the whole class like SetClassLong does?

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I looked into SetWindowLong but you can only change the wndproc for a dialog<<

    You can change the wndproc for anything the SetWindowLong function will let you away with.

    See attached code for an example of subclassed button with changed cursor.
    Attached Files Attached Files
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    THANK YOU SO MUCH, I have been trying to do this for weeks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. Replies: 1
    Last Post: 01-25-2002, 08:58 PM
  5. Button Identifier Problems!
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21