# Include "stdafx. H "# include" pngbutton. H "// pngbuttonimplement_dynamic (pngbutton, cbutton) pngbutton: pngbutton () {m_bdisable = false; m_bcursoronbutton = false; m_bpress = false;} pngbutton ::~ Pngbutton () {} second (pngbutton, cbutton) Second () on_message (wm_mousehover, onmousehover) on_message (messages, onmouseleave) Second () end_message_map () void pngbutton: drawitem (lpdrawitemstruct) {CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC); graphics (PDC-> m_hdc ); if (m_bdisable = true) {; graphics. drawimage (m_btnimage, rect (0, 0, m_nslicewidth, m_nheight), button_disable * m_nslicewidth, 0, m_nslicewidth, m_nheight, unitpixel, null, null );} else {// click state if (lpdrawitemstruct-> itemstate & ods_selected) {graphics. drawimage (m_btnimage, rect (0, 0, m_nslicewidth, m_nheight), button_click * m_nslicewidth, 0, m_nslicewidth, m_nheight, unitpixel, null, null );} // hover state else if (m_bpress) {graphics. drawimage (m_btnimage, rect (0, 0, m_nslicewidth, m_nheight), button_hover * m_nslicewidth, 0, m_nslicewidth, m_nheight, unitpixel, null, null );} // enable State else {graphics. drawimage (m_btnimage, rect (0, 0, m_nslicewidth, m_nheight), button_enable * m_nslicewidth, 0, distance, m_nheight, unitpixel, null) ;}} void pngbutton:: setbuttonimage (wchar * Str) {m_btnimage = new Bitmap (STR); m_nwidth = m_btnimage-> getwidth (); m_nheight = m_btnimage-> getheight (); m_nslicewidth = m_nwidth/4; // cut the image into four parts: cwnd * pwnd = This-> getparent (); getwindowrect (& m_rectbutton); pwnd-> screentoclient (m_rectbutton); m_rectbutton.right = m_rectbutton.left + m_nslice; cursor = m_rectbutton.top + m_nheight; movewindow (m_rectbutton); // adjust the button size to fit the image} void pngbutton: onmousemove (uint nflags, cpoint) {If (m_bcursoronbutton = false) {trackmouseevent tme; zeromemory (& tme, sizeof (trackmouseevent); tme. cbsize = sizeof (TME); tme. hwndtrack = m_hwnd; tme. dwflags = tme_leave | tme_hover; tme. dwhovertime = 1; m_bcursoronbutton = _ trackmouseevent (& TME);} cbutton: onmousemove (nflags, point);} lresult pngbutton: onmouseleave (wparam, lparam) {m_bcursoronbutton = false; m_bpress = false; invalidate (); Return 0l;} lresult pngbutton: onmousehover (wparam, lparam) {m_bpress = true; invalidate (); return 0l;} void pngbutton: onlbuttondown (uint nflags, cpoint point) {invalidate (); cbutton: onlbuttondown (nflags, point);} void pngbutton :: onlbuttonup (uint nflags, cpoint point) {invalidate (); cbutton: equals (nflags, point);} lresult pngbutton: defwindowproc (uint message, wparam, lparam) {If (Message = wm_lbuttondblclk) {message = wm_lbuttondown;} return cbutton: defwindowproc (message, wparam, lparam); // very important. Eliminate double-click failure} bool pngbutton:: onerasebkgnd (CDC * PDC) {return true ;}