继承CWnd自绘按钮
头文件:

//头文件 #pragma once // CLhsButton #define MYWM_BTN_CLICK WM_USER+3001 //关闭按钮单击响应 //tab按钮的状态
enum ButtonState
{
BTN_STATE_NOR = 0,
BTN_STATE_DOWN = 1,
}; class CLhsButton : public CWnd
{
DECLARE_DYNAMIC(CLhsButton) public:
CLhsButton();
virtual ~CLhsButton(); bool Create(CWnd* pParent,CRect rc,CString text,DWORD id = 0,DWORD style = WS_VISIBLE|WS_CHILD); DECLARE_MESSAGE_MAP() public: protected:
CString szClassName;
bool m_isMouseHover; //鼠标是否悬浮
bool m_isMouseClicked; //鼠标是否单击
CString m_strShowText; //要显示的文字 Image* m_pImgNor; //正常时的图片
Image* m_pImgHot; //鼠标悬浮时的图片
Image* m_pImgDown; //单击按下时的图片 void PostClickEvent();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
virtual BOOL PreTranslateMessage(MSG* pMsg);
public:
void SetTabState(ButtonState state){m_btnState = state; Invalidate();} //设置tab状态
ButtonState GetTabState(){return m_btnState;}
void SetToolTipText(CString spText, BOOL bActivate = TRUE);
void LoadBtnImg(LPCTSTR tType, UINT nNorID, UINT nHotID, UINT nDownID); //加载按钮图片 private:
ButtonState m_btnState; //tab的状态
CToolTipCtrl* m_pToolTip;
CString m_tooltext; public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
}; //源文件
// CLhsButton.cpp : 实现文件
// #include "stdafx.h"
#include "../Lander_mini.h"
#include "memdc.h"
#include "../Utility.h"
#include "LhsButton.h"
// CLhsButton IMPLEMENT_DYNAMIC(CLhsButton, CWnd) CLhsButton::CLhsButton()
{
m_isMouseHover = false;
m_isMouseClicked = false;
// 注册控件类
szClassName = AfxRegisterWndClass(0);
m_pImgNor = NULL;
m_pImgHot = NULL;
m_pImgDown = NULL;
m_btnState = BTN_STATE_NOR;
m_pToolTip = NULL;
} CLhsButton::~CLhsButton()
{
SAFE_RELEASE(m_pToolTip);
SAFE_RELEASE(m_pImgNor);
SAFE_RELEASE(m_pImgHot);
SAFE_RELEASE(m_pImgDown);
} BEGIN_MESSAGE_MAP(CLhsButton, CWnd)
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER() // 此消息系统并不会给我们发送
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP() // CLhsButton 消息处理程序
bool CLhsButton::Create(CWnd* pParent,CRect rc,CString text,DWORD id /* = 0 */,DWORD style /* = WS_VISIBLE|WS_CHILD */)
{
// 动态创建控件
BOOL ret = CWnd::CreateEx(0, szClassName, text, style, rc, pParent, id);
return ret ? true : false;
} int CLhsButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{ //::SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED); //ModifyStyle(WS_CLIPCHILDREN, 0);
//ModifyStyle(0, WS_CLIPCHILDREN , 0); return 0;
} void CLhsButton::PostClickEvent()
{
// 该函数用来向父窗口发送 单击 消息
CWnd* parent = GetParent();
if(parent)
parent->SendMessage(MYWM_BTN_CLICK, 0,0);
} void CLhsButton::OnMouseHover(UINT nFlags, CPoint point)
{
// 鼠标进入
Invalidate();
} void CLhsButton::OnMouseMove(UINT nFlags, CPoint point)
{
// 只处理鼠标第一次进入时的情况
if(!m_isMouseHover)
{
m_isMouseHover = true; TRACKMOUSEEVENT evt = { sizeof(evt), TME_LEAVE|TME_HOVER, m_hWnd, 0 };
TrackMouseEvent(&evt); OnMouseHover(0,CPoint());
}
} void CLhsButton::OnMouseLeave()
{
// 鼠标离开
m_isMouseHover = false;
m_isMouseClicked = false;
Invalidate();
} void CLhsButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// 鼠标按下
m_isMouseClicked = true;
Invalidate();
} void CLhsButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// 鼠标松开
if(m_isMouseClicked)
{
m_isMouseClicked = false;
Invalidate();
PostClickEvent();
}
} BOOL CLhsButton::OnEraseBkgnd(CDC* pDC)
{
// return CWnd::OnEraseBkgnd(pDC);
return TRUE; // 阻止擦除背景,防止闪烁
} void CLhsButton::OnPaint()
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc); // 采用双缓存,防止闪烁
CMemDC memdc(&dc,&rc,TRUE);
Graphics graphic(memdc);
if (!m_pImgNor || !m_pImgHot || !m_pImgDown)
{//没有提供按钮图片就刷下背景
// 刷背景
COLORREF bkgnd = RGB(100,0,0);
if(m_isMouseHover)
{
if(m_isMouseClicked)
bkgnd = RGB(250,0,0);
else
bkgnd = RGB(180,0,0);
}
memdc.FillSolidRect(&rc,bkgnd);
} if (m_isMouseClicked || m_btnState == BTN_STATE_DOWN)
{//单击一定画单击状态
graphic.DrawImage(m_pImgDown, 0, 0, m_pImgDown->GetWidth(), m_pImgDown->GetHeight());
}
else if (m_isMouseHover && !m_isMouseClicked)
{
//悬浮,但是没单击
graphic.DrawImage(m_pImgHot, 0, 0, m_pImgHot->GetWidth(), m_pImgHot->GetHeight());
}
else
{
graphic.DrawImage(m_pImgNor, 0, 0, m_pImgNor->GetWidth(), m_pImgNor->GetHeight());
} // 使绘制生效
graphic.ReleaseHDC(memdc);
} BOOL CLhsButton::PreTranslateMessage(MSG* pMsg)
{
if (m_pToolTip)
{
if (::IsWindow(m_pToolTip->m_hWnd))
{
m_pToolTip->RelayEvent(pMsg);
}
} return CWnd::PreTranslateMessage(pMsg);
} void CLhsButton::OnSize(UINT nType, int cx, int cy)
{ } void CLhsButton::SetToolTipText(CString spText, BOOL bActivate)
{
if (m_pToolTip == NULL)
{
m_pToolTip = new CToolTipCtrl;
// Create ToolTip control
m_pToolTip->Create(this);
m_pToolTip->Activate(TRUE);
} m_tooltext = spText; // If there is no tooltip defined then add it
if (m_pToolTip->GetToolCount() == 0)
{
CRect rectBtn;
GetClientRect(rectBtn);
m_pToolTip->AddTool(this, m_tooltext, rectBtn, 1);
} // Set text for tooltip
m_pToolTip->UpdateTipText(m_tooltext, this, 1);
m_pToolTip->SetDelayTime(2000);
m_pToolTip->Activate(bActivate);
} HBRUSH CLhsButton::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: 在此更改 DC 的任何属性 // TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
} //加载按钮图片
void CLhsButton::LoadBtnImg(LPCTSTR tType, UINT nNorID, UINT nHotID, UINT nDownID)
{
m_pImgNor = CQYUtility::LoadImgFromRes(_T("PNG"), nNorID);
m_pImgHot = CQYUtility::LoadImgFromRes(_T("PNG"), nHotID);
m_pImgDown = CQYUtility::LoadImgFromRes(_T("PNG"), nDownID);
}

