这是一个用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;
}