MFC控件

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);
    }
}

你可能感兴趣的:(MFC控件)