• 自绘实现半透明水晶按钮 .


    运行效果

    实现方法

    1.给按钮加上BS_OWNERDRAW样式
    2.重载DrawItem函数,在这里绘制按钮
    3.关键之处就是把父窗口的背景复制到按钮上,实现视觉上的透明
    4.最后通过AlphaBlend实现半透明.

    实现源码

    1. // MyButton.h   
    2. #pragma once   
    3.   
    4.   
    5. // CMyButton   
    6.   
    7. class CMyButton : public CButton  
    8. {  
    9.     DECLARE_DYNAMIC(CMyButton)  
    10.   
    11. public:  
    12.     CMyButton();  
    13.     virtual ~CMyButton();  
    14. public:  
    15.     void SetBkColor(COLORREF color);  
    16.     void SetTextColor(COLORREF color);  
    17. private:  
    18.     bool m_bOver;  
    19.     bool m_bDown;  
    20.     bool m_bDisable;  
    21.     bool m_bTracking;  
    22.     COLORREF m_bkColor;  
    23.     COLORREF m_textColor;  
    24. protected:  
    25.     DECLARE_MESSAGE_MAP()  
    26.     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);  
    27.     virtual void PreSubclassWindow();  
    28. public:  
    29.     virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);  
    30.     afx_msg void OnMouseMove(UINT nFlags, CPoint point);  
    31.     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);  
    32.     afx_msg void OnLButtonUp(UINT nFlags, CPoint point);  
    33.     afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);  
    34.     afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);  
    35.     afx_msg void OnEnable(BOOL bEnable);  
    36. private:  
    37.     void ButtonInit();  
    38.     void DrawButton();  
    39.     void DrawButton(HDC hDestDC);  
    40. };  

    1. // MyButton.cpp : 实现文件   
    2. //   
    3.   
    4. #include "stdafx.h"   
    5. #include "AlphaButton.h"   
    6. #include "MyButton.h"   
    7. #include "MainDlg.h"   
    8.   
    9. // CMyButton   
    10.   
    11. IMPLEMENT_DYNAMIC(CMyButton, CButton)  
    12.   
    13. CMyButton::CMyButton()  
    14. {  
    15.     m_bkColor=0xFFFFFF;  
    16.     m_textColor=0x000000;  
    17. }  
    18.   
    19. CMyButton::~CMyButton()  
    20. {  
    21. }  
    22.   
    23.   
    24. BEGIN_MESSAGE_MAP(CMyButton, CButton)  
    25.     ON_WM_MOUSEMOVE()  
    26.     ON_WM_LBUTTONDOWN()  
    27.     ON_WM_LBUTTONUP()  
    28.     ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)  
    29.     ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)  
    30.     ON_WM_ENABLE()  
    31. END_MESSAGE_MAP()  
    32.   
    33.   
    34.   
    35. // CMyButton 消息处理程序   
    36.   
    37. void CMyButton::SetBkColor(COLORREF color)  
    38. {  
    39.     m_bkColor=color;  
    40. }  
    41. void CMyButton::SetTextColor(COLORREF color)  
    42. {  
    43.     m_textColor=color;  
    44. }  
    45.   
    46. BOOL CMyButton::PreCreateWindow(CREATESTRUCT& cs)  
    47. {  
    48.     BOOL bRet=CButton::PreCreateWindow(cs);  
    49.     ButtonInit();  
    50.     return bRet;  
    51. }  
    52.   
    53. void CMyButton::PreSubclassWindow()  
    54. {  
    55.     CButton::PreSubclassWindow();  
    56.     ButtonInit();  
    57. }  
    58. void CMyButton::ButtonInit()  
    59. {  
    60.     m_bTracking=false;  
    61.     m_bOver=m_bDown=m_bDisable=false;  
    62.     m_bDisable=IsWindowEnabled()?FALSE:TRUE;  
    63.     ModifyStyle(NULL,BS_OWNERDRAW);  
    64. }  
    65.   
    66. void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  
    67. {  
    68.   
    69.     DrawButton(lpDrawItemStruct->hDC);  
    70. }  
    71.   
    72. void CMyButton::OnMouseMove(UINT nFlags, CPoint point)  
    73. {  
    74.     if (!m_bTracking)  
    75.     {  
    76.         m_bOver = TRUE;  
    77.         TRACKMOUSEEVENT tme;  
    78.         tme.cbSize = sizeof(tme);  
    79.         tme.hwndTrack = m_hWnd;  
    80.         tme.dwFlags = TME_LEAVE | TME_HOVER;  
    81.         tme.dwHoverTime = 50;  
    82.         m_bTracking = (bool)_TrackMouseEvent(&tme);  
    83.     }  
    84.   
    85.     CButton::OnMouseMove(nFlags, point);  
    86. }  
    87.   
    88. void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)  
    89. {  
    90.     m_bDown=TRUE;  
    91.   
    92.     CButton::OnLButtonDown(nFlags, point);  
    93. }  
    94.   
    95. void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)  
    96. {  
    97.     m_bDown=FALSE;  
    98.     CButton::OnLButtonUp(nFlags, point);  
    99. }  
    100. LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)  
    101. {  
    102.     m_bOver = FALSE;  
    103.     m_bTracking = FALSE;  
    104.     m_bDown=FALSE;  
    105.     InvalidateRect(NULL, FALSE);  
    106.     return 0;  
    107. }  
    108.   
    109. LRESULT CMyButton::OnMouseHover(WPARAM wParam, LPARAM lParam)  
    110. {  
    111.     m_bOver = TRUE;  
    112.     InvalidateRect(NULL);  
    113.     return 0;  
    114. }  
    115. void CMyButton::DrawButton()  
    116. {  
    117.     HDC hDC=::GetDC(m_hWnd);  
    118.     DrawButton(hDC);  
    119.     ::ReleaseDC(m_hWnd,hDC);  
    120. }  
    121. void CMyButton::DrawButton(HDC hDestDC)  
    122. {  
    123.     CRect rc;  
    124.     GetClientRect(rc);  
    125.     int nWindth=rc.Width();  
    126.     int nHeight=rc.Height();  
    127.     HDC hDC=CreateCompatibleDC(hDestDC);//创建兼容DC,采用双缓冲画出   
    128.     HDC hMaskDC=CreateCompatibleDC(hDestDC);  
    129.     HBITMAP hBitmap=CreateCompatibleBitmap(hDestDC,nWindth,nHeight);  
    130.     HBITMAP hMaskBitmap=CreateCompatibleBitmap(hDestDC,nWindth,nHeight);  
    131.     HBITMAP hOldBitmap=(HBITMAP)SelectObject(hDC,hBitmap);  
    132.     HBITMAP hOldMaskBitmap=(HBITMAP)SelectObject(hMaskDC,hMaskBitmap);  
    133.     SetBkMode(hDC,TRANSPARENT);  
    134.   
    135.     //把父窗口的背景图复制到按钮的DC上,实现视觉透明----------------   
    136.     CMainDlg* pParent=(CMainDlg*)GetParent();  
    137.     CPoint pt(0,0);  
    138.     MapWindowPoints(pParent,&pt,1);  
    139.     pParent->m_bkImage.BitBlt(hDC,rc,pt,SRCCOPY);  
    140.   
    141.   
    142.     //-------------------------------------------------------------   
    143.     int nAlpha=100;//0--255   
    144.     int nOffset=0;  
    145.   
    146.     HBRUSH hbr=CreateSolidBrush(m_bkColor);  
    147.     FillRect(hMaskDC,&rc,hbr);  
    148.     DeleteObject(hbr);  
    149.   
    150.     if(m_bDisable){  
    151.         nAlpha=100;  
    152.     }else if(m_bDown){  
    153.         nAlpha=180;  
    154.         nOffset=1;  
    155.     }else if(m_bOver){  
    156.         nAlpha=150;  
    157.     }else{  
    158.         nAlpha=100;  
    159.     }  
    160.     BLENDFUNCTION blend;  
    161.     memset( &blend, 0, sizeof( blend) );  
    162.     blend.BlendOp= AC_SRC_OVER;  
    163.     blend.SourceConstantAlpha= nAlpha; // 透明度 最大255   
    164.   
    165.     HRGN hRgn=CreateRoundRectRgn(0,0,nWindth,nHeight,3,3);  
    166.     SelectClipRgn (hDC,hRgn);  
    167.     AlphaBlend (hDC,0,0,nWindth,nHeight,hMaskDC, 0,0,nWindth,nHeight,blend);  
    168.   
    169.     CString strText;  
    170.     GetWindowText(strText);  
    171.     if(strText!=_T("")){  
    172.         rc.InflateRect(-2,-2);  
    173.         rc.OffsetRect(nOffset,nOffset);  
    174.         HFONT hFont=(HFONT)SendMessage(WM_GETFONT);  
    175.         if(!hFont)hFont=(HFONT)GetStockObject(DEFAULT_GUI_FONT);  
    176.         HFONT hOldFont=(HFONT)SelectObject(hDC,hFont);  
    177.         ::SetTextColor(hDC,m_textColor);  
    178.         ::DrawText(hDC,strText,-1,&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER|DT_WORD_ELLIPSIS);  
    179.         ::SelectObject(hDC,hOldFont);  
    180.     }  
    181.     SelectClipRgn (hDC,NULL);  
    182.     DeleteObject(hRgn);  
    183.     //复制到控件的DC上------------------------   
    184.     BitBlt(hDestDC,0,0,nWindth,nHeight,hDC,0,0,SRCCOPY);  
    185.     //删除资源,释放内存-----------------------   
    186.     SelectObject(hDC,hOldBitmap);  
    187.     DeleteObject(hBitmap);  
    188.     DeleteDC(hDC);  
    189.     SelectObject(hMaskDC,hOldMaskBitmap);  
    190.     DeleteObject(hMaskBitmap);  
    191.     DeleteDC(hMaskDC);  
    192.   
    193. }  
    194. void CMyButton::OnEnable(BOOL bEnable)  
    195. {  
    196.     CButton::OnEnable(bEnable);  
    197.     m_bDisable=IsWindowEnabled()?FALSE:TRUE;  
    198.     DrawButton();  
    199. }  

    源码下载:http://download.csdn.net/detail/cometnet/4955726

  • 相关阅读:
    判断有向无环图(DAG)
    单向连通图 Going from u to v or from v to u? poj2762
    百度地图的实时路况 2016 计蒜之道 复赛
    快速模取幂
    fibonacci数列(二)_矩阵快速幂
    数与矩阵快速幂基本知识
    Brute-force Algorithm_矩阵快速幂&&欧拉公式*****
    Nearest number
    Zipper_DP
    Jumping Cows_贪心
  • 原文地址:https://www.cnblogs.com/jinsedemaitian/p/5589153.html
Copyright © 2020-2023  润新知