你好,我是 WTL 的新手,我不明白如何在 ListView Ctrl 上接收消息
Hello, i am new in WTL, i dont understand how to reseive messages on CListViewCtrl
我创建了 .rc 文件,其中放置了我的 DialogBar。在此 DialogBar 的部分中,我创建 CListViewCtrl.And 填充它。
然后我需要在 SelectedItem 上接收消息。
请给我一些代码或链接以了解我的问题。谢谢你。
这是我的 DialogBar 代码,我在其中调用创建 CListViewCtrl
的函数
#include <thread>
#include "resource.h"
#include "resource2.h"
#include "MyListView.h"
class MyDialogBar:public CDialogImpl<MyDialogBar>
{
public:
enum { IDD = IDD_MYDIALOGBAR };
BEGIN_MSG_MAP(MyDialogBar)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_COMMAND,OnCommand)
MESSAGE_HANDLER(WM_NOTIFY,OnLButtonDown)
MESSAGE_HANDLER(WM_CLOSE,OnCloseCmd)
END_MSG_MAP()
LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CString parameter = TEXT("C:\Users\Огурчик\Desktop\");
switch (wParam)
{
case IDC_BUTTON_EXIT:
OnCloseCmd(uMsg,wParam,lParam,bHandled);
return 0;
case IDC_BUTTON_APPLY:
ListView_DeleteAllItems(this->myListView.m_hWnd);
if (FindThread.joinable())
{
FindThread.detach();
FindThread = std::thread((&MyListView::FindFile),this->myListView, parameter);
}
else
{
FindThread = std::thread((&MyListView::FindFile), this->myListView, parameter);
}
return 0;
case IDC_SEARCH_TEXT_BAR :
GetDlgItemText(IDC_SEARCH_TEXT_BAR, FileName);
return 0;
case IDC_EXTENTION_TEXT_BAR:
GetDlgItemText(IDC_EXTENTION_TEXT_BAR, FileExtention);
return 0;
}
return 0;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (LOWORD(wParam))
{
case (int)LVN_ITEMCHANGED:
MessageBox(TEXT("U am here"), TEXT("Here"), NULL);
}
return 0;
}
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
myListView.Create(m_hWnd);
return 0;
}
LRESULT OnCloseCmd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if (FindThread.joinable())
{
FindThread.detach();
}
EndDialog(NULL);
return 0;
}
private:
CString FileName;
CString FileExtention;
MyListView myListView;
std::thread FindThread;
};
//code of CListViewCtrl
#pragma once
#include <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
#include <atlwin.h>
#include <atlctrls.h>
#include <atlfile.h>
#include <atlstr.h>
class MyListView:public CListViewCtrl
{
private:
LVITEM lvItem;
CListViewCtrl myListView;
CString path;
int i;
public:
void Create(HWND m_hWnd);
void FindFile(CString szPath);
void View_List(CString buf, int i,CString path);
BOOL InitListViewImage(int size, CString path);
};
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (LOWORD(wParam))
{
case (int)LVN_ITEMCHANGED:
MessageBox(TEXT("U am here"), TEXT("Here"), NULL);
}
return 0;
}
这看起来不对。
WM_NOTIFY
处理程序应使用 lParam
并将其转换为 NMHDR
结构,而不应使用 wParam
。见 docs:
wParam
The identifier of the common control sending the message. This
identifier is not guaranteed to be unique. An application should use
the hwndFrom or idFrom member of the NMHDR structure (passed as the
lParam parameter) to identify the control.
lParam
A pointer to an NMHDR structure that contains the notification code
and additional information. For some notification messages, this
parameter points to a larger structure that has the NMHDR structure as
its first member.
您最好使用 NOTIFY_HANDLER
宏而不是 MESSAGE_HANDLER
宏。
WTL 在 <atlcrack.h>
header 中有更好的消息破解程序,你需要 NOTIFY_HANDLER_EX
从那里。
我创建了 .rc 文件,其中放置了我的 DialogBar。在此 DialogBar 的部分中,我创建 CListViewCtrl.And 填充它。 然后我需要在 SelectedItem 上接收消息。 请给我一些代码或链接以了解我的问题。谢谢你。 这是我的 DialogBar 代码,我在其中调用创建 CListViewCtrl
的函数
#include <thread>
#include "resource.h"
#include "resource2.h"
#include "MyListView.h"
class MyDialogBar:public CDialogImpl<MyDialogBar>
{
public:
enum { IDD = IDD_MYDIALOGBAR };
BEGIN_MSG_MAP(MyDialogBar)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_COMMAND,OnCommand)
MESSAGE_HANDLER(WM_NOTIFY,OnLButtonDown)
MESSAGE_HANDLER(WM_CLOSE,OnCloseCmd)
END_MSG_MAP()
LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
CString parameter = TEXT("C:\Users\Огурчик\Desktop\");
switch (wParam)
{
case IDC_BUTTON_EXIT:
OnCloseCmd(uMsg,wParam,lParam,bHandled);
return 0;
case IDC_BUTTON_APPLY:
ListView_DeleteAllItems(this->myListView.m_hWnd);
if (FindThread.joinable())
{
FindThread.detach();
FindThread = std::thread((&MyListView::FindFile),this->myListView, parameter);
}
else
{
FindThread = std::thread((&MyListView::FindFile), this->myListView, parameter);
}
return 0;
case IDC_SEARCH_TEXT_BAR :
GetDlgItemText(IDC_SEARCH_TEXT_BAR, FileName);
return 0;
case IDC_EXTENTION_TEXT_BAR:
GetDlgItemText(IDC_EXTENTION_TEXT_BAR, FileExtention);
return 0;
}
return 0;
}
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (LOWORD(wParam))
{
case (int)LVN_ITEMCHANGED:
MessageBox(TEXT("U am here"), TEXT("Here"), NULL);
}
return 0;
}
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
myListView.Create(m_hWnd);
return 0;
}
LRESULT OnCloseCmd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if (FindThread.joinable())
{
FindThread.detach();
}
EndDialog(NULL);
return 0;
}
private:
CString FileName;
CString FileExtention;
MyListView myListView;
std::thread FindThread;
};
//code of CListViewCtrl
#pragma once
#include <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
#include <atlwin.h>
#include <atlctrls.h>
#include <atlfile.h>
#include <atlstr.h>
class MyListView:public CListViewCtrl
{
private:
LVITEM lvItem;
CListViewCtrl myListView;
CString path;
int i;
public:
void Create(HWND m_hWnd);
void FindFile(CString szPath);
void View_List(CString buf, int i,CString path);
BOOL InitListViewImage(int size, CString path);
};
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (LOWORD(wParam))
{
case (int)LVN_ITEMCHANGED:
MessageBox(TEXT("U am here"), TEXT("Here"), NULL);
}
return 0;
}
这看起来不对。
WM_NOTIFY
处理程序应使用 lParam
并将其转换为 NMHDR
结构,而不应使用 wParam
。见 docs:
wParam
The identifier of the common control sending the message. This identifier is not guaranteed to be unique. An application should use the hwndFrom or idFrom member of the NMHDR structure (passed as the lParam parameter) to identify the control.
lParam
A pointer to an NMHDR structure that contains the notification code and additional information. For some notification messages, this parameter points to a larger structure that has the NMHDR structure as its first member.
您最好使用 NOTIFY_HANDLER
宏而不是 MESSAGE_HANDLER
宏。
WTL 在 <atlcrack.h>
header 中有更好的消息破解程序,你需要 NOTIFY_HANDLER_EX
从那里。