Use GDI + to create buttons that support more picture formats

Source: Internet
Author: User

Derive a Cbuttonex class
Heavy OnCreate using GDI + map to button

int cbuttonex::oncreate (lpcreatestruct lpcreatestruct) { if (cbutton::oncreate (lpcreatestruct) = =-1) RET  Urn -1;  CRect RC;  GetClientRect (&RC);  CDC DC; using namespace  gdiplus;//setting Graphics graph (GetDC ()->M_HDC) that must be used before GDI + is applied;  Image Image (l "e:\\ picture related \ \ My works \\gdi+button.png"); Graph.  DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);  return 0; }

Handle MouseMove and MouseLeave for a better effect

voidCbuttonex::onmousemove (UINTnflags, CPoint point) {CRect rc;  GetClientRect (&RC); CDC DC;usingnamespacegdiplus;//setting Graphics graph (GetDC ()->M_HDC) that must be used before GDI + is applied;  Image Image (l "e:\\ picture related \ \ My works \\gdi+button2.png"); Graph.  DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);  TrackMouseEvent event; Event.cbsize =sizeof(event);  Event.dwflags = Tme_leave;  Event.dwhovertime = 0;  Event.hwndtrack = GetSafeHwnd ();  VERIFY (_trackmouseevent (&event));  Cbutton::onmousemove (nflags, point); }intCbuttonex::oncreate (lpcreatestruct lpcreatestruct) {if(Cbutton::oncreate (lpcreatestruct) = =-1)return-1;  CRect RC;  GetClientRect (&RC); CDC DC;usingnamespacegdiplus;//setting Graphics graph (GetDC ()->M_HDC) that must be used before GDI + is applied;  Image Image (l "e:\\ picture related \ \ My works \\gdi+button.png"); Graph. DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);return0; }LRESULTCbuttonex::onmouseleave (WPARAMWParam,LPARAMLParam) {CRect rc;  GetClientRect (&RC); CDC DC;using namespacegdiplus;//setting Graphics graph (GetDC ()->M_HDC) that must be used before GDI + is applied;  Image Image (l "e:\\ picture related \ \ My works \\gdi+button.png"); Graph. DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);return1; }

Where the MouseLeave function is to add a message response manually


afx_msg LRESULT OnMouseLeave (WPARAM WPARAM, LPARAM LPARAM);

On_message (Wm_mouseleave, OnMouseLeave)

If the button that is added directly with VC is set to the owner drawing style

Dynamic Create to add Bs_ownerdraw type

Overloaded DrawItem functions

  Void cbuttonex::D rawitem (lpdrawitemstruct lpdrawitemstruct)   { // TODO:  Add your code to draw the specified item  CRect rc;  getclientrect (&RC);  CDC dc;  using namespace gdiplus;//the settings required before applying  gdi+  graphics graph (GetDC ()->m_hDC);    USES_CONVERSION;  //  very complex definition    header file added #include  "atlbase.h"     WCHAR_T* TEMPBOARDPATH=A2W (M_resoucepath);//cstring  turn  wchar_t  if (M_ResoucePath.IsEmpty ())  {  image image (l "e:\\ picture related \ \ My works \\gdi+button  Crystal 1.png");   graph. DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);    else  {     image image (tempboardpath);       graph. DrawImage (&image,0,0,rc.right-rc.left,rc.bottom-rc.top);  }}

Add a function to receive the externally transmitted resource void Cbuttonex::setresoucepath (CString path) {M_resoucepath=path;}

In the Dlg class initialization section

With M_btnex.  Setresoucepath ("e:\\ picture related \ \ My works \\Recycle_bin_blue2.ico"); M_btnex2. Setresoucepath ("e:\\ picture related \ \ My works \\Recycle_bin_full.ico"); M_btnex. Create ("", bs_ownerdraw| ws_visible |  Ws_child, CRect (0,0,50,50),this, 11111); M_btnex2. Create ("", bs_ownerdraw| ws_visible | Ws_child, CRect (0,50,50,100),this, 11112);


OK, quick, create a try.

The upper-left corner is created dynamically in the initialization

In the middle is the control added with VC

Effect Figure 1

After hovering the mouse, you switch to a picture display.

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.