Place a PNG image on the button

Source: Internet
Author: User
Header file"
C/C ++ code
   # Pragma export pngbutton: Public cbutton {declare_dynamic (pngbutton) Public: Enum button_state {button_enable = 0, button_hover = 1, button_click = 2, button_disable = 3 }; // button status pngbutton (); Virtual ~ Pngbutton (); protected: afx_msg lresult onmouseleave (wparam, lparam); afx_msg lresult onmousehover (wparam, lparam); listener () public: virtual void drawitem (callback); void setbuttonimage (wchar * Str); afx_msg void onmousemove (uint nflags, cpoint point); afx_msg void onlbuttondown (uint nflags, cpoint point ); afx_msg void onlbuttonup (uint nflags, cpoint point); bool m_bdisable; // whether the button disables bool m_bcursoronbutton; // whether the mouse is on the button bool m_bpress; // whether the button is pressed int m_nwidth; // int m_nheight of the image width; // int m_nslicewidth of the image height; // bitmap * m_btnimage of each image width; // button image crect m_rectbutton; // button area protected: Virtual lresult defwindowproc (uint message, wparam, lparam); Public: afx_msg bool onerasebkgnd (CDC * PDC );};

CPP file:

C/C ++ code
   # 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 ;}

Add a new class and test the code.

Add a button to the interface and set it to user self-Painting
Just add a variable control type to it and select this button class.

Call m_pngbt.setbuttonimage (L "res \ r. PNG") during initialization (the image is PNG)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.