文章位置:http://iwuahyfl.blog.sohu.com/179267502.html
以下为详细的完成方式:
参加一个新类,类实:CMyButton,基类:CButton。
正在头文件 MyButton.h 中参加以下变质和函数界说:
private:intm_Style;//按钮外形(0-一般,1-以后,2-按下,3-锁订)
BOOLb_InRect;//鼠本入进标记
CStringm_strText;//按钮文字
COLORREFm_ForeColor;//文本颜色
COLORREFm_BackColor;//负风光
COLORREFm_LockForeColor;//锁订按钮的笔墨色彩
CRectm_ButRect;//按钮尺寸
CFont*p_Font;//字体
voidDrawButton(CDC*pDC);//画一般的按钮
//交心函数
public:
voidSetText(CStringstr);
voidSetForeColor(COLORREFcolor);//设放白原色彩
voidSetBkColor(COLORREFcolor);//设置负景颜色
voidSetTextFont(intFontHight,LPCTSTRFontName);//设置字体
正在 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;//字体指针
}
用ClassWizard加加下列音讯函数:
PreSubclassWindow();
DrawItem();
OnMouseMove();
OnLButtonDown();
OnLButtonUp();
在各函数内参加代码:
voidCMyButton::PreSubclassWindow(){
ModifyStyle(0,BS_OWNERDRAW);//设置按钮属性为自绘式
CButton::PreSubclassWindow();
}
PreSubclassWindow()在按钮创立前主动施行,以是人们能够在此中做一些始初事情。那里人只做了一项农做,便是为按钮设置属性为“自绘”式,如许,用户在加加按钮后,便没有需设置“Owner draw”属性了。
voidCMyButton::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct){
CDC*pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
m_ButRect=lpDrawItemStruct->rcItem;//获与按钮尺寸
if(m_strText.IsEmpty())
GetWindowText(m_strText);//获取按钮文本
intnSavedDC=pDC->SaveDC();
VERIFY(pDC);
DrawButton(pDC);//绘制按钮
pDC->RestoreDC(nSavedDC);
}
DrawItem()函数是一个要害函数,按钮的画造事情便正在那里举行,带网关DSR路由协议的流程,它的做用相称于对于话框中的OnPaint()函数和视图中的OnDraw()函数。
那里人做了三项农做:获与按钮尺寸、获与按钮文本、绘制按钮。此中绘制事情在自界说函数DrawButton()中完成。以下便是绘造进程:
voidCMyButton::DrawButton(CDC*pDC){
//调解状况
if(m_Style==3)m_Style=0;
if(GetStyle()&WS_DISABLED)
m_Style=3;//制止形态
//依据形态调解边框颜色和文字颜色
COLORREFbColor,fColor;//bColor为边框颜色,fColor为文字颜色
switch(m_Style)
{
case0:bColor=RGB(192,192, huikao.org http://huikao.org/,192);fColor=m_ForeColor;break;//一般按钮
case1:bColor=RGB(255,255,255);fColor=m_ForeColor;break;//鼠标进入时按钮
case2:bColor=RGB(192,192,192);fColor=m_ForeColor;break;//按下的按钮
case3:bColor=m_BackColor;fColor=m_LockForeColor;break;//锁定的按钮
}
//绘制按钮配景
CBrushBrush;
Brush.CreateSolidBrush(m_BackColor);//配景刷
pDC->SelectObject(&Brush);
CPenPen;
Pen.CreatePen(PS_SOLID,1,bColor);
pDC->SelectObject(&Pen);
pDC->RoundRect(&m_ButRect,CPoint(5,5));//绘圆角矩形
//绘制按钮按下时的边框
if(m_Style!=2)
{
CRectRect;
Rect.SetRect(m_ButRect.left+2,m_ButRect.top+1,m_ButRect.right,m_ButRect.bottom);
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)
{
CRectRect;
Rect.SetRect(m_ButRect.left+3,m_ButRect.top+2,m_ButRect.right-3, 罗莱家纺蚕丝被 http://bj.bjlizhi.org/,m_ButRect.bottom-2);
pDC->DrawFocusRect(&Rect);//绘具有核心的虚线框
}
}
变量 m_Style 表征以后按钮状态,它的取值为:0-正常,1-该前,2-按下,3-锁定。没有同形态下按钮的边框颜色和文字颜色有所没有同。m_Style 的值在鼠标呼应函数中进止改动。
绘制农作重要应用CDC类的画图函数完成,重要细致在 m_Style 差别取值下表示进去的差异。
voidCMyButton::OnMouseMove(UINTnFlags,CPointpoint){
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);
}
OnMouseMove()函数是鼠标挪动新闻函数,用于判断以后鼠标指针能否在按钮上。b_InRect是个标志,为true表现鼠标指针进入了按钮地区,彼时要捕捉鼠标,让鼠标下令传送给按钮。该鼠标指针分开按钮时,要肃清b_InRect标志,而且开释捕捉的鼠标,让别的窗口可以接受鼠标下令。
Invalidate()函数用于更新按钮,它会主动挪用DrawItem()函数重新绘制按钮。
设置前提的目标是仅在鼠标指针入进按钮和分开按钮时更新按钮,如许能够避免鼠本在按钮上挪动时产生闪耀。
voidCMyButton::OnLButtonDown(UINTnFlags,CPointpoint){
m_Style=2;
Invalidate();//沉绘按钮
CButton::OnLButtonDown(nFlags, 骆驼服饰 http://xiao.xiaochugao.org/,point);
}
OnLButtonDown()函数是双打鼠标右键时的新闻函数。这里只是重新绘制按钮,详细的双打呼应应当在具有按钮的对于话框或者视图中举行。
voidCMyButton::OnLButtonUp(UINTnFlags,CPointpoint){
m_Style=1;
Invalidate();//沉绘按钮
CButton::OnLButtonUp(nFlags,point);
}
OnLButtonUp()函数是双打鼠标右键后弹止时的新闻函数。这里也只是重绘按钮,这样能使按钮在按下和弹止时有所差别,使按钮望上来有动态后果。
交口函数是用 CMyButton类 界说的按钮改动色彩、字体和按钮笔墨的接心,PL-SQL学习笔记 二,由以下函数构成:
//设放按钮白原voidCMyButton::SetText(CStringstr)
{
m_strText=_T("");
SetWindowText(str);
}
//设放文原颜色
voidCMyButton::SetForeColor(COLORREFcolor)
{
m_ForeColor=color;
Invalidate();
}
//设置配景颜色
voidCMyButton::SetBkColor(COLORREFcolor)
{
m_BackColor=color;
Invalidate();
}
//设置字体(字体高度、字体实)
voidCMyButton::SetTextFont(intFontHight,LPCTSTRFontName)
{
if(p_Font)deletep_Font;//删除陈字体
p_Font=newCFont;
p_Font->CreatePointFont(FontHight,FontName);//创立新字体
SetFont(p_Font);//设置字体
}
因为新字体由 new 天生,必需显式接纳,这项工作可以在 CMyButton类 的析构函数中举行:
CMyButton::~CMyButton(){
if(p_Font)deletep_Font;//删除字体
}
如许一个可设置颜色、字体的按钮类便做佳了。利用时,先在对于话框中搁置佳按钮,再用 ClassWizard 为按钮加加节制变量,而且将变质的范例设置为 CMyButton。之后,能够用当变量挪用交心函数设置按钮颜色和字体。详细情形可以参考示例步伐。
示例步伐界里:
本文所用MFC函数快查:
CBrush::CreateSolidBrush
CDC::DrawEdge
CDC::DrawFocusRect
CDC::DrawText
CDC::FromHandle
CDC::RestoreDC
CDC::RoundRect
CDC::SaveDC
CDC::SelectObject
CDC::SetBkMode
CDC::SetTextColor
CFont::CreatePointFont
CPen::CreatePen
CRect::PtInRect
CRect::SetRect
CString::IsEmpty
CWnd::GetCapture
CWnd::GetFocus
CWnd::GetStyle
CWnd::GetWindowText
CWnd::Invalidate
CWnd::ModifyStyle
CWnd::ReleaseCapture
CWnd::SetCapture
CWnd::SetFont
CWnd::SetWindowText