内存DC

<pre name="code" class="cpp">#ifndef _MEMDC_H_
#define _MEMDC_H_ //////////////////////////////////////////////////
// CMemDC - memory DC
//
class CMemDC : public CDC {
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:
void Build( CDC* pDC )
{
ASSERT(pDC != NULL); m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting(); if( m_bMemDC )
{
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect); m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap); SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
}
else
{
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
} FillSolidRect( m_rect, pDC->GetBkColor() );
} CMemDC(CDC* pDC, const CRect* pRect = NULL, BOOL bBg = FALSE) : CDC()
{
ASSERT(pDC != NULL); // Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
m_bMemDC = !pDC->IsPrinting(); // Get the rectangle to draw
if (pRect == NULL) {
pDC->GetClipBox(&m_rect);
} else {
m_rect = *pRect;
} if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect); m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap); SetMapMode(pDC->GetMapMode()); SetWindowExt(pDC->GetWindowExt());
SetViewportExt(pDC->GetViewportExt()); pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
} else {
// Make a copy of the relevent parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
} // Fill background
if( bBg )
BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
m_pDC, m_rect.left, m_rect.top, SRCCOPY);
else
FillSolidRect(m_rect, pDC->GetBkColor());
} ~CMemDC()
{
if (m_bMemDC) {
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY); //Swap back the original bitmap.
SelectObject(m_oldBitmap);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
} // Allow usage as a pointer
CMemDC* operator->()
{
return this;
} // Allow usage as a pointer
operator CMemDC*()
{
return this;
}
CMemDC( HDC hDC )
: CDC()
{
CDC::FromHandle( hDC )->GetClipBox( &m_rect );
Build( CDC::FromHandle( hDC ) );
} }; #endif

