[WTL]控件实现(DDX)

所需头文件  #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;

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值