WM_LBUTTONDOWN not working with CListCtrl class!!
-
I am handling WM_LBUTTONDOWN message in a CListCtrl-derived class, but not getting why it's not working. When this project was in Visual C++ 6.0, it was running fine, but now that I have converted this to Visual C++ 2005, it is failing to run, although it compiles fine, and creates the DLL.
Software Developer Sanjay Khapre
This works for me - all the message/notification handlers get called:
//
// ReflectedListCtrl.h
//#pragma once
class CReflectedListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CReflectedListCtrl)public:
CReflectedListCtrl();
virtual ~CReflectedListCtrl();protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult);
};//
// ReflectedListCtrl.cpp
//#include "stdafx.h"
#include "MyApp.h"
#include "ReflectedListCtrl.h"// CReflectedListCtrl
IMPLEMENT_DYNAMIC(CReflectedListCtrl, CListCtrl)
CReflectedListCtrl::CReflectedListCtrl()
{
}CReflectedListCtrl::~CReflectedListCtrl()
{
}BEGIN_MESSAGE_MAP(CReflectedListCtrl, CListCtrl)
ON_WM_LBUTTONDOWN()
ON_NOTIFY_REFLECT(NM_CLICK, &CReflectedListCtrl::OnNMClick)
ON_NOTIFY_REFLECT(NM_RCLICK, &CReflectedListCtrl::OnNMRClick)
END_MESSAGE_MAP()// CReflectedListCtrl message handlers
void CReflectedListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CListCtrl::OnLButtonDown(nFlags, point);
}void CReflectedListCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}void CReflectedListCtrl::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}What are you doing different? If the message handlers don't get called, does the constructor ever get called? Is the control being created successfully? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
This works for me - all the message/notification handlers get called:
//
// ReflectedListCtrl.h
//#pragma once
class CReflectedListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CReflectedListCtrl)public:
CReflectedListCtrl();
virtual ~CReflectedListCtrl();protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult);
};//
// ReflectedListCtrl.cpp
//#include "stdafx.h"
#include "MyApp.h"
#include "ReflectedListCtrl.h"// CReflectedListCtrl
IMPLEMENT_DYNAMIC(CReflectedListCtrl, CListCtrl)
CReflectedListCtrl::CReflectedListCtrl()
{
}CReflectedListCtrl::~CReflectedListCtrl()
{
}BEGIN_MESSAGE_MAP(CReflectedListCtrl, CListCtrl)
ON_WM_LBUTTONDOWN()
ON_NOTIFY_REFLECT(NM_CLICK, &CReflectedListCtrl::OnNMClick)
ON_NOTIFY_REFLECT(NM_RCLICK, &CReflectedListCtrl::OnNMRClick)
END_MESSAGE_MAP()// CReflectedListCtrl message handlers
void CReflectedListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CListCtrl::OnLButtonDown(nFlags, point);
}void CReflectedListCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}void CReflectedListCtrl::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}What are you doing different? If the message handlers don't get called, does the constructor ever get called? Is the control being created successfully? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Yes Mark, Thanks a ton for the code snippet. I have the same code snippet, and as you asked, the required no. of CListCtrl-derived class objects are also getting created successfully, but still the events, WM_LBUTTONDOWN and WM_LBUTTONDBLCLK for those objects are not getting invoked in Visual C++ 2005! I am puzzled, why is this happening! Warm regards,
Software Developer Sanjay Khapre
-
Yes Mark, Thanks a ton for the code snippet. I have the same code snippet, and as you asked, the required no. of CListCtrl-derived class objects are also getting created successfully, but still the events, WM_LBUTTONDOWN and WM_LBUTTONDBLCLK for those objects are not getting invoked in Visual C++ 2005! I am puzzled, why is this happening! Warm regards,
Software Developer Sanjay Khapre
SanjaySMK wrote:
the required no. of CListCtrl-derived class objects are also getting created successfully
Are you sure? How are you verifying that? What code are you using to create your custom control class? If the controls are in a dialog, how are you associating your custom control class with a control on the dialog? I tested on a dialog by adding this member to the dialog class
CReflectedListCtrl m_ListCtrl;
and associating it with a ListView control on the dialog resource by adding this to the dialog class' DoDataExchange()
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
SanjaySMK wrote:
the required no. of CListCtrl-derived class objects are also getting created successfully
Are you sure? How are you verifying that? What code are you using to create your custom control class? If the controls are in a dialog, how are you associating your custom control class with a control on the dialog? I tested on a dialog by adding this member to the dialog class
CReflectedListCtrl m_ListCtrl;
and associating it with a ListView control on the dialog resource by adding this to the dialog class' DoDataExchange()
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
Mark Salsbery Microsoft MVP - Visual C++ :java:
I called a message box from within the constructor of CListCtrl-derived class for which I declared two objects in its parent dialog class, and on both of their construction, I get this message box twice. That's how I ensured.
Software Developer Sanjay Khapre
-
I called a message box from within the constructor of CListCtrl-derived class for which I declared two objects in its parent dialog class, and on both of their construction, I get this message box twice. That's how I ensured.
Software Developer Sanjay Khapre
That covers the C++ object....you also need the HWND object. Again, how are you associating the control with the C++ object?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
That covers the C++ object....you also need the HWND object. Again, how are you associating the control with the C++ object?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I am subclassing both CListCtrl-derived class and the Combo box objects.
Software Developer Sanjay Khapre
Still not answering my question however :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Still not answering my question however :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
Sorry if I haven't answered your last question. The thing is, that subclassing is used in case of CListCtrl-derived class, the reason why I don't see its varible entries inside DoDataExchange, hence the problem. That means the requirement is that of making LBUTTONDOWN and LBUTTONDBLCLK work with subclassed windows of CListCtrl-derived class. Can you help me out with this? Thanks in advance.
Software Developer Sanjay Khapre
-
Sorry if I haven't answered your last question. The thing is, that subclassing is used in case of CListCtrl-derived class, the reason why I don't see its varible entries inside DoDataExchange, hence the problem. That means the requirement is that of making LBUTTONDOWN and LBUTTONDBLCLK work with subclassed windows of CListCtrl-derived class. Can you help me out with this? Thanks in advance.
Software Developer Sanjay Khapre
You have to add the entries to DoDataExchange if they are not there. If the listview controls are in the dialog resource, you can use the wizard to add a control variable. It will add an entry like DDX_Control(pDX, IDC_LIST1, m_ListCtrl); Change m_ListCtrl's type to your derived type in the header file. Then the control will be subclassed. From my previous working sample code:
// In the dialog class
CReflectedListCtrl m_ListCtrl;// The dialog class' DoDataExchange() override
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
}Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Wednesday, January 7, 2009 2:40 AM
-
You have to add the entries to DoDataExchange if they are not there. If the listview controls are in the dialog resource, you can use the wizard to add a control variable. It will add an entry like DDX_Control(pDX, IDC_LIST1, m_ListCtrl); Change m_ListCtrl's type to your derived type in the header file. Then the control will be subclassed. From my previous working sample code:
// In the dialog class
CReflectedListCtrl m_ListCtrl;// The dialog class' DoDataExchange() override
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
}Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Wednesday, January 7, 2009 2:40 AM
In which case CWnd::SubclassWindow(HWND DocumentList) fails? Even though, the variable HWND DocumentList which I am using as its parameter is locally declared and initialized where I am calling SubclassWindow, still SubclassWindow is failing(Debug Assertion Failure!). What is its remedy? Could u please suggest? DocumentList parameter is declared and initialized some lines before calling SubclassWindow, as following: HWND DocumentList = CreateDocumentList(parenthandle, rect.left, rect.top, rect.Width(), rect.Height(), someflag) ; Line 1 Line 2 Line 3 SubclassWindow(DocumentList) ; // which fails here, why?
Software Developer Sanjay Khapre
modified on Thursday, January 8, 2009 5:24 AM
-
In which case CWnd::SubclassWindow(HWND DocumentList) fails? Even though, the variable HWND DocumentList which I am using as its parameter is locally declared and initialized where I am calling SubclassWindow, still SubclassWindow is failing(Debug Assertion Failure!). What is its remedy? Could u please suggest? DocumentList parameter is declared and initialized some lines before calling SubclassWindow, as following: HWND DocumentList = CreateDocumentList(parenthandle, rect.left, rect.top, rect.Width(), rect.Height(), someflag) ; Line 1 Line 2 Line 3 SubclassWindow(DocumentList) ; // which fails here, why?
Software Developer Sanjay Khapre
modified on Thursday, January 8, 2009 5:24 AM
I got the answer of why SubclassWindow was failing, it was because I had DDX_Control entries inside DoDataExchange function, which shouldn't be there, otherwise the parameter to SubclassWindow will always be attached to the parent dialog box, which doesn't fit its criterion. But now after that, the messages WM_LBUTTONDOWN and WM_LBUTTONDBLCLK should run, which are not running. That means my question is, after calling CWnd::SubclassWindow function, why these two messages do not respond at all?Please suggest some remedy.
Software Developer Sanjay Khapre
modified on Thursday, January 8, 2009 9:34 AM
-
I got the answer of why SubclassWindow was failing, it was because I had DDX_Control entries inside DoDataExchange function, which shouldn't be there, otherwise the parameter to SubclassWindow will always be attached to the parent dialog box, which doesn't fit its criterion. But now after that, the messages WM_LBUTTONDOWN and WM_LBUTTONDBLCLK should run, which are not running. That means my question is, after calling CWnd::SubclassWindow function, why these two messages do not respond at all?Please suggest some remedy.
Software Developer Sanjay Khapre
modified on Thursday, January 8, 2009 9:34 AM
Why are you calling SubclassWindow? If you're using MFC you very rarely need to explicitly call it. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
In which case CWnd::SubclassWindow(HWND DocumentList) fails? Even though, the variable HWND DocumentList which I am using as its parameter is locally declared and initialized where I am calling SubclassWindow, still SubclassWindow is failing(Debug Assertion Failure!). What is its remedy? Could u please suggest? DocumentList parameter is declared and initialized some lines before calling SubclassWindow, as following: HWND DocumentList = CreateDocumentList(parenthandle, rect.left, rect.top, rect.Width(), rect.Height(), someflag) ; Line 1 Line 2 Line 3 SubclassWindow(DocumentList) ; // which fails here, why?
Software Developer Sanjay Khapre
modified on Thursday, January 8, 2009 5:24 AM
Where is this code called from? You really need to provide a context. If SubclassWindow is the CWnd SubclassWindow method, then what's the class you are calling it from?
SanjaySMK wrote:
which fails here, why?
Why don't you step into the function with the debugger and find out? :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Where is this code called from? You really need to provide a context. If SubclassWindow is the CWnd SubclassWindow method, then what's the class you are calling it from?
SanjaySMK wrote:
which fails here, why?
Why don't you step into the function with the debugger and find out? :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I have a method in CListCtrl-derived class, from where I am calling CWnd::SubclassWindow, and after calling that, WM_LBUTTONDOWN and WM_LBUTTONDBLCLK do not get called at all.
Software Developer Sanjay Khapre
You still haven't answered WHY you are calling SubclassWindow. Where do the CListCtrl derived objects come from? Show some code.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You still haven't answered WHY you are calling SubclassWindow. Where do the CListCtrl derived objects come from? Show some code.
Mark Salsbery Microsoft MVP - Visual C++ :java:
CListCtrl derived objects come from the parent class, i.e. the dialog class, I call one member function of CListCtrl-derived class, SubClassMyList, in which I am calling SubclassWindow(HWND) function, after which mouse events don't work on this derived class. Sorry, I was away for some days, hence couldn't update you quickly.
Software Developer Sanjay Khapre
-
CListCtrl derived objects come from the parent class, i.e. the dialog class, I call one member function of CListCtrl-derived class, SubClassMyList, in which I am calling SubclassWindow(HWND) function, after which mouse events don't work on this derived class. Sorry, I was away for some days, hence couldn't update you quickly.
Software Developer Sanjay Khapre
SanjaySMK wrote:
in which I am calling SubclassWindow(HWND) function
I don't know how many times I've asked why... All my MFC controls for the past decade have got mouse messages just fine without calling subclassWindow...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
SanjaySMK wrote:
in which I am calling SubclassWindow(HWND) function
I don't know how many times I've asked why... All my MFC controls for the past decade have got mouse messages just fine without calling subclassWindow...
Mark Salsbery Microsoft MVP - Visual C++ :java:
Dear Mark, First of all, I am very sorry if I have tested your patience, although unknowingly and unintentionally. I have some big code of my project, which was running fine in Visual C++ 6.0(in which it was first written), but which I now needed to convert to Visual C++ 2005. After everything I have explained till date, since the functionality(mouse events) after conversion is not running in VC 2005, I created my own MFC application in VC 2005, in which, like the project code, I tried using CWnd's SubclassWindow(HWND). Since this functionality was running in VC 6.0, I need to keep and also make it run in VC 2005. Hence the inclusion of SubclassWindow also in my code(the sample application which I am trying these things in). I hope I am very much clear this time. Could you please help?
Software Developer Sanjay Khapre
modified on Friday, January 16, 2009 12:45 AM