MFC的知识网络上其实不多,想要全面一点的更是困难,下面是对于控件修改的一些些总结,欢迎指正。
别忘了:将控件的OWner Draw 改为 true
用到的消息函数:
<span style="font-size:24px;"> CMy2015102203Dlg::OnInitDialog()
和
void CMy2015102203Dlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)</span>
1.OnInitDialog作用:设置字形/大小
CFont * f;
f = new CFont;
·····
GetDlgItem(IDC_BUTTON1)->SetFont(f);
2.OnDrawItem作用:设置内容和颜色
::SetTextColor()
::DrawText()
下面是具体的代码:
BOOL CMy2015102203Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
//设置字体大小和字形
CFont * f;
f = new CFont;
f->CreateFont(36, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight
TRUE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFac
// :: SetTextColor(HDC hDC,RGB(255,255,0)); //设置字体颜色
GetDlgItem(IDC_BUTTON1)->SetFont(f);
``````````````````````````````````````````````````````````
}
void CMy2015102203Dlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT Style =DFCS_BUTTONPUSH; //普通控件
if (lpDrawItemStruct->CtlID==IDC_BUTTON1 ) //选择控件的ID
{
ASSERT(lpDrawItemStruct->CtlType==ODT_BUTTON); //指定是按钮控件
COLORREF dsd=::SetTextColor(lpDrawItemStruct->hDC,RGB(255,0,0));/[按钮中的颜色]
if(lpDrawItemStruct->itemState&ODS_SELECTED) //指定当按钮按下时候的情况
Style |=DFCS_PUSHED ;
::DrawFrameControl(lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,DFC_BUTTON,Style); //画出控件
CString dsd2=_T("happybirthday!"); //设置其中的文字
// GetWindowText(dsd2);
::DrawText(lpDrawItemStruct->hDC,dsd2,dsd2.GetLength(),&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER); //画出其中的文字
}
else
{
ASSERT(lpDrawItemStruct->CtlType==ODT_BUTTON);
COLORREF dsd=::SetTextColor(lpDrawItemStruct->hDC,RGB(255,0,0));
::DrawFrameControl(lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,DFC_BUTTON,DFCS_BUTTONPUSH|DFCS_PUSHED);
CString dsd2=_T("happybirthdaytoyou!");
// GetWindowText(dsd2);
::DrawText(lpDrawItemStruct->hDC,dsd2,dsd2.GetLength(),&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
}