改变CButton背景颜色(自绘CButton)---VC

这是一个实现自绘CButton的类,期望对大家有点帮助,我提供这个类,只是为大家提供一个自绘控件的思路,你可以在此基础上,随意地修改代码,直到它能完成你所需要的功能.其它控件的自绘,原理了和这个差不多.
  1. #if !defined(AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_)
  2. #define AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. // ButtonST.h : header file
  7. //
  8. /
  9. // CButtonST window
  10. class CButtonST : public CButton
  11. {
  12. // Construction
  13. public:
  14.     CButtonST();
  15. // Attributes
  16. public:
  17.     
  18. // Operations
  19. public:
  20.     //设置背景颜色
  21.     void SetBkColor(COLORREF BkColor);
  22.     //设置鼠标在按钮上的偏移颜色
  23.     void SetShOffset(int nShOffset);
  24.     //设置字体颜色
  25.     void SetTextColor(COLORREF TextColor);
  26.     //设置网页链接
  27.     void SetURL(CString strURL);
  28.     //设置背景图片
  29.     void SetBkPicture(CBitmap *pBitMap);
  30. // Overrides
  31.     // ClassWizard generated virtual function overrides
  32.     //{{AFX_VIRTUAL(CButtonST)
  33.     public:
  34.     virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  35.     virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
  36.     protected:
  37.     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
  38.     virtual void PreSubclassWindow();
  39.     //}}AFX_VIRTUAL
  40. // Implementation
  41. public:
  42.     virtual ~CButtonST();
  43.     
  44.     void DrawItem1(LPDRAWITEMSTRUCT lpDrawItemStruct);
  45.     // Generated message map functions
  46. protected:
  47.     //{{AFX_MSG(CButtonST)
  48.     afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  49.     afx_msg BOOL OnNcActivate(BOOL bActive);
  50.     afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
  51.     afx_msg void OnKillFocus(CWnd* pNewWnd);
  52.     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  53.     //}}AFX_MSG
  54. protected:
  55.     //鼠标是否在按钮上面
  56.     BOOL m_IsPressed;
  57.     COLORREF m_BkColor;
  58.     int      m_nShOffset;
  59.     COLORREF m_TextColor;
  60.     CString  m_strURL;
  61.     CBitmap  *m_pBitMapBK;
  62. protected:
  63.     //背景颜色偏移
  64.     COLORREF OffsetColor(COLORREF color,int nOffset);
  65.     //鼠标离开
  66.     LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
  67.     DECLARE_MESSAGE_MAP()
  68. };
  69. /
  70. //{{AFX_INSERT_LOCATION}}
  71. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  72. #endif // !defined(AFX_BUTTONST_H__1271FF9C_E28C_4D3B_B429_AFE65924A5D0__INCLUDED_)

 

 

  1. // ButtonST.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ContrlST.h"
  5. #include "ButtonST.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define BS_TYPEMASK SS_TYPEMASK
  12. /
  13. // CButtonST
  14. CButtonST::CButtonST()
  15. {   
  16.     m_IsPressed = FALSE;
  17.     m_BkColor = RGB(216,233,216);
  18.     m_nShOffset = 30;
  19.     m_TextColor = RGB(0,0,0);
  20.     m_strURL = "";
  21.     m_pBitMapBK = NULL;
  22. }
  23. CButtonST::~CButtonST()
  24. {
  25.     if(m_pBitMapBK!=NULL)
  26.     {
  27.         delete m_pBitMapBK;
  28.         m_pBitMapBK = NULL;
  29.     }
  30. }
  31. BEGIN_MESSAGE_MAP(CButtonST, CButton)
  32.     //{{AFX_MSG_MAP(CButtonST)
  33.     ON_WM_MOUSEMOVE()
  34.     ON_WM_NCACTIVATE()
  35.     ON_WM_NCMOUSEMOVE()
  36.     ON_WM_KILLFOCUS()
  37.     ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  38.     ON_WM_LBUTTONDOWN()
  39.     //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /
  42. // CButtonST message handlers
  43. //设置背景颜色
  44. void CButtonST::SetBkColor(COLORREF BkColor)
  45. {
  46.     m_BkColor = BkColor;
  47.     this->Invalidate();
  48. }
  49. //设置鼠标在按钮上的偏移颜色
  50. void CButtonST::SetShOffset(int nShOffset)
  51. {
  52.     m_nShOffset = nShOffset;
  53.     this->Invalidate();
  54. }
  55. //设置字体颜色
  56. void CButtonST::SetTextColor(COLORREF TextColor)
  57. {
  58.     m_TextColor = TextColor;
  59.     this->Invalidate();
  60. }
  61. //设置网页链接
  62. void CButtonST::SetURL(CString strURL)
  63. {
  64.     m_strURL = strURL;
  65. }
  66. //设置背景图片
  67. void CButtonST::SetBkPicture(CBitmap *pBitMap)
  68. {
  69.     if(m_pBitMapBK==pBitMap)
  70.         return;
  71.     if(m_pBitMapBK!=NULL)
  72.     {
  73.         delete m_pBitMapBK;
  74.         m_pBitMapBK = pBitMap;
  75.     }
  76.     m_pBitMapBK = pBitMap;
  77.     this->Invalidate();
  78. }
  79. //背景颜色偏移
  80. COLORREF CButtonST::OffsetColor(COLORREF color,int nOffset)
  81. {
  82.     BYTE    byRed = 0;
  83.     BYTE    byGreen = 0;
  84.     BYTE    byBlue = 0;
  85.     short   shOffsetR = nOffset;
  86.     short   shOffsetG = nOffset;
  87.     short   shOffsetB = nOffset;
  88.     if (nOffset < -255 || nOffset > 255)
  89.     {
  90.         nOffset = 30;
  91.     }
  92.     
  93.     // Get RGB components of specified color
  94.     byRed = GetRValue(color);
  95.     byGreen = GetGValue(color);
  96.     byBlue = GetBValue(color);
  97.     
  98.     // Calculate max. allowed real offset
  99.     if (nOffset > 0)
  100.     {
  101.         if (byRed + nOffset > 255)      shOffsetR = 255 - byRed;
  102.         if (byGreen + nOffset > 255)    shOffsetG = 255 - byGreen;          
  103.         if (byBlue + nOffset > 255)     shOffsetB = 255 - byBlue;
  104.         nOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
  105.     }
  106.     else
  107.     {
  108.         if (byRed + nOffset < 0)        shOffsetR = -byRed;
  109.         if (byGreen + nOffset < 0)      shOffsetG = -byGreen;
  110.         if (byBlue + nOffset < 0)       shOffsetB = -byBlue;
  111.         
  112.         nOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
  113.     }
  114.     int r,g,b;
  115.     r = byRed + nOffset;
  116.     g =byGreen + nOffset;
  117.     b =byBlue + nOffset;
  118.     return RGB(r,g,b);
  119. }
  120. void CButtonST::DrawItem1(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  121. {
  122.     // TODO: Add your code to draw the specified item   
  123.     
  124.     
  125.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  126.     CRect rect = lpDrawItemStruct->rcItem;
  127.     CRect offRect = rect;
  128.     int ndist = 2;
  129.     offRect.left+= ndist;
  130.     offRect.right -=ndist;
  131.     offRect.top += ndist;
  132.     offRect.bottom -=ndist;
  133.     
  134.     
  135.     pDC->SetBkMode(TRANSPARENT);
  136.     pDC->SetTextColor(m_TextColor);
  137.     
  138.     //绘制背景
  139.     if(m_pBitMapBK!=NULL)
  140.     {
  141.         CDC* pNewDC = new CDC;
  142.         pNewDC->CreateCompatibleDC(pDC);
  143.         pNewDC->SelectObject(m_pBitMapBK);
  144.         BITMAP   bmp;
  145.         m_pBitMapBK->GetBitmap(&bmp);
  146.         if(m_IsPressed)
  147.         {
  148.             pDC->StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
  149.         }
  150.         else
  151.         {
  152.             CBrush brush(RGB(220,220,220));//RGB(m_nShOffset,m_nShOffset,m_nShOffset));
  153.             pDC->FillRect(&rect,&brush);
  154.             pDC->StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCAND);
  155.         }
  156.         
  157.         delete pNewDC;
  158.         pNewDC = NULL;
  159.     }
  160.     else
  161.     {
  162.         if(m_IsPressed)
  163.         {
  164.             CBrush brush(OffsetColor(m_BkColor,m_nShOffset));
  165.             pDC->FillRect(rect,&brush);             
  166.             CBrush brush1(RGB(128,128,128));
  167.             pDC->FrameRect(rect,&brush1);
  168.             pDC->FrameRect(offRect,&brush1);            
  169.         }
  170.         else
  171.         {
  172.             CBrush brush(m_BkColor);
  173.             pDC->FillRect(rect,&brush);
  174.             CBrush brush1(RGB(128,128,128));
  175.             pDC->FrameRect(offRect,&brush1);
  176.         }
  177.     }
  178.     
  179.     
  180.     CString sTitle;
  181.     GetWindowText(sTitle);
  182.     offRect.OffsetRect(0,rect.Height()/2-8);
  183.     //  pDC->DrawText(sTitle, sTitle.GetLength(),offRect, DT_WORDBREAK | DT_CENTER);
  184.     pDC->DrawText(sTitle, sTitle.GetLength(),offRect, DT_WORDBREAK|DT_CENTER);
  185. }
  186. void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  187. {
  188.     // TODO: Add your code to draw the specified item  
  189.     DrawItem1(lpDrawItemStruct);
  190.     return;
  191.     
  192.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  193.     CRect rect = lpDrawItemStruct->rcItem;
  194.     CRect offRect = rect;
  195.     int ndist = 2;
  196.     offRect.left+= ndist;
  197.     offRect.right -=ndist;
  198.     offRect.top += ndist;
  199.     offRect.bottom -=ndist;
  200.     CDC* pResetDC=new CDC();
  201.     pResetDC->CreateCompatibleDC(pDC);
  202.     CBitmap bitmap;
  203.     bitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
  204.     pResetDC->SelectObject(&bitmap);
  205.     
  206.     pResetDC->SetBkMode(TRANSPARENT);
  207.     pResetDC->SetTextColor(m_TextColor);
  208.     
  209.     //绘制背景
  210.     if(m_pBitMapBK!=NULL)
  211.     {
  212.         CDC* pNewDC = new CDC;
  213.         pNewDC->CreateCompatibleDC(pResetDC);
  214.         pNewDC->SelectObject(m_pBitMapBK);
  215.         BITMAP   bmp;
  216.         m_pBitMapBK->GetBitmap(&bmp);
  217.         if(m_IsPressed)
  218.         {
  219.             pResetDC->StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
  220.         }
  221.         else
  222.         {
  223.             CBrush brush(RGB(220,220,220));//RGB(m_nShOffset,m_nShOffset,m_nShOffset));
  224.             pResetDC->FillRect(&rect,&brush);
  225.             pResetDC->StretchBlt(0,0,rect.Width(),rect.Height(),pNewDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCAND);
  226.         }
  227.         
  228.         delete pNewDC;
  229.         pNewDC = NULL;
  230.     }
  231.     else
  232.     {
  233.         if(m_IsPressed)
  234.         {
  235.             CBrush brush(OffsetColor(m_BkColor,m_nShOffset));
  236.             pResetDC->FillRect(rect,&brush);                
  237.             CBrush brush1(RGB(128,128,128));
  238.             pResetDC->FrameRect(rect,&brush1);
  239.             pResetDC->FrameRect(offRect,&brush1);
  240.             
  241.         }
  242.         else
  243.         {
  244.             CBrush brush(m_BkColor);
  245.             pResetDC->FillRect(rect,&brush);
  246.             CBrush brush1(RGB(128,128,128));
  247.             pResetDC->FrameRect(offRect,&brush1);
  248.         }
  249.     }
  250.     
  251.     
  252.     CString sTitle;
  253.     GetWindowText(sTitle);
  254.     offRect.OffsetRect(0,rect.Height()/2-8);
  255. //  pDC->DrawText(sTitle, sTitle.GetLength(),offRect, DT_WORDBREAK | DT_CENTER);
  256.     pResetDC->DrawText(sTitle, sTitle.GetLength(),offRect, DT_CENTER);
  257.     pDC->StretchBlt(0,0,rect.Width(),rect.Height(),pResetDC,0,0,rect.Width(),rect.Height(),SRCCOPY);
  258.     delete pResetDC;
  259. }
  260. void CButtonST::OnMouseMove(UINT nFlags, CPoint point) 
  261. {
  262.     // TODO: Add your message handler code here and/or call default
  263.     TRACKMOUSEEVENT     csTME;
  264.     csTME.cbSize = sizeof(csTME);
  265.     csTME.dwFlags = TME_LEAVE;
  266.     csTME.hwndTrack = m_hWnd;
  267.     ::_TrackMouseEvent(&csTME); 
  268.     if(m_IsPressed==FALSE)
  269.     {
  270.         m_IsPressed = TRUE;     
  271.         this->Invalidate();
  272.     }
  273.     
  274.     CButton::OnMouseMove(nFlags, point);
  275. }
  276. BOOL CButtonST::OnNcActivate(BOOL bActive) 
  277. {
  278.     // TODO: Add your message handler code here and/or call default
  279.     return CButton::OnNcActivate(bActive);
  280. }
  281. void CButtonST::OnNcMouseMove(UINT nHitTest, CPoint point) 
  282. {
  283.     // TODO: Add your message handler code here and/or call default
  284.     
  285.     
  286.     CButton::OnNcMouseMove(nHitTest, point);
  287. }
  288. void CButtonST::OnKillFocus(CWnd* pNewWnd) 
  289. {
  290.     CButton::OnKillFocus(pNewWnd);  
  291.     // TODO: Add your message handler code here
  292. }
  293. LRESULT CButtonST::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  294. {
  295.     m_IsPressed = FALSE;
  296.     this->Invalidate();
  297.     return 0;
  298. // End of OnMouseLeave
  299. BOOL CButtonST::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  300. {
  301.     // TODO: Add your specialized code here and/or call the base class  
  302.     dwStyle |= BS_OWNERDRAW;
  303.     return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  304. }
  305. void CButtonST::OnLButtonDown(UINT nFlags, CPoint point) 
  306. {
  307.     // TODO: Add your message handler code here and/or call default
  308.     if(m_strURL.IsEmpty()==FALSE)
  309.     {
  310.         SHELLEXECUTEINFO    csSEI;
  311.         
  312.         memset(&csSEI, 0, sizeof(csSEI));
  313.         csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
  314.         csSEI.fMask = SEE_MASK_FLAG_NO_UI;
  315.         csSEI.lpVerb = _T("open");
  316.         csSEI.lpFile = m_strURL;
  317.         csSEI.nShow = SW_SHOWMAXIMIZED;
  318.         ::ShellExecuteEx(&csSEI);
  319.     }
  320.     CButton::OnLButtonDown(nFlags, point);
  321. }
  322. BOOL CButtonST::PreCreateWindow(CREATESTRUCT& cs) 
  323. {
  324.     // TODO: Add your specialized code here and/or call the base class
  325.     
  326.     return CButton::PreCreateWindow(cs);
  327. }
  328. void CButtonST::PreSubclassWindow() 
  329. {
  330.     // TODO: Add your specialized code here and/or call the base class
  331.     ModifyStyle(BS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);   
  332.     CButton::PreSubclassWindow();
  333. }
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值