Recently, I am studying button self-painting. The main difficulties of Button self-painting are as follows:
1. if you want to achieve a slightly more beautiful effect, I think these statuses must be considered: move the mouse, press the mouse, jump the mouse, move the mouse away. then you can process it accordingly. here we will mainly discuss how to capture these events: first, the mouse clicks and the mouse pops up the event, these two events are relatively simple, windows and we have already written these two response messages (we only need to reload them ). one is onlbuttonup and the other is onlbuttondown .. we can set a flag when the mouse is pressed or popped up. in fact, we can judge again in drawitem. (This item can be seen below) followed by mouse entry and mouse removal of the message. you don't think we can reload the onmousemove and onmouseleave messages. in fact, these two types of messages are "special" (because Windows does not directly process them for us). We need to use a Windows function (trackmouseevent) for judgment and processing. the approximate processing is as follows: we add the following code in the onmousemove response function:
// Bhighlight indicates the variable: indicates whether the mouse is on the button.
If (! Bhighlight)
{
Bhighlight = true;
Trackmouseevent TM;
TM. cbsize = sizeof (trackmouseevent );
TM. dwflags = tme_leave;
TM. dwhovertime = 0;
TM. hwndtrack = m_hwnd;
: Trackmouseevent (& TM );
}
// Button re-painting
Invalidaterect (null, true );
Then, we installed a similar mouse exit function: onmouseleave.
Now let's add the message response of this function:
Void clinkbutton: onmouseleave (wparam, lparam)
{
If (bhighlight)
{
// Redraw button
Bhighlight = false;
}
// Pay attention to this statement. If we do not refresh the interface, the interface may not be able to process the message with the mouse removed.
Invalidaterect (null, true );
}
After the two types of messages are added, we also need to do some initialization work to match wm_mouseleave and onmouseleave. Also, we need to use the following two functions: trackmouseevent, which must include such a declaration:
Extern "C" winuserapi bool winapi trackmouseevent (lptrackmouseevent lpeventtrack);. If not, the compiler does not know the trackmouseevent function.
For details about the trackmouseevent struct, refer to another blog.
Blog: http://blog.csdn.net/yuanweihuayan/article/details/6831248
2. Several different states are defined, which correspond to the first four states.
Here we mainly discuss how to obtain the system color.
◆ Color_3dhighlight and color_graytext are two colors for disable.
◆ Color_3dshadow and color_3dhilight are two colors for mouse pressing.
Here you can refer to the getsystemcolor function.
DWORD getsyscolor (INT nindex ); |
Description |
Determines the color of the display object in the specified Windows |
Return Value |
Long: Specifies the RGB color of the object. |
Parameter table |
Parameters |
Type and description |
Nindex |
Long, a constant that indicates a specific windows Display object, as shown in the following table |
Windows Object constant table |
Constant Definition |
Windows objects |
Constant Definition |
Windows objects |
Color_activeborder |
Border of the activity window |
Color_activecaption |
Title of the activity window |
Color_appworkspace |
MDI desktop background |
Color_background |
Windows Desktop |
Color_btnface |
Button |
Color_btnhighlight |
3D highlight area of the button |
Color_btnshadow |
3D shadow of buttons |
Color_btntext |
Button text |
Color_captiontext |
Text in the window title |
Color_graytext |
Gray text; zero if jitter technology is used |
Color_highlight |
Selected project background |
Color_highlighttext |
Selected project text |
Color_inactiveborder |
Border of the inactive window |
Color_inactivecaption |
Title of the inactive window |
Color_inactivecaptiontext |
Text in the inactive window |
Color_menu |
Menu |
Color_menutext |
Menu body |
Color_scrollbar |
Scroll bar |
Color_window |
Window background |
Color_windowframe |
Window Frame |
Color_windowtext |
Window body |
Color_3ddkshadow |
3D Deep shadow * |
Color_3dface |
Front color of a 3D shadow object * |
Color_3dhilight |
3D highlight color (Win95) |
Color_3dlight |
Light Color of 3D shadow objects * |
Color_infobk |
Background Color of the tooltip * |
Color_infotext |
Text color of the tooltip * |
|
|
*: The constant with "*" is not supported by NT 3.51. |
3. Some explanations about drawitem:
See http://blog.csdn.net/yuanweihuayan/article/details/6817631 for details
The key is the following struct: drawitemstruct, which is a frequently used member.
Itemstate: indicates the current state. macros and operations starting with ODS _ are required.
Rcitem: the widget size.
HDC: You can use it to obtain the CDC of the current control.
There are also several common member functions of CDC:
Draw3drect: used to draw 3D buttons.
Drawfocusrect: draws a rectangle with focus.
For specific usage, refer to msdn.
Today, I will summarize some of these ideas. I will continue to do better later ....