代码改变世界

Visual C++ 2011-4-8

2011-04-11 09:08  Clingingboy  阅读(696)  评论(0编辑  收藏  举报

 

一.DDX_Control && UpdateData

将控件Id和实例关联起来,方便直接取得对象,与GetDlgItem函数取对象相同

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDemoDlg)
    
    DDX_Control(pDX, IDC_LIST, m_ctrlList);
    CWnd *dlg=this->GetDlgItem(IDC_LIST);
    //}}AFX_DATA_MAP
}

除了DDX_Control之外,还有其他DDX_xxx的函数,比如绑定到CheckBox和BOOL变量,调用UpdateData方法则会更新数据

二.MAKEINTRESOURCE

资源名转换的宏,即将数字转换为字符串

参考:http://baike.baidu.com/view/2899077.htm

三.ExpandEnvironmentStrings

获取操作系统环境变量

#include <windows.h>
#include <stdio.h>
void main(void)
{
    putenv("Program Files=c:\\Program   Files");
    putenv("MyPath=Outlook Express");
    
    //char *var1="%Program Files%\\%MyPath%\\msimn.exe";
    char *var1 = "%SystemDrive%";
    char buf1[1024];
    
    ExpandEnvironmentStrings(var1,buf1,sizeof(buf1));
    MessageBoxA(NULL, buf1, "ExpandStrings1: SystemDrive", MB_OK);
    
    //char *var2="%temp%\\adsdasd";
    char *var2 = "%WinDir%";
    char buf2[1024];
    
    ExpandEnvironmentStrings(var2,buf2,sizeof(buf2));
    MessageBoxA(NULL, buf2, "ExpandStrings2: WinDir", MB_OK);
}

四.CImageList

Provides the functionality of the Windows common image list control.

即为MFC控件提供一个图片的列表.
比如给CComboBoxEx和CListCtrl等控件使用,调用相关控件的SetImageList方法

 CImageList *m_pImageListSmall = new CImageList();
    m_pImageListSmall->Create(16, 16, TRUE, 4, 4);
    m_pImageList->Add(pApp->LoadIcon(IDI_ICONLIST1));
    m_pImageList->Add(pApp->LoadIcon(IDI_ICONLIST2));
    m_pImageList->Add(pApp->LoadIcon(IDI_ICONLIST3));
    m_pImageList->Add(pApp->LoadIcon(IDI_ICONLIST4));
CComboBoxEx *m_wndCBEx= (CComboBoxEx*)this->GetDlgItem(IDC_COMBOBOXEX1);
 m_wndCBEx->SetImageList (m_pImageListSmall);
for (int i=0; i<5; i++) {
    CString string;
    string.Format (_T ("Item %d"), i);
    COMBOBOXEXITEM cbei;
    cbei.mask = CBEIF_IMAGE| CBEIF_SELECTEDIMAGE | CBEIF_TEXT;
    cbei.iItem = i;
    cbei.pszText = (LPTSTR) (LPCTSTR) string;
    cbei.iImage = 1;
    cbei.iSelectedImage = 1;
    cbei.iIndent = (i & 0x03); 
    m_wndCBEx->InsertItem (&cbei);
}

五.GetStockObject

取得一个固有对象(即操作系统预定的对象,无法删除),不要用DeleteObject去删除GetStockObject取出的对象

六.CPropertySheet && CPropertyPage

属性Tab选项卡,CPropertyPage继承自CDialog,所以也是对话框

class CFirstPage : public CPropertyPage
{
public:
    CFirstPage () : CPropertyPage (IDD_FIRSTPAGE) {};
    // Declare CFirstPage's data members here.
protected:
    virtual void DoDataExchange (CDataExchange*);
};
class CSecondPage : public CPropertyPage
{
public:
    CSecondPage () : CPropertyPage (IDD_SECONDPAGE) {};
    // Declare CSecondPage's data members here.
protected:
    virtual void DoDataExchange (CDataExchange*);
};
class CMyPropertySheet : public CPropertySheet
{
public:
    CFirstPage m_firstPage;         // First page
    CSecondPage m_secondPage;       // Second page
    // Constructor adds the pages automatically.
    CMyPropertySheet (LPCTSTR pszCaption, 
        CWnd* pParentWnd = NULL) :
        CPropertySheet (pszCaption, pParentWnd, 0)
    {
        AddPage (&m_firstPage);
        AddPage (&m_secondPage);
    }
};

先有个概念了解下
参考:Visual Studio的PropDlg例子
http://www.vckbase.com/document/viewdoc/?id=398
http://www.vckbase.com/document/viewdoc/?id=330
http://blog.csdn.net/lixiaosan/archive/2006/04/13/661458.aspx

七.SetIcon两次的疑惑

http://zhidao.baidu.com/question/176982744.html