Thread: Checking if the mouse is over a button

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

    Checking if the mouse is over a button

    Hey, how can I check to see if the mouse cursor is over a button and if it is then change the mouse cursor?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    When you get the mouse move message, the high part of lparam is the y cordinate, and the low part of lparam is the x cordinate.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You could sublass the button and trap the messages...

    Using spy++ it looks like the button would recieve, WM_MOUSEMOVE, WM_SETCURSOR and WM_NCHITTEST....

    Try trapping one or a few of these and see if you get any results

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    there's like a WM_MOUSEOVER or WM_MOUSEHOVER or something message you could trap if you subclass it. I forget the exact message name.

  5. #5
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Look up ChildWindowFromPoint();

    HWND ChildWindowFromPoint(
    HWND hWndParent, // handle to parent window
    POINT Point // structure with point coordinates
    );


    use WM_MOUSEMOVE and make somthing like this i have never tried this so i dunno if it will work

    Code:
    case WM_MOUSEMOVE:
    {
    POINT pt;
    HWND hRet;
    
    pt.x = LOWORD(lParam);  
    pt.y = HIWORD(lParam);
    
    hRet = ChildWindowFromPoint(hWnd, pt);
    
    if(hRet == hWndMyButton)
    {
    //yea do some stuff
    }
    
    else
    {
    //it isnt on that button
    }
    
    }

    I read up that function in my book casue i had been wanting to do the same thing, but for some reason i never got around to testing it . If you want to check multiple handles your gonna have to make a big nested structure of ifs and eleses....

    Hope that helps/works,
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    Ok, this is wierd. When I use the code below the message box comes up saying it isn't over when the mouse is on the dialog box. However when the mouse is over any control such as a button nothing comes up.

    case WM_MOUSEMOVE:
    POINT pt;
    HWND hRet;

    pt.x = LOWORD(lParam);
    pt.y = HIWORD(lParam);

    hRet = ChildWindowFromPoint(hwnd, pt);

    if(hRet == GetDlgItem(hwnd, IDC_BANNERSITE))
    {
    MessageBox(hwnd, "is over banner", "mouse", MB_OK);
    }
    else
    {
    MessageBox(hwnd, "isn't over banner", "mouse", MB_OK);
    }
    break;

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Watch the client area cood's v's screen cood's as this may be your problem.

    How about PtInRect() function

    Check each mouse move msg to see if the mouse is in the buttons area (GetWindowRect() )
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 12-08-2008, 07:13 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. checking is mouse button is down
    By h_howee in forum Windows Programming
    Replies: 4
    Last Post: 06-30-2007, 10:14 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM