在对话框中怎样设置文字的颜色和大小

经验总结:
(一)改变staitc,edit,list等非botton控件的字体颜色大小,背景可以创建新类绘制,也可以添加消息响应函数OnCtlColor来实现。
(1)创建新类绘制
首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据成员。
class CMyListBox publilc CListBox
{

private:
COLORREF m_clrFore ;// foreground color
COLORREF m_clrBack; //background color
CBrush m_brush //background brush;

}
其次,在类的构造函数中,初始化数据中。
CMyListBox : : CMyListBox ()
{
//Initialize data members .
m_clrFore =RGB (255 , 255 , 0) ;//yellow text
m_clrBack=RGB (0 , 0 , 255) ;// blue background
m_brush . CreateSolidBrush(m _clrBack );
}

最后,使用ClassWizard处理反射的=WM_CTLCOLOR消息(重载函数)并指定新的绘画属性。
HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )
{
pDC->SetTextColor (m_clrFore);
pDC->SetBkColor (m_clrBack);

return (HBRUSH) m_brush.GetSafeHandle ();
}


(2)添加消息响应函数OnCtlColor:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if(pWnd->GetDlgCtrlID()==IDC_STATIC_OK)//GetDlgCtrlID()获取当前控件ID号
{
//设置文本颜色
pDC->SetTextColor(RGB(255,0,0));
//设置背景颜色
pDC->SetBkColor(RGB(0,0,255));
//设置文字的字体和大小,(CFont)m_font在OnlnitDialog中初始化
pDC->SelectObject(&m_font);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}

(二)要改变botton控件的字体颜色大小,背景只能创建新类绘制:

使用ClassWizard 创建一个CBotton 的派生类.
添加虚函数DrawItem
void CMyButton::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);

CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

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

// Draw the button text using the text color red.
CBrush B;
CRect rect;
CRect focusRect;
focusRect.CopyRect(&lpDrawItemStruct->rcItem);
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 4;
focusRect.bottom -= 4;

rect.CopyRect(&lpDrawItemStruct->rcItem);
pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
B.CreateSolidBrush(RGB(0,255,0)); //背景颜色
::FillRect(lpDrawItemStruct->hDC,&rect, (HBRUSH)B.m_hObject);
::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
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);
}

botton其实也可以这样绘
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBitmap g_BitmapBack;
g_BitmapBack.LoadBitmap(IDB_BACKBROUND);
if ( nCtlColor == CTLCOLOR_BTN )
{
pDC->SetBkMode(TRANSPARENT);
HBRUSH B = CreatePatternBrush((HBITMAP)g_BitmapBack);
return (HBRUSH) B;
}
return hbr;
}
其它控件也一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值