MFC,按钮设计?
我在一个button里面,调用了一个CColorDialog colordlg;
我想在button表面上涂上颜色,选的那个就涂哪个?谁知道如何处理?
[解决办法]
可以用CBitmapButton
它有个LoadBitmaps 方法,可以针对不同的状态设置不同的图片。比如你设置红按钮就在资源里准备个红色的位图,然后调LoadBitmaps 把位图资源id设进去。
点按钮的时候SetCheck 设置按下抬起状态。GetCheck 可以得到这个状态。
[解决办法]
对于改变按钮颜色,需要重载按钮类的虚函数DrawItem
我试过这个方法,希望对你有帮助
1、新建一个类CTestBtn(基于CButton类)
2、在CTButton类上添加虚函数,选择DrawItem,添加虚函数,添加代码如下:
void CTestBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
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);
}
3、 把你要设置颜色的按钮 关联一个CTestBtn的变量m_btn1,m_btn1当然是添加到你的对话框类中了。
在你的对话框类头文件中 添加#include "TestBtn.h"
4、将你的按钮属性改一下,Style中的Owner draw勾选上(必须有这步,选择后支持自己改颜色。否则白扯)
5、运行成功
[解决办法]
自绘DRAWITEM风格的按钮