调用方法:
//使用例子
RECT rc = {0, 0, 20, 20};
m_btnClose.LoadBtnImg(_T("PNG"),IDB_PNG_TAB_CLOSE_NOR, IDB_PNG_TAB_CLOSE_HOT,IDB_PNG_TAB_CLOSE_HOT);
m_btnClose.Create(this, rc, L"",ID_BTN_MYTAB_CLOSE);
m_btnClose.SetToolTipText(_T("关闭"));
源文地址:http://blog.csdn.net/huasonl88/article/details/43226011
继承CWnd自绘按钮的更多相关文章
- win32自绘按钮,使用GDI+(三)
解决前面的问题.实现鼠标移动进入到按钮的特效. 效果是这样的 鼠标移到按钮上,改变按钮的颜色(这里用的是直接换贴在按钮上的图片) 程序运行 鼠标进入按钮 代码 #ifndef ULONG_PTR // ...
- swing重绘按钮为任意形状图案的方法
swing重绘按钮为任意形状图案的方法 摘自https://www.jb51.net/article/131290.htm 转载 更新时间:2017年12月22日 13:43:00 作者:_Th ...
- MFC自绘按钮的实现,按钮动态效果
最近项目需要实现按钮的动态效果,多方学习,现在终于能实现一些功能了. 过程如下: 第一,新建一MFC对话框应用程序. 第二,删除自带按钮,并添加两个按钮,button1,button2,ID为IDB_ ...
- win32自绘按钮,使用GDI+(二)
一.解决上一篇的两个问题. 1.按钮背景透明 方法是,在绘制按钮之前,向按钮的父窗口发生WM_CTLCOLORBTN消息.该消息返回一个画刷句柄,系统使用该画刷句柄画出按钮的背景.所以我们在处理这个消 ...
- 自绘按钮,添加Color属性(转载)
在标准的Windows程序中所有按钮均没有颜色.因此Delphi提供的所有按钮组件也均无颜色属性,有时你可能做了一个五颜六色的程序界面,而按钮颜色可能很不相称. 在此本人提供一种用自定义组件制作有颜色 ...
- win32自绘按钮,使用GDI+(一)
第一次写随笔,我本来想将win32窗口的标题栏设置成渐变色,像这样的效果 但发现找不到设置标题栏属性的api,SetWindowLong也只是增减窗口的固定的样式而已.所以想到一个思路,把标题栏去掉, ...
- WinForm中的重绘 - 按钮等控件的背景渐变色重绘
注:brush通过起止坐标来控制重绘范围及方向.比如从上到下渐变时,brush第二个Point参数是左下角坐标. private void PaintGradientBackground(Button ...
- MFC 自绘按钮 消息响应
单检测到按下消息时,发送一个消息 m_pParent->PostMessage(WM_COMMAND, IDC_BUTTON1); 然后再在消息映射里建立映射. ON_COMMAND(IDC_B ...
- 自绘实现半透明水晶按钮(继承CButton,设置BS_OWNERDRAW风格,覆盖DrawItem函数绘制按钮,把父窗口的背景复制到按钮上,实现视觉上的透明,最后通过AlphaBlend实现半透明)
运行效果 实现方法 1.给按钮加上BS_OWNERDRAW样式2.重载DrawItem函数,在这里绘制按钮3.关键之处就是把父窗口的背景复制到按钮上,实现视觉上的透明4.最后通过AlphaBlend实 ...
随机推荐
- Python基础:11.2_函数调用
我们已经接触过函数(function)的参数(arguments)传递.当时我们根据位置,传递对应的参数.这种参数传递的方式被称为函数参数的位置传递. 我们将接触更多的参数传递方式. 回忆一下位置传递 ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. 异常
异常信息如下: The Struts dispatcher cannot be found. This is usually caused by using Struts tags without t ...
- ajax读取txt文件
注意url为网络路径 <html><head><script type="text/javascript"> function loadXML ...
- POJ 1052 Plato's Blocks
Plato's Blocks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 734 Accepted: 296 De ...
- SMO要点总结
SMO要点总结: SMO使用坐标上升的方法,求解SVM的最优解.和原始坐标上升方法的不同点在于: 1. 由于SVM的限制条件 ,所以不能只使用一个坐标,改为更新两个 2. 采用 ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- matlab 矩阵
假设矩阵A=[1 3;4 2] 1.对角置零: A-diag(diag(A)) 2.求A的特征值以及特征向量: 用到eig(A)函数,此函数有五种用法,如下: 2.1 E=eig(A):求矩阵A的全部 ...
- poj 3661 Running(区间dp)
Description The cows are trying to become better athletes, so Bessie ≤ N ≤ ,) minutes. During each m ...
- 四、cocos2dx动画Animation介绍
qinning199原创,欢迎转载,转载请注明:http://www.cocos2dx.net/?p=22 一.帧动画 你可以通过一系列图片文件,像如下这样,创建一个动画: CCAnimation * ...
- bootstrap-js(4)标签页
实例 标签页(Tab)在 Bootstrap 导航元素 一章中介绍过.通过结合一些 data 属性,您可以轻松地创建一个标签页界面. 通过这个插件您可以把内容放置在标签页或者是胶囊式标签页甚至是下拉菜 ...