The example we want is to draw buttons with a bitmap containing a button (mouse movement, mouse off, mouse click) to draw buttons because three states
On a bitmap, the image height of each state is equal, and the width is 1/3 of the total length of the bitmap.
1. First create a subclass of CBitMapButton, and create four class member variables: / / Indicate whether the mouse is on the button BOOL M_BHOVER; / / The button is tracking the mouse BOOL M_BTRACKING; / / Save the variable of the picture CBitmap mybitmap; / / Button Size CSIZE M_BUTTONSIZE; 2. In the constructor of the class, initialization and mouse-related variables choverButton :: choverbutton () {m_bhover = false; m_btracking = false;}
3. Create a member function of the load bitmap, the parameter is the resource identifier of the bitmap. Before the button is self-painted, there must be a corresponding bitmap already loaded. BOOL LoadBitmap (UINT bitmapid); implemented as: BOOL CHoverButton :: LoadBitmap (UINT bitmapid) {// load image mybitmap.Attach (:: LoadImage (:: AfxGetInstanceHandle (), MAKEINTRESOURCE (bitmapid), IMAGE_BITMAP, 0,0 , Lr_loadmap3dcolors); bitmap bitmapbits; // Get bitmap information and store myBitMap.GetBitmap (& BitmapBits) in the BitmapBits structure; // Net position The corresponding height and 1/3 width. m_ButtonSize.cy = bitmapbits.bmHeight; m_ButtonSize.cx = bitmapbits.bmWidth / 3; SetWindowPos (NULL, 0,0, m_ButtonSize.cx, m_ButtonSize.cy, SWP_NOMOVE | SWP_NOOWNERZORDER); return TRUE;} virtual buttons 4. Overloaded Function DrawItem () Member Function When the appearance of a self-painted button changes by the framework. The prototype is: Virtual Void DrawItem (LPDRAWITEMSTRUCT LPDRAWITEMSTRUCT); the DrawItemStruct structure contains information that is drawn. Here is the function of the void CHoverButton :: DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct) {// device context stored in DRAWITEMSTRUCT structure and must be used when drawing the button CDC * mydc = CDC :: FromHandle (lpDrawItemStruct-> hDC);
// Create a compatible device context CDC * Pmemdc = New CDC; Pmemdc -> CreateCompATIBLEDC (MYDC);
/ / Save the old object cbitmap * Poldbitmap; PoldbitMap = Pmemdc -> selectObject (& mybitmap); cpoint point (0, 0); // If the button is selected, if it is the button bitmap that draws the selected state, in us In the bitmap, the button selected is the second IF (lpdrawitemstruct-> itemState & ods_selected) {mydc-> bitblt (0, 0, m_buttonsize.cx, m_buttonsize.cy, pmemdc, m_buttonsize.cx, 0, srccopy );} Else {// Judging whether the mouse is left or on the button to draw the corresponding bitmap if (m_bhover) {mydc-> bitblt (0, 0, m_buttonsize.cx, m_buttonsize.cy, pmemdc, m_buttonsize.cx * 2,0, srccopy;} else {mydc-> bitblt (0, 0, m_buttonsize.cx, m_buttonsize.cy, pmemdc, 0, 0, srcopy);}} // clean pmemdc -> selectObject (PoldbitMap); Delete Pmemdc;
5. For the mouse movement of the button, we can use its WM_MOUSEMOVE message to handle, and we must use the trackMouseEvent () function for the button to leave the form or hover on the mouse pointer. On the form is a send message. The message that can be sent is (WM_MouseLeave, WM_Mousehover, WM_NCMOUSELEAVE, WM_NCMOUSEHOVER)
This function comes with a TRACKMOUSEEVENT structure, which contains information that tracks the mouse. For the mouse moving on the button, the process code hover on the button is as follows:
void CHoverButton :: OnMouseMove (UINT nFlags, CPoint point) {if (m_bTracking!) {TRACKMOUSEEVENT tme; tme.cbSize = sizeof (tme); tme.hwndTrack = m_hWnd; tme.dwFlags = TME_LEAVE | TME_HOVER; tme.dwHoverTime = 1 ; M_btracking = _trackmouseevent (& TME);} cbitmapButton :: onmousemove (nflags, point);}
Lresult Chovebutton :: OnMouseLeave (WPARAM WPARAM, LPARAM LPARAM) {m_btracking = false; m_bhover = false; // Heavy painting button invalidate (TRUE); Return 0;}
Lresult Chovebutton :: OnMousehover (WPARAM WPARAM, LPARAM LPARAM) {
m_bhover = true; invalidate (true); return 0;}