所需头文件 #include<atlddx.h>
1.控件类 2.函数映射及实现 3.声明对象 4.绑定 5.绑定
控件类 函数声明及实现
class CMyEdit :public CWindowImpl<CMyEdit, CEdit>
{
BEGIN_MSG_MAP_EX(CMyEdit)
MSG_WM_CONTEXTMENU(OnContextMenu) //右键单击
END_MSG_MAP()
void OnContextMenu(HWND, CPoint pt)
{
MessageBox(_T("Menu done!"));
}
};
声明对象
CMyEdit m_Myedit;
绑定
BEGIN_DDX_MAP(CMainDlg)
DDX_CONTROL(IDC_EDIT2,m_Myedit)
END_DDX_MAP()
DoDataExchange(FALSE);
全部代码
// MainDlg.h : interface of the CMainDlg class
//
/
#pragma once
template <LPCTSTR res>
class CMyButton :public CWindowImpl<CMyButton<res>,CButton>
{
public:
BEGIN_MSG_MAP_EX(CMyButton)
MSG_WM_SETCURSOR(OnSetCursor)
END_MSG_MAP()
LRESULT OnSetCursor(HWND hWnd, UINT uHitTest, UINT uMsg)
{
static HCURSOR hcur = LoadCursor(NULL, res);
if (hcur != NULL)
{
SetCursor(hcur);
return TRUE;
}
else
{
SetMsgHandled(FALSE);
return FALSE;
}
}
};
class CMyEdit :public CWindowImpl<CMyEdit, CEdit>
{
BEGIN_MSG_MAP_EX(CMyEdit)
MSG_WM_CONTEXTMENU(OnContextMenu) //右键单击
END_MSG_MAP()
void OnContextMenu(HWND, CPoint pt)
{
MessageBox(_T("Menu done!"));
}
};
class CMainDlg : public CDialogImpl<CMainDlg>,
public CWinDataExchange<CMainDlg> //ddx所需
{
public:
enum { IDD = IDD_MAINDLG };
CMainDlg() :
m_btnOK(this, 1), m_btnCancel(this, 2) {}
BEGIN_DDX_MAP(CMainDlg)
DDX_CONTROL(IDC_EDIT2,m_Myedit)
END_DDX_MAP()
BEGIN_MSG_MAP_EX(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
ALT_MSG_MAP(1) //case 1 m_btnOK(1)
MSG_WM_SETCURSOR(OnSetCursorOk) //捕获鼠标光标
ALT_MSG_MAP(2) //case2 m_btnCancel(2)
MSG_WM_SETCURSOR(OnSetCursorCancel)
END_MSG_MAP()
// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
// set icons
HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
SetIcon(hIcon, TRUE);
HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
SetIcon(hIconSmall, FALSE);
HWND hWnd = GetDlgItem(IDC_LIST1);
m_list.Attach(hWnd); //绑定
hWnd = GetDlgItem(IDC_EDIT1);
m_edit.Attach(hWnd);
m_edit.SetWindowText(_T("My Edit!!!"));
m_btnOK.SubclassWindow(GetDlgItem(IDOK));
m_btnCancel.SubclassWindow(GetDlgItem(IDCANCEL));
m_btnAbout.SubclassWindow(GetDlgItem(ID_APP_ABOUT));
DoDataExchange(FALSE); //m_MyEdit 绑定
return TRUE;
}
LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CSimpleDialog<IDD_ABOUTBOX, FALSE> dlg;
dlg.DoModal();
return 0;
}
LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: Add validation code
EndDialog(wID);
return 0;
}
LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
EndDialog(wID);
return 0;
}
LRESULT OnSetCursorOk(HWND hWnd, UINT uHitTest, UINT uMsg)
{
static HCURSOR hcur = LoadCursor(NULL, IDC_HAND);
if (hcur != NULL)
{
SetCursor(hcur);
return TRUE;
}
else
{
SetMsgHandled(FALSE);
return FALSE;
}
}
LRESULT OnSetCursorCancel(HWND hWnd, UINT uHitTest, UINT uMsg)
{
static HCURSOR hcur = LoadCursor(NULL, IDC_NO);
if (hcur != NULL)
{
SetCursor(hcur);
return TRUE;
}
else
{
SetMsgHandled(FALSE);
return FALSE;
}
}
protected:
CListViewCtrl m_list;
CEdit m_edit;
CContainedWindow m_btnOK; //btn按钮
CContainedWindow m_btnCancel;
CMyButton<IDC_NO> m_btnAbout;
CMyEdit m_Myedit;
};