WTL之dragListBox

这是一个用WTL框架写的一个dragListBox小应用。

首先建立一个CListBoxImpl类

#include "stdafx.h"
class CListBoxImpl : public CWindowImpl<CListBoxImpl, CDragListBox>{
public:
	int m_startIndex;
	int m_destIndex;
public:
	BEGIN_MSG_MAP(CListBoxImpl)
	END_MSG_MAP()
	BOOL OnBeginDrag(POINT ptCursor)
	{
		BOOL 	bOutside;
		CListBoxImpl::ScreenToClient(&ptCursor);
		m_startIndex = CListBoxImpl::ItemFromPoint(ptCursor, bOutside);
		if(bOutside) 	
return FALSE;
		return TRUE;
	}

	void OnDropped(POINT ptCursor)
	{
		CString			text;
		DWORD_PTR		itemData;
		CListBoxImpl::ScreenToClient(&ptCursor);//ptCursor是屏幕坐标
		m_destIndex = CListBoxImpl::ItemFromPoint(ptCursor, bOutside);
		itemData = CListBoxImpl::GetItemData(m_startIndex);
		CListBoxImpl::GetText(m_startIndex, text);
		CListBoxImpl::DeleteString(m_startIndex);
		CListBoxImpl::InsertString(m_destIndex, text);
		CListBoxImpl::SetItemData(m_destIndex, itemData);
	}
};

在CMainDlg中,继承CDragListNotifyImpl,如下:

#pragma once
#include "CListBoxImpl.h"

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
		public CMessageFilter, public CIdleHandler ,public CDragListNotifyImpl<CMainDlg>
{
public:
	enum { IDD = IDD_MAINDLG };

	virtual BOOL PreTranslateMessage(MSG* pMsg);
	virtual BOOL OnIdle();

	CListBoxImpl	m_list;

	BEGIN_UPDATE_UI_MAP(CMainDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainDlg)
		CHAIN_MSG_MAP(CDragListNotifyImpl<CMainDlg>)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		//CHAIN_MSG_MAP(CListBoxImpl);
	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*/);
	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

	BOOL OnBeginDrag(int /*nCtlID*/, HWND /*hWndDragList*/, POINT ptCursor)
	{
		BOOL 	bOutside;
		bOutside = m_list.OnBeginDrag(ptCursor);
		return bOutside;
	}

	void OnDropped(int /*nCtlID*/, HWND /*hWndDragList*/, POINT ptCursor) 
	{
		m_list.OnDropped(ptCursor); 
		m_list.RedrawWindow();
	} 

	void CloseDialog(int nVal);
};
在MainDlg的CPP文件中,OnInitDialog方法如下修改:

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();
	m_edit.Attach(GetDlgItem(IDC_EDIT));
	m_list.SubclassWindow(GetDlgItem(IDC_LIST));
	m_list.MakeDragList();//不能忘
	for(int i = 0; i < 16 ; i ++)
	{
		CString str;
		str.Format(_T("%d"), i);
		m_list.AddString(str);
	}

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	return TRUE;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值