Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a dialog (CMeasureLC) with a CMFCListCtrl in it. I want the user to be able to edit the text in the header row (leftmost column only), in response to clicking on the header cell. I know how to edit the text once I detect a click in the cell, but the first task is to detect the click - and I'm stuck.

What I have tried:

I've tried to detect it by mapping in the dialog. I used ClassWizard to map LVN_COLUMNCLICK in the dialog thus:
In .h
afx_msg void OnColumnclickDataList(NMHDR* pNMHDR, LRESULT* pResult);

and in .cpp (IDC_DATA_LIST is the ID of the CListCtrl)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_DATA_LIST, &CMeasureLC::OnColumnclickDataList)
...
void CMeasureLC::OnColumnclickDataList(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;
}

However, this does not get called at all when I click in the header row.
In fact, the only mapping that I can get to work in the dialog is
ON_NOTIFY(NM_CLICK, IDC_DATA_LIST, &CMeasureLC::OnClickDataList)

which responds when I click on a cell in the body of the list control.
I have also tried subclassing the list control and handling the reflected message there:
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, &CEditListCtrl::OnLvnColumnclick)

but that doesn't get called either.
Any help would be much appreciated - I've got myself very confused about where the notifications get sent, and how to detect them.
Posted
Updated 22-Aug-20 2:09am
v2

Thanks Richard. That was my understanding of the documentation too, but it wasn't working in my code and I couldn't figure out why. But after a bunch of experiments I think I've got it sorted.
There were two problems.

First:
To detect mouse clicks in the header, the list control apparently has to have the LVS_NOSORTHEADER style set FALSE. I don't want sorting, so I had set it to TRUE in the dialog editor properties section, which seemed reasonable, but this seems to stop the mouse click generating the LVN_COLUMNCLICK message at all.

Once I set the property FALSE, I was able to pick up the message in the parent dialog as per the documentation, with the handler as in my original problem post.

However, this ONLY works with a CListCtrl, not with a CMFCListCtrl. I preferred the latter because it generates a nicer default colour for the header (at least with my system).

Second:
Examination of the CMFCListCtrl source code in afxlistctrl.cpp showed that it traps LVN_COLUMNCLICK in a NotifyRefect handler, which then calls the virtual sort code
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, &CMFCListCtrl::OnColumnClick)
.
The Reflect part means that the message does not propagate to the parent dialog, which was why I didn't pick it up there. I was able to get round this by subclassing CMFCListCtrl, and handling the message in my version of the control. This responds to mouse clicks in the header, and I could then activate the mechanism for changing its text, which was my original aim. The reason it didn't work in my first attempt at subclassing was the styles problem described in First above.
 
Share this answer
 
v2
According to the documentation (LVN_COLUMNCLICK notification code (Commctrl.h) - Win32 apps | Microsoft Docs[^]) this should be received when you click the column header.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900