社区
界面
帖子详情
请教:在OnDrawItem()外如何绘制按钮?
tangyao13711927
2009-11-16 02:29:36
如题,请教达人指点,如何能够不通过OnDrawItem()函数实现按钮绘制?谢谢
...全文
143
4
打赏
收藏
请教:在OnDrawItem()外如何绘制按钮?
如题,请教达人指点,如何能够不通过OnDrawItem()函数实现按钮绘制?谢谢
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
juckxu
2009-11-16
打赏
举报
回复
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; //字体指针
}
CMyButton::~CMyButton()
{
if ( p_Font ) delete p_Font; //删除字体
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
END_MESSAGE_MAP()
// CMyButton 消息处理程序
void CMyButton::PreSubclassWindow(void)
{
ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自画式
CButton::PreSubclassWindow();
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
m_ButRect = lpDrawItemStruct->rcItem; //获取按钮尺寸
if( m_strText.IsEmpty() )
GetWindowText( m_strText ); //获取按钮文本
int nSavedDC = pDC->SaveDC();
VERIFY( pDC );
DrawButton( pDC ); //绘制按钮
pDC->RestoreDC( nSavedDC );
}
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; //按下的按钮
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+1, m_ButRect.top, 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 )
{
CRect Rect;
Rect.SetRect( m_ButRect.left+3, m_ButRect.top+2, m_ButRect.right-3, m_ButRect.bottom-2 );
pDC->DrawFocusRect( &Rect ); //画拥有焦点的虚线框
}
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
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);
}
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_Style = 2;
Invalidate(); //重绘按钮
CButton::OnLButtonDown(nFlags, point);
}
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_Style = 1;
Invalidate(); //重绘按钮
CButton::OnLButtonUp(nFlags, point);
}
void CMyButton::SetText(CString str)
{
m_strText = _T("");
SetWindowText(str);
}
void CMyButton::SetForeColor(COLORREF color)
{
m_ForeColor = color;
Invalidate();
}
void CMyButton::SetBkColor(COLORREF color)
{
m_BackColor = color;
Invalidate();
}
void CMyButton::SetTextFont(int FontHight,LPCTSTR FontName)
{
if ( p_Font ) delete p_Font; //删除旧字体
p_Font = new CFont;
p_Font->CreatePointFont( FontHight, FontName ); //创建新字体
SetFont( p_Font ); //设置字体
}
tangyao13711927
2009-11-16
打赏
举报
回复
知道是在OnPaint里完成,要具体的方法
oyljerry
2009-11-16
打赏
举报
回复
OnPaint,OnDraw
wfx_net
2009-11-16
打赏
举报
回复
不通过OnDrawItem那就捕获OnPaint绘制。
VC小知识
(1) 如何通过代码获得应用程序主窗口的 指针?
主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//使程序最大化.
(2) 确定应用程序的路径
Use GetModuleFileName 获得应用程序的路径,然后去掉可执行文件名。
Example:
TC
vc开发工作小结
一隐藏任务条上的图标第一、创建对话框时必须将它作为某个不可见框架窗口的子窗口;第二、这个不可见框架窗口的扩展式样必须设置 WS_EX_TOOLWINDOW;第三、保证对话框的扩展式样没有设置 WS_EX_APPWINDOW; 先在CWinApp继承类中建立一个不显示的顶级窗口. CFrameWnd *pFrame=new CFrameWnd();pFrame->Create(0,0,WS_OV
VC小知识(转)
(1) 如何通过代码获得应用程序主窗口的 指针?
主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//使程序最大化.
(2) 确定应用程序的路径
Use GetModuleFileName 获得应用程序的路径,然后去掉可执行文件名。
Example:
TCHAR
[Copied] 80 VC++ tips
(1) 如何通过代码获得应用程序主窗口的 指针?主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现。AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)//使程序最大化. (2) 确定应用程序的路径Use GetModuleFileName 获得应用程序的路径,然后去掉可执行文件名。Example:T...
VC小知识总结1
1)当文档被修改时,如何在标题上加上标志*?重载CDocument类的虚函数virtual SetModifiedFlag: void CTest2Doc::SetModifiedFlag(BOOL bModified){ CString strTitle = GetTitle(); CString strDirtyFlag = " *"; // note space before
界面
15,979
社区成员
115,896
社区内容
发帖
与我相关
我的任务
界面
VC/MFC 界面
复制链接
扫一扫
分享
社区描述
VC/MFC 界面
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章