Use GDI + to create buttons that support more image formats

Source: Internet
Author: User

Derive a cbuttonex class
Reload oncreate to use the GDI + texture to the button

 

  1. IntCbuttonex: oncreate (maid)
  2. {
  3. If(Cbutton: oncreate (lpcreatestruct) =-1)
  4. Return-1;
  5. Crect RC;
  6. Getclientrect (& rc );
  7. Cdc dc;
  8. Using NamespaceGdiplus;// Required before using GDI +
  9. Graphics graph (getdc ()-> m_hdc );
  10. Image image (L"E: \ image related \ my work \ gdi?button.png");
  11. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  12. Return0;
  13. }

Processing mousemove and mouseleave for better results

 

  1. VoidCbuttonex: onmousemove (UintNflags, cpoint point)
  2. {
  3. Crect RC;
  4. Getclientrect (& rc );
  5. Cdc dc;
  6. Using NamespaceGdiplus;// Required before using GDI +
  7. Graphics graph (getdc ()-> m_hdc );
  8. Image image (L"E: \ image related \ my work \ gdi?button2.png");
  9. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  10. Trackmouseevent event;
  11. Event. cbsize =Sizeof(Event );
  12. Event. dwflags = tme_leave;
  13. Event. dwhovertime = 0;
  14. Event. hwndtrack = getsafehwnd ();
  15. Verify (_ trackmouseevent (& event ));
  16. Cbutton: onmousemove (nflags, point );
  17. }
  18. IntCbuttonex: oncreate (maid)
  19. {
  20. If(Cbutton: oncreate (lpcreatestruct) =-1)
  21. Return-1;
  22. Crect RC;
  23. Getclientrect (& rc );
  24. Cdc dc;
  25. Using NamespaceGdiplus;// Required before using GDI +
  26. Graphics graph (getdc ()-> m_hdc );
  27. Image image (L"E: \ image related \ my work \ gdi?button.png");
  28. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  29. Return0;
  30. }
  31. LresultCbuttonex: onmouseleave (WparamWparam,LparamLparam)
  32. {
  33. Crect RC;
  34. Getclientrect (& rc );
  35. Cdc dc;
  36. Using NamespaceGdiplus;// Required before using GDI +
  37. Graphics graph (getdc ()-> m_hdc );
  38. Image image (L"E: \ image related \ my work \ gdi?button.png");
  39. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  40. Return1;
  41. }

 

The mouseleave function must manually add a message response.


 

    1. Afx_msgLresultOnmouseleave (WparamWparam,LparamLparam );

 

    1. On_message (wm_mouseleave, onmouseleave)

 

 

 

If you want to use the buttons directly added by VC to set the style as the owner

The bs_ownerdraw type must be added for dynamic creation.

Reload the drawitem Function

 

  1. VoidCbuttonex: drawitem (lpdrawitemstruct)
  2. {
  3.  // Todo: add your code to draw the specified item
  4. Crect RC;
  5. Getclientrect (& rc );
  6. Cdc dc;
  7.  Using NamespaceGdiplus;// Required before using GDI +
  8. Graphics graph (getdc ()-> m_hdc );
  9. Uses_conversion;// Added # include "atlbase. H" to the complicated definition header file"
  10.  Wchar_t* Tempboardpath = a2w (m_resoucepath );// Convert cstring to wchar_t
  11.  If(M_resoucepath.isempty ())
  12. {
  13. Image image (L"E: \ image related \ my work \ GDI + button crystal 1.png");
  14. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  15. }
  16.  Else
  17. {
  18. Image image (tempboardpath );
  19. Graph. drawimage (& image, RC. right-rc.left, RC. bottom-rc.top );
  20. }
  21. }

Add a function to receive external resources.

    1. VoidCbuttonex: setresoucepath (cstring path)
    2. {
    3. M_resoucepath = path;
    4. }

Initialize the DLG class

Use

  1. M_btnex.setresoucepath ("E: \ image related \ my work \ recycle_bin_blue2.ico");
  2. M_btnex2.setresoucepath ("E: \ image related \ my work \ recycle_bin_full.ico");
  3. M_btnex.create ("", Bs_ownerdraw | ws_visible | ws_child, crect (0, 50, 50 ),This, 11111 );
  4. M_btnex2.create ("", Bs_ownerdraw | ws_visible | ws_child, crect (50,100 ),This, 11112 );

 

OK. Create one. Try it ~~

The one in the upper left corner is dynamically created during initialization.

In the middle is the control added with VC

 

1

After hovering over the mouse, you can switch to an image 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.