
1、把MFC類與控件關聯起來
1)使用GetDlgItem函數
CWnd* CWnd::GetDlgItem(int nID) const;
void* CWnd::GetDlgItem(int nID,HWND* phWnd) const;
void CDialogDlg::OnGetdlgitem()
{
CComboBox* pCbo=(CComboBox*)GetDlgItem(IDC_COMBO1);
//獲取某個空間的指針
ASSERT(pCbo);
if(pCbo)
{
pCbo->AddString("GetDlgItem-1");
pCbo->SetCurSel(0);
}
}
2)通過Attach函數為控件定義子類
使用此函數要注意在添加代碼以去掉關聯
void CDialogDlg::OnUsingattach()
{
if(NULL==m_combo2.GetSafeHwnd())
//檢查控件有沒關聯到子類
{
HWND hwnd;
GetDlgItem(IDC_COMBO2,&hwnd);
if(hwnd)
{
m_combo2.Attach(hwnd);
m_combo2.AddString("Using Attach-1");
m_combo2.AddString("Using Attach-2");
m_combo2.SetCurSel(0);
}
}
}
void CDialogDlg::OnDestroy()
{
CDialog::OnDestroy();
m_combo2.Detach();
}
3)使用對話框模板控件的DDX
添加完控件后,單擊新添加的組合框,選擇Add Member Variable菜單項。選中Member Variables,單擊Add Variables,將Variable type設為CComboBox,在Variable name框中輸入“m_combo3”
void CDialogDlg::OnButton2()
{
m_combo3.AddString("Using DDX");
m_combo3.SetCurSel(0);
}
4)動態創建控件
添加一個變量:CComBox m_combo4
void CDialogDlg::OnButton3()
{
if(NULL==m_combo4.GetSafeHwnd())
{
CComboBox* pExistingCombo=(CComboBox*)GetDlgItem(IDC_COMBO2);
CWnd* pUsingDynamic=GetDlgItem(IDC_BUTTON3);
ASSERT(pExistingCombo&&pUsingDynamic);
if(pExistingCombo&&pUsingDynamic)
{
CRect rectCombo,rectDropdown;
pExistingCombo->GetWindowRect(rectCombo);
ScreenToClient(rectCombo);
CRect rectButton;
pUsingDynamic->GetWindowRect(rectButton);
ScreenToClient(rectButton);
rectCombo.right=rectButton.left+rectCombo.Width();
rectCombo.left=rectButton.left;
pExistingCombo->ShowDropDown();
pExistingCombo->GetDroppedControlRect(rectDropdown);
//得到已存在組合框的下拉長度
pExistingCombo->ShowDropDown(FALSE);
ScreenToClient(rectDropdown);
rectCombo.bottom+=rectDropdown.Height();
DWORD dwStyle=pExistingCombo->GetStyle();
m_combo4.Create(dwStyle,rectCombo,this,1222);
m_combo4.AddString("Dynamic-1");
m_combo4.AddString("Dynamic-2");
m_combo4.SetCurSel(0);
m_combo4.SetFocus();
}
}
}
2、位圖按鈕
1)使用CBitmapButton類
在項目中添加一個CBitmapButton類的子類(稱為CMyFancyButton),并在按鈕屬性的樣式中把Owner Draw屬性設為TRUE。
BOOL CDialogDlg::OnInitDialog()
{

m_fancyButton.LoadBitmaps(IDB_UP,IDB_DOWN,IDB_FOCUSED,IDB_DISABLED);
//分別設置圖像的四個狀態
m_fancyButton.SizeToContent();//自動將按鈕的大小設置為位圖的大小

}
2)使用BS_BITMAP風格
在按鈕屬性的樣式中把Bitmap屬性設為TRUE。
BOOL CDialogDlg::OnInitDialog()
{

CButton* pBSBitmap=(CButton*)GetDlgItem(IDC_BUTTON4);
ASSERT(pBSBitmap);
if(pBSBitmap)
{
VERIFY(m_bmpBSBitmap.LoadBitmap(IDB_UP));
HBITMAP hbmp=(HBITMAP)m_bmpBSBitmap.GetSafeHandle();
pBSBitmap->SetBitmap(hbmp);
}

}
3、修改控件運行時的屬性
1)改變控件的顏色
添加WM_CTLCOLOR事件
HBRUSH CDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetTextColor(RGB(255,255,255));
pDC->SetBkColor(RGB(255,0,0));
return hbr;
}
4)設置控件的字體
首先聲明字體CFont m_font,再定義字體,最后循環遍歷控件并對其進行設置字體
int CDialogDlg::PointSizeToHeight(int iPointSize,HDC hDC)
{
int iRetVal=0;
ASSERT(hDC!=NULL);
iRetVal=-(MulDiv(iPointSize,::GetDeviceCaps(hDC,LOGPIXELSY),72));
return iRetVal;
}
BOOL CDialogDlg::OnInitDialog()
{
LOGFONT lf;
ZeroMemory(&lf,sizeof(LOGFONT));
CClientDC clientDC(this);
lf.lfHeight=PointSizeToHeight(18,clientDC.m_hDC);
lf.lfWeight=FW_BOLD;
strcpy(lf.lfFaceName,"MS Sans Serif");
VERIFY(m_font.CreateFontIndirect(&lf));
}
void CDialogDlg::OnButton6()
{
CWnd* pChildWnd=NULL;
pChildWnd=GetTopWindow();
while (NULL!=pChildWnd)
{
pChildWnd->SetFont(&m_font);
pChildWnd=pChildWnd->GetNextWindow(GW_HWNDNEXT);
}
}
posted on 2009-07-26 10:38
The_Moment 閱讀(505)
評論(0) 編輯 收藏 引用 所屬分類:
VC實踐