CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 48

Thread: Mouseover event

  1. #1
    Join Date
    Mar 2004
    Posts
    147

    Mouseover event

    Env: Win32 (Visual C++)

    How can I implement the Mouseover event ? Assuming I have two bitmap buttons (IDC_Btn1, IDC_Btn2) in main dialog.

    When Mouseover, IDC_Btn1 will change to IDC_Btn2. When Click, only the child Dialog will be displayed, and the button remains IDC_Btn2.

    Any sample code, will be great. Thanks.

  2. #2
    Join Date
    Oct 2003
    Location
    India
    Posts
    195
    It is possible using TrackMouseEvent Function !

    Refer MSDN or this Site for getting more details !

  3. #3
    Join Date
    Mar 2004
    Posts
    147
    if have sample project code, it will be great.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  5. #5
    Join Date
    Jan 2002
    Posts
    195
    declare this globally
    Code:
    TRACKMOUSEEVENT tme;
    put this in WinMain
    Code:
    tme.cbSize=sizeof(tme);
    tme.dwFlags=TME_HOVER;
    tme.hwndTrack=hWnd;]//hanlde of window you want the mouse over message for.
    tme.dwHoverTime=HOVER_DEFAULT;
    
    TrackMouseEvent(&tme)
    put this in the Window Proceedure for the window
    Code:
         case WM_MOUSEHOVER:
              //Process Mouse Over message here.

  6. #6
    Join Date
    Mar 2004
    Posts
    147
    Thanks for the code.

    How can I process the bitmap button changes ? in
    case WM_MOUSEHOVER: ?

  7. #7
    Join Date
    Mar 2004
    Posts
    147
    I would like process mouseover event for each button in my main Dialog. Not sure if your approach will work in my case.

    Why I got TrackMouseEvent : undeclared identifier ?

  8. #8
    Join Date
    Sep 2002
    Posts
    924
    I really haven't worked with TrackMouseEvent as of yet, but I think you should still be able to do what you want using it.

    Set the HWND in your TRACKMOUSEEVENT structure to be that of the dialog.

    In your WM_MOUSEHOVER handler, use GetCursorPos and ChildWindowFromPoint to do hit testing, and compare the HWND value returned with the HWND's for your buttons to see if the cursor is over a button. If it is over a button then use SetWindowLong with GWL_ID to change the Control ID of your button, etc.

    TrackMouseEvent is defined for Windows 98 and later, so you may have to define _WIN32_WINDOWS=0x0410 to use it (may be why you are getting undeclared identifier message).

  9. #9
    Join Date
    Sep 2002
    Posts
    924
    BTW:
    I am not exactly sure what it is you are trying to do, as changing the Control ID for a button when the mouse hovers over it seems a bit strange (are you doing this only once? should the button revert back to the original id at some point? etc.) Also, I am not sure if you want to catch the message when the mouse moves over the button or just when it hovers over the button. If you want to do it when the mouse moves over the button then you would probably do it differently. You would probably need to subclass the button and handle the WM_MOUSEMOVE message in the button's window procedure. Again, I cannot be specific as I do not completly understand what it is you want to do, but you can check out ownerdrawn button examples for flat buttons, etc, to see how it is handled. Basically you would handle the WM_MOUSEMOVE message so that you know when the mouse moves over the button (check which mouse buttons are pressed, if any, etc), and under the appropriate conditions use TrackMouseEvent (within the handler for WM_MOUSEMOVE), with TME_LEAVE for dwFlags paramater in the TRACKMOUSEEVENT structure, so that you are sent the WM_MOUSELEAVE message when the mouse leaves the button's area, etc (and handle that message). This way you know when the mouse enters the buttons area, and when it leaves (you would probably want to use global boolean variables as flags to keep track of things, so you can test for certain conditions and only handle the WM_MOUSEMOVE message under the those conditions, and clear those flags in the WM_MOUSELEAVE message, etc). The specifics would depend on the specifics of what you want to do.

  10. #10
    Join Date
    Mar 2004
    Posts
    147
    ok...very simple. what I to do is to swap between two bitmap button when mouseover the button. That's all.

    I think there is not necessary to change any Control ID, etc.

  11. #11
    Join Date
    Mar 2004
    Posts
    147
    when Mouse leaves the button, it will back to original bitmap button.

  12. #12
    Join Date
    Sep 2002
    Posts
    924
    ahh OK, your original post made it sound like you wanted to change control id's.
    Originally posted by Dimension
    When Mouseover, IDC_Btn1 will change to IDC_Btn2.
    Also "swap between two bitmap button" sounds like you want to swap buttons.
    Originally posted by Dimension
    what I to do is to swap between two bitmap button when mouseover the button
    Probably just an language issue, not a big deal (just confusing).

    So what you want is a normal button that changes pictures when the mouse is over it.
    In that case, I assume you have at at least 2 bitmaps, one for the MouseOver state and one for the normal state.
    You should probably use a global boolean variable, i.e. bIsHighlighted to track which image the button currently has, initially set to false.
    In your subclassed buttons window proc, handle the WM_MOUSEMOVE message. In the WM_MOUSEMOVE handler, check to see if bIsHighlighted is TRUE and if it is, return the default window proc, etc. If it is false, set it to true, and send the BM_SETIMAGE message to change the buttons bitmap to the MouseOver version. Use TrackMouseEvent (with TME_LEAVE) so that it sends the WM_MOUSELEAVE message when the mouse leaves the buttons area.
    Handle the WM_MOUSELEAVE message, and in tht handler, set bIsHighlighted to false, and send the BM_SETIMAGE message to set the buttons image back to normal state, etc.

    Are you doing this for multiple buttons, or just one button?

    It is not that difficult, if I have the chance I will see if I can provide an example for you, if you cannot figure out how to do it yourself.

    BTW:
    To use WM_MOUSEMOVE and WM_MOUSELEAVE and TRACKMOUSEEVENT you need to define _WIN32_WINNT as 0x0400 and WINVER as 0x0500 (or higher).
    Last edited by RussG1; April 14th, 2004 at 11:57 PM.

  13. #13
    Join Date
    Sep 2002
    Posts
    924
    Here is an example, for two buttons (both buttons have the BS_BITMAP style and use the same bitmaps).
    PHP Code:
    //////////// Globals ////////////
    // HWND hWndBtn1 = NULL;
    // HWND hWndBtn2 = NULL;
    // WNDPROC wpOrigButtonProc = NULL;
    // HBITMAP hBmpNormal = NULL;
    // HBITMAP hBmpHiLight = NULL;
    // BOOL bMouseInWindow = FALSE;

    // IDC_DLGBTN1 is a button resource
    // IDC_DLGBTN2 is a button resource
    // IDB_BMP_NORMAL is a bitmap resource
    // IDB_BMP_HILIGHT is a bitmap resource

    // in WM_INITDIALOG of main dialog procedure
    hWndBtn1 GetDlgItem(hWndIDC_DLGBTN1);
    hWndBtn2 GetDlgItem(hWndIDC_DLGBTN2);
         
    // load bitmaps
    hBmpNormal  LoadBitmap(hInstMAKEINTRESOURCE(IDB_BMP_NORMAL));
    hBmpHiLight LoadBitmap(hInstMAKEINTRESOURCE(IDB_BMP_HILIGHT));
         
    // set initial button images        
    SendMessage(hWndBtn1BM_SETIMAGE
              (
    WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal );
    SendMessage(hWndBtn2BM_SETIMAGE
              (
    WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal);
         
    // subclass buttons            
    wpOrigButtonProc = (WNDPROCSetWindowLong(hWndBtn1,
              
    GWLP_WNDPROC, (LONGNewButtonProc); 
    /* same original btn proc */ SetWindowLong(hWndBtn2
              
    GWLP_WNDPROC, (LONGNewButtonProc); 

    // in WM_CLOSE of main dialog procedure
    SetWindowLong(hWndBtn1GWLP_WNDPROC, (LONG)wpOrigButtonProc);
    SetWindowLong(hWndBtn2GWLP_WNDPROC, (LONG)wpOrigButtonProc);

    // NewButtonProc window procedure
    LRESULT CALLBACK NewButtonProc(HWND hWndUINT uMsgWPARAM wParamLPARAM lParam)
    {
         switch(
    uMsg)
         {
              case 
    WM_MOUSEMOVE:
              {            
                   if (!
    bMouseInWindow)
                   {
                        
    bMouseInWindow true;
                        
    SendMessage(hWndBM_SETIMAGE
                             (
    WPARAM)IMAGE_BITMAP, (LPARAM)hBmpHiLight );
                        
    TRACKMOUSEEVENT tme;
                        
    tme.cbSize sizeof(tme);
                        
    tme.dwFlags TME_LEAVE;
                        
    tme.hwndTrack hWnd;
                        
    TrackMouseEvent(&tme);
                   }
              }
              break;
              case 
    WM_MOUSELEAVE:
              {
                   
    bMouseInWindow false;
                   
    SendMessage(hWndBM_SETIMAGE
                        (
    WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal);
              }
              break;
              default:
                   return 
    CallWindowProc(wpOrigButtonProchWnd
                        
    uMsgwParamlParam);
         }
         return 
    true;

    If you want to use different bitmap images for the different buttons, then you could compare the HWND value to determine which button the mouse is over (or is leaving), and set the correct bitmap for that button, etc.
    This is just the basic stuff. You may wanna check which mouse button is pressed in your WM_MOUSEMOVE procedure and do something different dependiing on which button is pressed, etc.
    Last edited by RussG1; April 14th, 2004 at 11:53 PM.

  14. #14
    Join Date
    Mar 2004
    Posts
    147
    Where should I define _WIN32_WINNT as 0x0400 and WINVER as 0x0500 (or higher) ?

  15. #15
    Join Date
    Mar 2004
    Posts
    147
    I defined this
    _WIN32_WINNT as 0x0400 and WINVER as 0x0500
    in StdAfx.h. It works ok.

    I got this error C2065: 'GWLP_WNDPROC' : undeclared identifier.
    Why ?

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured