• 使用GDI+创建支持更多图片格式的按钮


    派生一个 CButtonEx类
    重载 OnCreate  利用GDI+贴图到 按钮上

    1. int CButtonEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    2. if (CButton::OnCreate(lpCreateStruct) == -1) 
    3. return -1; 
    4. CRect rc; 
    5. GetClientRect(&rc); 
    6. CDC dc; 
    7. using namespace Gdiplus;//应用 GDI+前必须的设置 
    8. Graphics graph(GetDC()->m_hDC); 
    9. Image image(L"E:\\图片相关\\我的作品\\GDI+BUTTON.png"); 
    10. graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top); 
    11. return 0; 

    为了好一点的效果处理 MouseMove 和MouseLeave

    1. void CButtonEx::OnMouseMove(UINT nFlags, CPoint point) 
    2. CRect rc; 
    3. GetClientRect(&rc); 
    4. CDC dc; 
    5. using namespace Gdiplus;//应用 GDI+前必须的设置 
    6. Graphics graph(GetDC()->m_hDC); 
    7. Image image(L"E:\\图片相关\\我的作品\\GDI+BUTTON2.png"); 
    8. graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top); 
    9. TRACKMOUSEEVENT event ; 
    10. event.cbSize = sizeof( event ); 
    11. event.dwFlags = TME_LEAVE ; 
    12. event.dwHoverTime = 0 ; 
    13. event.hwndTrack = GetSafeHwnd() ; 
    14. VERIFY (_TrackMouseEvent( &event )) ; 
    15. CButton::OnMouseMove(nFlags, point); 
    16. int CButtonEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    17. if (CButton::OnCreate(lpCreateStruct) == -1) 
    18. return -1; 
    19. CRect rc; 
    20. GetClientRect(&rc); 
    21. CDC dc; 
    22. using namespace Gdiplus;//应用 GDI+前必须的设置 
    23. Graphics graph(GetDC()->m_hDC); 
    24. Image image(L"E:\\图片相关\\我的作品\\GDI+BUTTON.png"); 
    25. graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top); 
    26. return 0; 
    27. LRESULT CButtonEx::OnMouseLeave( WPARAM wParam , LPARAM lParam ) 
    28. CRect rc; 
    29. GetClientRect(&rc); 
    30. CDC dc; 
    31. using namespace Gdiplus;//应用 GDI+前必须的设置 
    32. Graphics graph(GetDC()->m_hDC); 
    33. Image image(L"E:\\图片相关\\我的作品\\GDI+BUTTON.png"); 
    34. graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top); 
    35. return 1; 

     

    其中MouseLeave函数要手动添加消息响应


     

    1. afx_msg LRESULT OnMouseLeave( WPARAM wParam , LPARAM lParam ) ; 

     

    1. ON_MESSAGE( WM_MOUSELEAVE , OnMouseLeave )

     

     

     

    如果是用VC直接添加的按钮要设置为 所有者绘制 样式

    动态创建要加上 BS_OWNERDRAW 类型

    重载DrawItem函数

     

    1. void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    2. {
    3.  // TODO: Add your code to draw the specified item
    4.  CRect rc;
    5.  GetClientRect(&rc);
    6.  CDC dc;
    7.  using namespace Gdiplus;//应用 GDI+前必须的设置
    8.  Graphics graph(GetDC()->m_hDC); 
    9.  USES_CONVERSION;  // 很复杂的定义  头文件添加了#include "atlbase.h" 
    10.  wchar_t* TempBoardPath=A2W(m_ResoucePath);//CString 转 wchar_t
    11.  if(m_ResoucePath.IsEmpty())
    12.  {
    13.   Image image(L"E:\\图片相关\\我的作品\\GDI+BUTTON 水晶1.png");
    14.   graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top);
    15.  }
    16.  else
    17.  {
    18.      Image image(TempBoardPath);
    19.      graph.DrawImage(&image,0,0,rc.right-rc.left,rc.bottom-rc.top);
    20.  }
    21. }

    再添加一个函数用来接收外部传来的资源

    1. void CButtonEx::SetResoucePath(CString path)
    2. {
    3.     m_ResoucePath=path;
    4. }

    在Dlg类初始化部分

    1. m_btnex.SetResoucePath("E:\\图片相关\\我的作品\\Recycle_bin_blue2.ico");
    2.  m_btnex2.SetResoucePath("E:\\图片相关\\我的作品\\Recycle_bin_full.ico");
    3. m_btnex.Create(" ",BS_OWNERDRAW|WS_VISIBLE | WS_CHILD, CRect(0,0,50,50),this,11111);
    4.  m_btnex2.Create(" ",BS_OWNERDRAW|WS_VISIBLE | WS_CHILD, CRect(0,50,50,100),this,11112);


     

    OK 快点Create一个试试看~~

    左上角那个是在初始化中动态创建的

    中间的是用VC添加的控件

    效果图1

    鼠标悬停后 也就是切换一张图片显示

  • 相关阅读:
    [Jobdu] 题目1528:最长回文子串
    [Jobdu] 题目1510:替换空格
    [Leetcode] Candy
    [Leetcode] Jump Game
    [Leetcode] Longest Valid Parentheses
    [Leetcode] Triangle
    [Leetcode] Populating Next Right Pointers in Each Node
    java web作用域page request session application
    String对象不可改变的特性及内存机制
    Java IO
  • 原文地址:https://www.cnblogs.com/buffer/p/1410293.html
Copyright © 2020-2023  润新知