I want to implement a simpl开发者_如何学Ce volume up/down button using a custom bitmap, and all it's going to have is a simple hover effect, and mouse down effect.
My first idea was to processWM_MOUSEMOVE
for the hover, and process WM_LBUTTONUP
and WM_LBUTTONDOWN
for the mouse down effect.
First, is this the easiest way to do this? I could superclass a button and just paint the bitmap and forget about the text...
Then I have the problem with the background, which should be transparent, I know I can use a mask by passing SRCAND
to BitBlt
, but this requires me to have 2 images for each button. Is there any way to do this with just one image? Like if I put a green background on it in my image editor, could I mask that out with a green screen like effect?
You need to create a regular button, and subclass it with SetWindowSubclass. The button must have the owner-draw style on it, which means in the parent WndProc, you are handling WM_DRAWITEM. As you correctly say, you will be BitBlt'ing or StretchBlt'ing the background of the image on.
For hover events you must do the following:
- Have some sort of shared boolean between the parent and subclassed WndProc, eg.
IsMousedOver
- In the subclassed WndProc, process WM_MOUSEMOVE. When this message is hit, you should set
IsMousedOver
, then invalidate the control with InvalidateRect (this will triggerWM_DRAWITEM
in the parent) - In the parent WndProc, if you receive
WM_MOUSEMOVE
, you should clearIsMousedOver
- In
WM_DRAWITEM
in the parent WndProc, you should check IsMousedOver and BitBlt based on that state
If you want to process MouseUp/MouseDown, you can change the boolean to an int instead and have a tri-state. However, most implementations have MouseDown being the same as the regular button, and MouseUp is then simply the regular moused over background.
As for your query about 2 images, it may well be possible to do it with one but I haven't tried that before.
精彩评论