在對(duì)話框類的頭文件里創(chuàng)建所要改變屬性的控件的對(duì)象,如要改變一個(gè)Button(其ID為IDC_MyButton)的屬性,則需創(chuàng)建Cbutton的對(duì)象m_button。然后在.cpp中的DoDataExchange函數(shù)里將Button的ID和創(chuàng)建的對(duì)象綁定在一起:
//{{AFX_DATA_MAP(CPrintDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_MyButton, m_button);
//}}AFX_DATA_MAP
然后可以在該函數(shù)的最后進(jìn)行初始化:
m_button.EnableWindow(FALSE);
到這里已經(jīng)實(shí)現(xiàn)了改變屬性。如果要?jiǎng)討B(tài)改變其屬性,可如下實(shí)現(xiàn)(通過兩個(gè)Button的點(diǎn)擊改變起屬性):
// RadioAll Button的點(diǎn)擊響應(yīng)函數(shù)
void CPrintDlg::OnRadioAll()
{
// TODO: Add your control notification handler code here
m_button.EnableWindow(TRUE);
}
// RadioSelect Button的點(diǎn)擊響應(yīng)函數(shù)
void CPrintDlg::OnRadioSelect()
{
// TODO: Add your control notification handler code here
m_button.EnableWindow(FALSE);
}
也可以通過一個(gè)Check Button的點(diǎn)擊來改變,在其點(diǎn)擊響應(yīng)函數(shù)里實(shí)現(xiàn):
m_button.EnableWindow(!m_button.IsWindowEnabled());
其余控件屬性的改變都如此。
運(yùn)行AppWizard生成一個(gè)基于對(duì)話框的test工程,在對(duì)話框中加入一個(gè)CButton控件。在CButton控件的General屬性頁將控件的ID改為IDC_3DTEXTBTN,Caption改為“誰與爭瘋”,在控件Styles屬性頁選中OwnerDraw,其余設(shè)置保持默認(rèn)。
用classwizard創(chuàng)建一個(gè)新類:C3dTextButton,基類為CButton。為C3dTextButton類添加一個(gè)protected的函數(shù)void Draw(CDC* pDC, const CRect& rect, UINT state)。如下所示編寫代碼:
void C3dTextButton::Draw(CDC *pDC, const CRect &rect, UINT state)
{
CString text; GetWindowText(text);
int l=text.GetLength();
CRect rectClient=rect;
//獲得控件的字體
CFont* pFont=GetFont();
//確定所選字體有效高度和寬度
LOGFONT logfont;
pFont->GetObject(sizeof(LOGFONT),&logfont);
if(logfont.lfHeight==0)logfont.lfHeight=20;
logfont.lfWidth=0;//寬度設(shè)為0,寬度值由高度確定
logfont.lfWeight=1000;
logfont.lfEscapement=logfont.lfOrientation=0;
CFont tryfont; VERIFY(tryfont.CreateFontIndirect(&logfont));
CFont* pFontOld=pDC->SelectObject(&tryfont);
//根據(jù)控件大小,調(diào)整字體的高度,使文本與控件協(xié)調(diào)
CSize textSizeClient=pDC->GetTextExtent(text,l);
if(rectClient.Width()*textSizeClient.cy>rectClient.Height()*textSizeClient.cx)
{
logfont.lfHeight=::MulDiv(logfont.lfHeight,rectClient.Height(),textSizeClient.cy);
}
else{
logfont.lfHeight = ::MulDiv(logfont.lfHeight,rectClient.Width(),textSizeClient.cx);
}
//創(chuàng)建并選擇協(xié)調(diào)后的字體
CFont font; font.CreateFontIndirect(&logfont);
pDC->SelectObject(&font);
textSizeClient=pDC->GetTextExtent(text,l);
//確定文本與控件邊界的距離minx,miny
int minx=rectClient.left+(rectClient.Width()-textSizeClient.cx)/2;
int miny=rectClient.top+(rectClient.Height()-textSizeClient.cy)/2;
int oldBkMode=pDC->SetBkMode(TRANSPARENT);
COLORREF textcol=::GetSysColor(COLOR_BTNTEXT);
COLORREF oldTextColor=pDC->SetTextColor(textcol);
int cx = minx;
int cy = miny;
int s=(state&ODS_SELECTED)?-1:+1;
cx+= 3; cy+= 3;
//實(shí)現(xiàn)3D效果
pDC->SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
pDC->TextOut(cx-s*2,cy+s*2,text);
pDC->TextOut(cx+s*2,cy-s*2,text);
pDC->TextOut(cx+s*2,cy+s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->TextOut(cx+s*1,cy-s*2,text);
pDC->TextOut(cx-s*2,cy+s*1,text);
pDC->TextOut(cx-s*2,cy-s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->TextOut(cx-s*1,cy+s*1,text);
pDC->TextOut(cx+s*1,cy-s*1,text);
pDC->TextOut(cx+s*1,cy+s*1,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DLIGHT));
pDC->TextOut(cx,cy-s*1,text);
pDC->TextOut(cx-s*1,cy,text);
pDC->TextOut(cx-s*1,cy-s*1,text);
pDC->SetTextColor(textcol);
//輸出標(biāo)題
pDC->TextOut(cx,cy,text);
//恢復(fù)設(shè)備描述表
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode(oldBkMode);
pDC->SelectObject(pFontOld);
}
用classwizard重載C3dTextButton類的DrawItem函數(shù)。編寫代碼如下所示:
void C3dTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT_VALID(pDC);
CRect rectClient=lpDrawItemStruct->rcItem;
Draw(pDC,rectClient,lpDrawItemStruct->itemState);
}
用classwizard為IDC_3DTEXTBTN建立一個(gè)C3dTextButton控件變量m_3dTextButton1。
把“3dTextButton.h”加入testDlg頭文件。編譯并測試應(yīng)用程序。
1、 在OnInitialDialog中用CButton::CheckRadioButton(...)
2、 在OnInitialDialog中用CButton::SetCheck(...)
關(guān)聯(lián)一個(gè)整型值,在構(gòu)造函數(shù)中設(shè)為0。