MFC工程对话框中button背景和文字颜色变色?

~我爱中国~ 2012-08-27 01:52:28
在用c++做MFC,有一个对话框,对话框中有一个button控件,点击该控件后弹出一个子对话框,子对话框中有OK和Cancel键,点击OK键可使button键背景色变绿,button上的文字变黑色,点击Cancel键可使button键背景变红,button上的文字变蓝色。 该怎么实现啊~ 急很,请高手指点!!
看到别的帖子,说是button属性要设置为自绘,但设置后,运行程序后对话框中的button按键就看不到了,好像隐藏了,但点击却可以出现子对话框。这个方法好像不行,到底该怎么实现呢??? 求解答!!
...全文
428 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
~我爱中国~ 2012-10-09
  • 打赏
  • 举报
回复
各位,不好意思,这段太忙,结贴有点慢,感谢各位。
qlizhang 2012-08-27
  • 打赏
  • 举报
回复
动态加载CBitmapButton的四副状态图
http://www.vckbase.com/index.php/cv/640
CrazyAsk 2012-08-27
  • 打赏
  • 举报
回复
very good
ok1234567 2012-08-27
  • 打赏
  • 举报
回复
在网上搜索一个CColorButton 之类的类,就可以了

http://www.codeguru.com/
http://www.codeproject.com/
上很多实现好的子类
在CSDN的下载频道,也有不少资源
个性的界面资源,靠积累...
Gloveing 2012-08-27
  • 打赏
  • 举报
回复
点击OK和Cancel键就改变一个全局BOOL变量的值
在WM_CTLCOLORBTN消息处理中去绘制其背景和颜色
淡定的飘着 2012-08-27
  • 打赏
  • 举报
回复
楼上正解。。。。。。
Eleven 2012-08-27
  • 打赏
  • 举报
回复
改变Button的颜色必须要自绘,重写CButton类,添加处理虚函数DrawItem,MSDN上有例子代码。

// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"),
// WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
// CRect(10,10,100,30), pParentWnd, 1);
//

// This example implements the DrawItem method for a CButton-derived
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = DFCS_BUTTONPUSH;

// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;

// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);

// Get the button's text.
CString strText;
GetWindowText(strText);

// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

注意看看前面的注释

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