自繪實現半透明水晶按鈕
執行效果
實現方法
1.給按鈕加上BS_OWNERDRAW樣式
2.過載DrawItem函式,在這裡繪製按鈕
3.關鍵之處就是把父視窗的背景複製到按鈕上,實現視覺上的透明
4.最後通過AlphaBlend實現半透明.
實現原始碼
// MyButton.h
#pragma once
// CMyButton
class CMyButton : public CButton
{
DECLARE_DYNAMIC(CMyButton)
public:
CMyButton();
virtual ~CMyButton();
public:
void SetBkColor(COLORREF color);
void SetTextColor(COLORREF color);
private:
bool m_bOver;
bool m_bDown;
bool m_bDisable;
bool m_bTracking;
COLORREF m_bkColor;
COLORREF m_textColor;
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void PreSubclassWindow();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
afx_msg void OnEnable(BOOL bEnable);
private:
void ButtonInit();
void DrawButton();
void DrawButton(HDC hDestDC);
};
// MyButton.cpp : 實現檔案
//
#include "stdafx.h"
#include "AlphaButton.h"
#include "MyButton.h"
#include "MainDlg.h"
// CMyButton
IMPLEMENT_DYNAMIC(CMyButton, CButton)
CMyButton::CMyButton()
{
m_bkColor=0xFFFFFF;
m_textColor=0x000000;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_WM_ENABLE()
END_MESSAGE_MAP()
// CMyButton 訊息處理程式
void CMyButton::SetBkColor(COLORREF color)
{
m_bkColor=color;
}
void CMyButton::SetTextColor(COLORREF color)
{
m_textColor=color;
}
BOOL CMyButton::PreCreateWindow(CREATESTRUCT& cs)
{
BOOL bRet=CButton::PreCreateWindow(cs);
ButtonInit();
return bRet;
}
void CMyButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ButtonInit();
}
void CMyButton::ButtonInit()
{
m_bTracking=false;
m_bOver=m_bDown=m_bDisable=false;
m_bDisable=IsWindowEnabled()?FALSE:TRUE;
ModifyStyle(NULL,BS_OWNERDRAW);
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
DrawButton(lpDrawItemStruct->hDC);
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bTracking)
{
m_bOver = TRUE;
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 50;
m_bTracking = (bool)_TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bDown=TRUE;
CButton::OnLButtonDown(nFlags, point);
}
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bDown=FALSE;
CButton::OnLButtonUp(nFlags, point);
}
LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bOver = FALSE;
m_bTracking = FALSE;
m_bDown=FALSE;
InvalidateRect(NULL, FALSE);
return 0;
}
LRESULT CMyButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
m_bOver = TRUE;
InvalidateRect(NULL);
return 0;
}
void CMyButton::DrawButton()
{
HDC hDC=::GetDC(m_hWnd);
DrawButton(hDC);
::ReleaseDC(m_hWnd,hDC);
}
void CMyButton::DrawButton(HDC hDestDC)
{
CRect rc;
GetClientRect(rc);
int nWindth=rc.Width();
int nHeight=rc.Height();
HDC hDC=CreateCompatibleDC(hDestDC);//建立相容DC,採用雙緩衝畫出
HDC hMaskDC=CreateCompatibleDC(hDestDC);
HBITMAP hBitmap=CreateCompatibleBitmap(hDestDC,nWindth,nHeight);
HBITMAP hMaskBitmap=CreateCompatibleBitmap(hDestDC,nWindth,nHeight);
HBITMAP hOldBitmap=(HBITMAP)SelectObject(hDC,hBitmap);
HBITMAP hOldMaskBitmap=(HBITMAP)SelectObject(hMaskDC,hMaskBitmap);
SetBkMode(hDC,TRANSPARENT);
//把父視窗的背景圖複製到按鈕的DC上,實現視覺透明----------------
CMainDlg* pParent=(CMainDlg*)GetParent();
CPoint pt(0,0);
MapWindowPoints(pParent,&pt,1);
pParent->m_bkImage.BitBlt(hDC,rc,pt,SRCCOPY);
//-------------------------------------------------------------
int nAlpha=100;//0--255
int nOffset=0;
HBRUSH hbr=CreateSolidBrush(m_bkColor);
FillRect(hMaskDC,&rc,hbr);
DeleteObject(hbr);
if(m_bDisable){
nAlpha=100;
}else if(m_bDown){
nAlpha=180;
nOffset=1;
}else if(m_bOver){
nAlpha=150;
}else{
nAlpha=100;
}
BLENDFUNCTION blend;
memset( &blend, 0, sizeof( blend) );
blend.BlendOp= AC_SRC_OVER;
blend.SourceConstantAlpha= nAlpha; // 透明度 最大255
HRGN hRgn=CreateRoundRectRgn(0,0,nWindth,nHeight,3,3);
SelectClipRgn (hDC,hRgn);
AlphaBlend (hDC,0,0,nWindth,nHeight,hMaskDC, 0,0,nWindth,nHeight,blend);
CString strText;
GetWindowText(strText);
if(strText!=_T("")){
rc.InflateRect(-2,-2);
rc.OffsetRect(nOffset,nOffset);
HFONT hFont=(HFONT)SendMessage(WM_GETFONT);
if(!hFont)hFont=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
HFONT hOldFont=(HFONT)SelectObject(hDC,hFont);
::SetTextColor(hDC,m_textColor);
::DrawText(hDC,strText,-1,&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER|DT_WORD_ELLIPSIS);
::SelectObject(hDC,hOldFont);
}
SelectClipRgn (hDC,NULL);
DeleteObject(hRgn);
//複製到控制元件的DC上------------------------
BitBlt(hDestDC,0,0,nWindth,nHeight,hDC,0,0,SRCCOPY);
//刪除資源,釋放記憶體-----------------------
SelectObject(hDC,hOldBitmap);
DeleteObject(hBitmap);
DeleteDC(hDC);
SelectObject(hMaskDC,hOldMaskBitmap);
DeleteObject(hMaskBitmap);
DeleteDC(hMaskDC);
}
void CMyButton::OnEnable(BOOL bEnable)
{
CButton::OnEnable(bEnable);
m_bDisable=IsWindowEnabled()?FALSE:TRUE;
DrawButton();
}
原始碼下載:http://download.csdn.net/detail/cometnet/4955726
相關文章:自繪按鈕實現顏色選擇器
相關文章
- JavaFx 實現按鈕防抖Java
- Simple WPF: WPF 實現按鈕的長按,短按功能
- 實現前端點選按鈕自動複製剪貼簿功能前端
- ReactiveCocoa 實現 按鈕倒數計時React
- LayoutTransiton實現簡單的錄製按鈕
- VUE動態路由和按鈕的實現Vue路由
- 利用css變數實現按鈕懸浮效果CSS變數
- CocosCreator遊戲開發(五)實現技能按鈕遊戲開發
- CustomPainter——微信拍視訊按鈕效果實現AI
- jQuery入門(七)jQuery實現按鈕分頁jQuery
- 水晶藍蓮花(Matlab實現)Matlab
- WebGL半透明物體的繪製Web
- 短視訊系統,長按側滑實現刪除的按鈕
- unity 實現輪盤方式的按鈕滾動效果Unity
- [譯] 用 Flutter 實現 Facebook 的響應式按鈕Flutter
- 基於js實現點選按鈕回到頂部JS
- Js Jquery 實現的按鈕倒數計時整理JSjQuery
- Vue實現浮動按鈕元件 - 頁面滾動時自動隱藏 - 可拖拽Vue元件
- Fiori Elements List Report table 裡的普通按鈕,Global 按鈕 和 Determining 按鈕
- 點選按鈕,實現檔案下載,通過按鈕傳送url,spring後臺實現伺服器端檔案下載。Spring伺服器
- 專案需要實現按鈕懸浮的功能, 實現後的記錄
- 使用SVG實現的一個Android播放/暫停按鈕SVGAndroid
- HTML 單選按鈕實現 (性別選擇)(解讀)HTML
- Vue 專案實現按鈕級別許可權管理Vue
- 用CSS Houdini實現一個Material風格的按鈕CSS
- Jquery實現的Switch開關按鈕(仿iOS開關)jQueryiOS
- 高亮按鈕
- 【Oculus Interaction SDK】(六)實體按鈕 && 按壓互動
- CSS 實現超過固定高度後出現展開摺疊按鈕CSS
- Qt實現一個支援QSS的Switch Button(開關按鈕)QT
- Flutter Button(按鈕)Flutter
- 小程式按鈕
- 復飛按鈕
- 使用自定義 View 繪製一個懸浮式可拖拽按鈕View
- iOS 11開發教程(二十一)iOS11應用檢視美化按鈕之實現按鈕的響應(1)iOS
- 基於VUE自定義指令實現按鈕級許可權控制Vue
- FairyGui--實現點選按鈕使UI欄開啟和收起AIGUI
- Vue2-利用自定義指令實現按鈕許可權控制Vue
- php短視訊原始碼,按鈕的圓角圖示實現PHP原始碼