//定义彩
const COLORREF CLOUDBLUE = RGB(128, 184, 223);
const COLORREF WHITE = RGB(255, 255, 255);
const COLORREF BLACK = RGB(1, 1, 1);
const COLORREF DKGRAY = RGB(128, 128, 128);
const COLORREF LTGRAY = RGB(192, 192, 192);
const COLORREF YELLOW = RGB(255, 255, 0);
const COLORREF DKYELLOW = RGB(128, 128, 0);
const COLORREF RED = RGB(255, 0, 0);
const COLORREF DKRED = RGB(128, 0, 0);
const COLORREF BLUE = RGB(0, 0, 255);
const COLORREF DKBLUE = RGB(0, 0, 128);
const COLORREF CYAN = RGB(0, 255, 255);
const COLORREF DKCYAN = RGB(0, 128, 128);
const COLORREF GREEN = RGB(0, 255, 0);
const COLORREF CLOUDBLUE = RGB(128, 184, 223);
const COLORREF WHITE = RGB(255, 255, 255);
const COLORREF BLACK = RGB(1, 1, 1);
const COLORREF DKGRAY = RGB(128, 128, 128);
const COLORREF LTGRAY = RGB(192, 192, 192);
const COLORREF YELLOW = RGB(255, 255, 0);
const COLORREF DKYELLOW = RGB(128, 128, 0);
const COLORREF RED = RGB(255, 0, 0);
const COLORREF DKRED = RGB(128, 0, 0);
const COLORREF BLUE = RGB(0, 0, 255);
const COLORREF DKBLUE = RGB(0, 0, 128);
const COLORREF CYAN = RGB(0, 255, 255);
const COLORREF DKCYAN = RGB(0, 128, 128);
const COLORREF GREEN = RGB(0, 255, 0);
const COLORREF DKGREEN = RGB(0, 128, 0);
const COLORREF MAGENTA = RGB(255, 0, 255);
const COLORREF DKMAGENTA = RGB(128, 0, 128);
//在.h文件定义彩按钮
CColorButton m_btnUp;
//在.cpp文件调用函数着
VERIFY(m_btnUp.Attach(IDC_BUTTON1, this, RED, WHITE, DKRED));
//CColorButton 类原型
//colorbtn.h
#ifndef __COLORBTN_H__
#define __COLORBTN_H__
class CColorButton : public CButton
const COLORREF MAGENTA = RGB(255, 0, 255);
const COLORREF DKMAGENTA = RGB(128, 0, 128);
//在.h文件定义彩按钮
CColorButton m_btnUp;
//在.cpp文件调用函数着
VERIFY(m_btnUp.Attach(IDC_BUTTON1, this, RED, WHITE, DKRED));
//CColorButton 类原型
//colorbtn.h
#ifndef __COLORBTN_H__
#define __COLORBTN_H__
class CColorButton : public CButton
{
DECLARE_DYNAMIC(CColorButton)
public:
CColorButton();
virtual ~CColorButton();
BOOL Attach(const UINT nID, CWnd* pParent,
const COLORREF BGColor = RGB(192, 192, 192), // gray button
const COLORREF FGColor = RGB(1, 1, 1), // black text
const COLORREF DisabledColor = RGB(128, 128, 128), // dark gray disabled text
const UINT nBevel = 2
);
protected:
DECLARE_DYNAMIC(CColorButton)
public:
CColorButton();
virtual ~CColorButton();
BOOL Attach(const UINT nID, CWnd* pParent,
const COLORREF BGColor = RGB(192, 192, 192), // gray button
const COLORREF FGColor = RGB(1, 1, 1), // black text
const COLORREF DisabledColor = RGB(128, 128, 128), // dark gray disabled text
const UINT nBevel = 2
);
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
void DrawFrame(CDC *DC, CRect R, int Inset);
void DrawFilledRect(CDC *DC, CRect R, COLORREF color);
void DrawLine(CDC *DC, CRect EndPoints, COLORREF color);
void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color);
void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor);
COLORREF GetFGColor() { return m_fg; }
COLORREF GetBGColor() { return m_bg; }
COLORREF GetDisabledColor() { return m_disabled; }
UINT GetBevel() { return m_bevel; }
private:
COLORREF m_fg, m_bg, m_disabled;
void DrawFrame(CDC *DC, CRect R, int Inset);
void DrawFilledRect(CDC *DC, CRect R, COLORREF color);
void DrawLine(CDC *DC, CRect EndPoints, COLORREF color);
void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color);
void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor);
COLORREF GetFGColor() { return m_fg; }
COLORREF GetBGColor() { return m_bg; }
COLORREF GetDisabledColor() { return m_disabled; }
UINT GetBevel() { return m_bevel; }
private:
COLORREF m_fg, m_bg, m_disabled;
UINT m_bevel;
};
#endif
//colorbtn.cpp
#include "stdafx.h "
#include "colorbtn.h "
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
};
#endif
//colorbtn.cpp
#include "stdafx.h "
#include "colorbtn.h "
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#ifdef CColorButton
#undef CColorButton CColorButton
#endif
IMPLEMENT_DYNAMIC(CColorButton, CButton)
CColorButton::CColorButton()
{
#if (_MFC_VER < 0x0250)
hwndOwner = NULL;
#endif
}
CColorButton::~CColorButton()
{
}
BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel)
{
if (!SubclassDlgItem(nID, pParent))
return FALSE;
m_fg = FGColor;
m_bg = BGColor;
m_disabled = DisabledColor;
m_bevel = nBevel;
return TRUE;
}
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS-> hDC);
UINT state = lpDIS-> itemState;
CRect focusRect, btnRect;
focusRect.CopyRect(&lpDIS-> rcItem);
btnRect.CopyRect(&lpDIS-> rcItem);
m_bevel = nBevel;
return TRUE;
}
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS-> hDC);
UINT state = lpDIS-> itemState;
CRect focusRect, btnRect;
focusRect.CopyRect(&lpDIS-> rcItem);
btnRect.CopyRect(&lpDIS-> rcItem);
focusRect.left += 4;
focusRect.right -= 4;
p += 4;
focusRect.bottom -= 4;
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, GetBevel());
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
if (state & ODS_FOCUS) {
DrawFocusRect(lpDIS-> hDC, (LPRECT)&focusRect);
if (state & ODS_SELECTED){
DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, -1);
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
DrawFocusRect(lpDIS-> hDC, (LPRECT)&focusRect);
}
}
else if (state & ODS_DISABLED) {
DrawButtonText(pDC, btnRect, buffer, GetDisabledColor());
}
}
void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
{
DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, -1);
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
DrawFocusRect(lpDIS-> hDC, (LPRECT)&focusRect);
}
}
else if (state & ODS_DISABLED) {
DrawButtonText(pDC, btnRect, buffer, GetDisabledColor());
}
}
void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
{
COLORREF dark, light, tlColor, brColor;
int i, m, width;
width = (Inset < 0)? -Inset : Inset;
for (i = 0; i < width; i += 1) {
m = 255 / (i + 2);
dark = PALETTERGB(m, m, m);
m = 192 + (63 / (i + 1));
light = PALETTERGB(m, m, m);
if ( width == 1 ) {
light = RGB(255, 255, 255);
dark = RGB(128, 128, 128);
}
int i, m, width;
width = (Inset < 0)? -Inset : Inset;
for (i = 0; i < width; i += 1) {
m = 255 / (i + 2);
dark = PALETTERGB(m, m, m);
m = 192 + (63 / (i + 1));
light = PALETTERGB(m, m, m);
if ( width == 1 ) {
light = RGB(255, 255, 255);
dark = RGB(128, 128, 128);
}
if ( Inset < 0 ) {
tlColor = dark;
brColor = light;
}
else {
tlColor = light;
brColor = dark;
}
DrawLine(DC, R.left, R.top, R.right, R.top, tlColor); // Across top
DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor); // Down left
if ( (Inset < 0) && (i == width - 1) && (width > 1) ) {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom
tlColor = dark;
brColor = light;
}
else {
tlColor = light;
brColor = dark;
}
DrawLine(DC, R.left, R.top, R.right, R.top, tlColor); // Across top
DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor); // Down left
if ( (Inset < 0) && (i == width - 1) && (width > 1) ) {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom
DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1)); // Down right
}
else {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor); // Across bottom
DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor); // Down right
}
InflateRect(R, -1, -1);
}
}
void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
}
else {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor); // Across bottom
DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor); // Down right
}
InflateRect(R, -1, -1);
}
}
void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
CBrush B;
B.CreateSolidBrush(color);
DC-> FillRect(R, &B);
}
void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC-> SelectObject(&newPen);
DC-> MoveTo(EndPoints.left, p);
DC-> LineTo(EndPoints.right, EndPoints.bottom);
DC-> SelectObject(oldPen);
newPen.DeleteObject();
B.CreateSolidBrush(color);
DC-> FillRect(R, &B);
}
void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC-> SelectObject(&newPen);
DC-> MoveTo(EndPoints.left, p);
DC-> LineTo(EndPoints.right, EndPoints.bottom);
DC-> SelectObject(oldPen);
newPen.DeleteObject();
}
void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC-> SelectObject(&newPen);
DC-> MoveTo(left, top);
DC-> LineTo(right, bottom);
DC-> SelectObject(oldPen);
newPen.DeleteObject();
}
void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC-> SelectObject(&newPen);
DC-> MoveTo(left, top);
DC-> LineTo(right, bottom);
DC-> SelectObject(oldPen);
newPen.DeleteObject();
}
void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor)
{
COLORREF prevColor = DC-> SetTextColor(TextColor);
DC-> SetBkMode(TRANSPARENT);
DC-> DrawText(Buf, strlen(Buf), R, DT_CENTER and DT_VCENTER and DT_SINGLELINE);
DC-> SetTextColor(prevColor);
}
{
COLORREF prevColor = DC-> SetTextColor(TextColor);
DC-> SetBkMode(TRANSPARENT);
DC-> DrawText(Buf, strlen(Buf), R, DT_CENTER and DT_VCENTER and DT_SINGLELINE);
DC-> SetTextColor(prevColor);
}
能够实现的代码:
要想修改CButton类按钮背景颜和文字颜,必须利用自绘方法对按钮进行重新绘制。这可以通过定义一个以CButton为基类的新按钮类来实现。以下为具体的实现方法:
加入一个新类,类名:CMyButton,基类:CButton。
在头文件 MyButton.h 中加入以下变量和函数定义:
private:
int m_Style; //按钮形状(0-正常,1-当前,2-按下,3-锁定)
BOOL b_InRect; //鼠标进入标志
CString m_strText; //按钮文字
COLORREF m_ForeColor; //文本颜
COLORREF m_BackColor; //背景
COLORREF m_LockForeColor; //锁定按钮的文字颜
CRect m_ButRect; //按钮尺寸
CFont* p_Font; //字体
int m_Style; //按钮形状(0-正常,1-当前,2-按下,3-锁定)
BOOL b_InRect; //鼠标进入标志
CString m_strText; //按钮文字
COLORREF m_ForeColor; //文本颜
COLORREF m_BackColor; //背景
COLORREF m_LockForeColor; //锁定按钮的文字颜
CRect m_ButRect; //按钮尺寸
CFont* p_Font; //字体
void DrawButton(CDC *pDC); //画正常的按钮
// 接口函数
public:
void SetText(CString str);
void SetForeColor(COLORREF color); //设置文本颜
public:
void SetText(CString str);
void SetForeColor(COLORREF color); //设置文本颜
void SetBkColor(COLORREF color); //设置背景颜
void SetTextFont(int FontHight,LPCTSTR FontName); //设置字体
void SetTextFont(int FontHight,LPCTSTR FontName); //设置字体
在 MyButton.cpp 的构造函数中初始化变量:
CMyButton::CMyButton()
{
m_Style = 0; //按钮形状风格
b_InRect = false; //鼠标进入标志
m_strText = _T(""); //按钮文字(使用默认文字)
m_ForeColor = RGB(0,0,0); //文字颜(黑)
m_BackColor = RGB(243,243,243); //背景(灰白)
m_LockForeColor = GetSysColor(COLOR_GRAYTEXT); //锁定按钮的文字颜
p_Font = NULL; //字体指针
}
{
m_Style = 0; //按钮形状风格
b_InRect = false; //鼠标进入标志
m_strText = _T(""); //按钮文字(使用默认文字)
m_ForeColor = RGB(0,0,0); //文字颜(黑)
m_BackColor = RGB(243,243,243); //背景(灰白)
m_LockForeColor = GetSysColor(COLOR_GRAYTEXT); //锁定按钮的文字颜
p_Font = NULL; //字体指针
}
用ClassWizard添加下列消息函数:
PreSubclassWindow();
DrawItem();
onMouseMove();
OnLButtonDown();
OnLButtonUp();
DrawItem();
onMouseMove();
OnLButtonDown();
OnLButtonUp();
在各函数内加入代码:
void CMyButton::PreSubclassWindow()
{
ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自画式
CButton::PreSubclassWindow();
}
{
ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自画式
CButton::PreSubclassWindow();
}
PreSubclassWindow()在按钮创建前自动执行,所以我们可以在其中做一些初始工作。这里我只做了一项工作,就是为按钮设置属性为“自绘”式,这样,用户在添加按钮后,就不需
设置“Owner draw”属性了。
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
m_ButRect = lpDrawItemStruct->rcItem; //获取按钮尺寸
{
CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
m_ButRect = lpDrawItemStruct->rcItem; //获取按钮尺寸
if( m_strText.IsEmpty() )
GetWindowText( m_strText ); //获取按钮文本
GetWindowText( m_strText ); //获取按钮文本
改变button按钮的形状 int nSavedDC = pDC->SaveDC();
VERIFY( pDC );
DrawButton( pDC ); //绘制按钮
pDC->RestoreDC( nSavedDC );
}
VERIFY( pDC );
DrawButton( pDC ); //绘制按钮
pDC->RestoreDC( nSavedDC );
}
DrawItem()函数是一个关键函数,按钮的绘制工作就在这里进行,它的作用相当于对话框中的OnPaint()函数和视图中的OnDraw()函数。
这里我做了三项工作:获取按钮尺寸、获取按钮文本、绘制按钮。其中绘制工作在自定义函数DrawButton()中完成。以下就是绘制过程:
void CMyButton::DrawButton(CDC *pDC)
{
//调整状态
if( m_Style==3 ) m_Style = 0;
if( GetStyle() & WS_DISABLED )
m_Style = 3; //禁止状态
//根据状态调整边框颜和文字颜
COLORREF bColor, fColor; //bColor为边框颜,fColor为文字颜
switch( m_Style )
{
case 0: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //正常按钮
case 1: bColor = RGB(255,255,255); fColor = m_ForeColor; break; //鼠标进入时按钮
case 2: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //按下的按钮
{
//调整状态
if( m_Style==3 ) m_Style = 0;
if( GetStyle() & WS_DISABLED )
m_Style = 3; //禁止状态
//根据状态调整边框颜和文字颜
COLORREF bColor, fColor; //bColor为边框颜,fColor为文字颜
switch( m_Style )
{
case 0: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //正常按钮
case 1: bColor = RGB(255,255,255); fColor = m_ForeColor; break; //鼠标进入时按钮
case 2: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //按下的按钮
case 3: bColor = m_BackColor; fColor = m_LockForeColor; break; //锁定的按钮
}
//绘制按钮背景
CBrush Brush;
Brush.CreateSolidBrush( m_BackColor ); //背景刷
pDC->SelectObject( &Brush );
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, bColor );
pDC->SelectObject( &Pen );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形
//绘制按钮按下时的边框
if( m_Style!=2 )
{
CRect Rect;
Rect.SetRect( m_ButRect.left+2, p+1, m_ButRect.right, m_ButRect.bot
}
//绘制按钮背景
CBrush Brush;
Brush.CreateSolidBrush( m_BackColor ); //背景刷
pDC->SelectObject( &Brush );
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, bColor );
pDC->SelectObject( &Pen );
pDC->RoundRect(&m_ButRect,CPoint(5,5)); //画圆角矩形
//绘制按钮按下时的边框
if( m_Style!=2 )
{
CRect Rect;
Rect.SetRect( m_ButRect.left+2, p+1, m_ButRect.right, m_ButRect.bot
tom );
pDC->DrawEdge( &Rect, BDR_RAISEDINNER, BF_RECT ); //画边框
}
//绘制按钮文字
pDC->SetTextColor( fColor ); //画文字
pDC->SetBkMode( TRANSPARENT );
pDC->DrawText( m_strText, &m_ButRect, DT_SINGLELINE | DT_CENTER
| DT_VCENTER | DT_END_ELLIPSIS);
//绘制拥有焦点按钮的虚线框
if( GetFocus()==this )
{
CRect Rect;
Rect.SetRect( m_ButRect.left+3, p+2, m_ButRect.right-3, m_ButRect.bottom-2 );
pDC->DrawFocusRect( &Rect ); //画拥有焦点的虚线框
pDC->DrawEdge( &Rect, BDR_RAISEDINNER, BF_RECT ); //画边框
}
//绘制按钮文字
pDC->SetTextColor( fColor ); //画文字
pDC->SetBkMode( TRANSPARENT );
pDC->DrawText( m_strText, &m_ButRect, DT_SINGLELINE | DT_CENTER
| DT_VCENTER | DT_END_ELLIPSIS);
//绘制拥有焦点按钮的虚线框
if( GetFocus()==this )
{
CRect Rect;
Rect.SetRect( m_ButRect.left+3, p+2, m_ButRect.right-3, m_ButRect.bottom-2 );
pDC->DrawFocusRect( &Rect ); //画拥有焦点的虚线框
}
}
}
变量 m_Style 表征当前按钮状态,它的取值为:0-正常,1-当前,2-按下,3-锁定。不同状态下按钮的边框颜和文字颜有所不同。m_Style 的值在鼠标响应函数中进行修改。
绘制工作主要利用CDC类的绘图函数完成,主要注意在 m_Style 不同取值下表现出来的差别。
void CMyButton::onMouseMove(UINT nFlags, CPoint point)
{
if( !b_InRect || GetCapture()!=this ) //鼠标进入按钮
{
b_InRect = true; //设置进入标志
SetCapture(); //捕获鼠标
m_Style = 1; //设置按钮状态
Invalidate(); //重绘按钮
{
if( !b_InRect || GetCapture()!=this ) //鼠标进入按钮
{
b_InRect = true; //设置进入标志
SetCapture(); //捕获鼠标
m_Style = 1; //设置按钮状态
Invalidate(); //重绘按钮
}
else
{
if ( !m_ButRect.PtInRect(point) ) //鼠标离开按钮
{
b_InRect = false; //清除进入标志
ReleaseCapture(); //释放捕获的鼠标
m_Style = 0; //设置按钮状态
Invalidate(); //重绘按钮
}
}
CButton::onMouseMove(nFlags, point);
}
else
{
if ( !m_ButRect.PtInRect(point) ) //鼠标离开按钮
{
b_InRect = false; //清除进入标志
ReleaseCapture(); //释放捕获的鼠标
m_Style = 0; //设置按钮状态
Invalidate(); //重绘按钮
}
}
CButton::onMouseMove(nFlags, point);
}
onMouseMove()函数是鼠标移动消息函数,用于判定当前鼠标指针是否在按钮上。b_InRe
ct是个标志,为true表示鼠标指针进入了按钮区域,此时要捕获鼠标,让鼠标命令传送给按钮。当鼠标指针离开按钮时,要清除b_InRect标志,并且释放捕获的鼠标,让其它窗口可以接收鼠标命令。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论