|
-
April 6th, 2004, 11:23 PM
#1
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.
-
April 7th, 2004, 03:36 AM
#2
It is possible using TrackMouseEvent Function !
Refer MSDN or this Site for getting more details !
-
April 9th, 2004, 02:24 AM
#3
if have sample project code, it will be great.
-
April 9th, 2004, 04:27 AM
#4
-
April 9th, 2004, 07:54 AM
#5
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.
-
April 9th, 2004, 10:04 AM
#6
Thanks for the code.
How can I process the bitmap button changes ? in
case WM_MOUSEHOVER: ?
-
April 14th, 2004, 05:54 AM
#7
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 ?
-
April 14th, 2004, 02:57 PM
#8
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).
-
April 14th, 2004, 06:07 PM
#9
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.
-
April 14th, 2004, 08:31 PM
#10
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.
-
April 14th, 2004, 08:32 PM
#11
when Mouse leaves the button, it will back to original bitmap button.
-
April 14th, 2004, 09:10 PM
#12
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.
-
April 14th, 2004, 11:49 PM
#13
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(hWnd, IDC_DLGBTN1);
hWndBtn2 = GetDlgItem(hWnd, IDC_DLGBTN2);
// load bitmaps
hBmpNormal = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BMP_NORMAL));
hBmpHiLight = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BMP_HILIGHT));
// set initial button images
SendMessage(hWndBtn1, BM_SETIMAGE,
(WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal );
SendMessage(hWndBtn2, BM_SETIMAGE,
(WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal);
// subclass buttons
wpOrigButtonProc = (WNDPROC) SetWindowLong(hWndBtn1,
GWLP_WNDPROC, (LONG) NewButtonProc);
/* same original btn proc */ SetWindowLong(hWndBtn2,
GWLP_WNDPROC, (LONG) NewButtonProc);
// in WM_CLOSE of main dialog procedure
SetWindowLong(hWndBtn1, GWLP_WNDPROC, (LONG)wpOrigButtonProc);
SetWindowLong(hWndBtn2, GWLP_WNDPROC, (LONG)wpOrigButtonProc);
// NewButtonProc window procedure
LRESULT CALLBACK NewButtonProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_MOUSEMOVE:
{
if (!bMouseInWindow)
{
bMouseInWindow = true;
SendMessage(hWnd, BM_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(hWnd, BM_SETIMAGE,
(WPARAM)IMAGE_BITMAP, (LPARAM)hBmpNormal);
}
break;
default:
return CallWindowProc(wpOrigButtonProc, hWnd,
uMsg, wParam, lParam);
}
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.
-
April 15th, 2004, 01:36 AM
#14
Where should I define _WIN32_WINNT as 0x0400 and WINVER as 0x0500 (or higher) ?
-
April 15th, 2004, 01:58 AM
#15
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 ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|