First, this article introduces a CButton derived class Clinkbutton, the buttons made with this derived class have the following characteristics:
1, the appearance of the button is similar to the static control class CStatic objects produced. (See figure I)
Figure I
2, when the mouse cursor moved to the button, but did not press, the cursor changes shape, font change shape; buttons similar to the Flat button effect applied to toolbars and menus. (See figure II)
Figure II
3, when the button pressed the situation: (see figure III)
Figure Three
Two, the following specific description of the implementation of the button and steps:
- generates a dialog-based project in the VC6 IDE environment.
- Opens the property page of the button in the dialog resource, and selects the Owner Draw property of the button on the Style tab page. The
- introduces the cursor to the application's resources.
- derives a new class using ClassWizard, CButton as a base class: Clinkbutton.
- to overload a virtual function of the base class CButton in a derived class:
virtual void DrawItem (lpdrawitemstruct lpdrawitemstruct)
The reason to overload this function is that the application's framework calls this function to redraw the button when the visual behavior of the button is changed when the button's "Owner Draw" property has been selected.
- Customizes the following message processing:
afx_msg void OnMouseMove (UINT nflags, CPoint point);
afx_msg BOOL onsetcursor (cwnd* pwnd, UINT nhittest, UINT message);
afx_msg void OnTimer (UINT nidevent);
afx_msg void OnLButtonUp (UINT nflags, CPoint point);
afx_msg void OnLButtonDown (UINT nflags, CPoint point);
afx_msg int OnCreate (lpcreatestruct lpcreatestruct);
afx_msg BOOL onerasebkgnd (cdc* pDC);
- declares a class member variable definition:
///define font variable
CFont funderline;
Define the cursor variable
hcursor hhand;
Determines whether the button presses the
bool Blbtndown;
Determines whether the mouse is on a button
bool bhighlight;