2

I have a ListView where I display other controls on top of it. I can capture when you click one of the controls and take action. What I would like is to also send a click notification to the ListView below it so it would act as if the ListView was clicked. So it will select/unselect the item in the ListView where the click happened. I want to send a click message to the ListView rather than manually select/unselect the item because I don't want to have to try and figure out if it is a multiselect list or not, what keys are pressed, what non-clicked items need to remain selected or unselected, ... Ideally I would send a click notification to the ListView and it would take all appropriate actions just as if it was clicked.

I have played around with calling the SendMessage from user32.dll - both WM_NOTIFY and OCM_NOTIFY with an NMITEMACTIVATE struct as the lParam. I cant seem to get it to work. I have also played with the OnNotifyMessage call with no luck.

I can successfully capture the click event from the control on top of the list view, get the cursor position, translate that to the item/sub item in the ListView below it, ... I just can't seem to programmatically send whatever notification needs to be sent to the ListView so it takes appropriate action.

EDIT:

The possible solution in the link below will allow the click to pass through the control to the ListView below it, but you must create custom classes for all controls and you lose the ability to handle any click events in the controls on top.

jrhoads23
  • 109
  • 1
  • 8
  • Seems as you just over-complexing what you need. *"I don't want to have to try and figure out if it is a multiselect list or not."* - this is a 1-liner – T.S. Aug 04 '17 at 18:10
  • If it is a multiselect list, and you send a click event to the list, it should behave to select only 1 item when the ctrl button is not pressed. In other words, I don't see the added complexity that occurs when manually selecting/unselecting. – Frederik Gheysels Aug 04 '17 at 18:18
  • It would be quite complex to try and simulate the logic the ListView does. You would need to know 1) is it multiselect, 2) is the ctrl key pressed, 3) is the shift key pressed, 4) what was the last item selected (if there is one selected),5) is the current item selected or not ... While it would be fairly easy to get an answer to all of these, you would need to program what to select and unselect based on all combinations of these variables - it would get messy and much easier to just fire off a message to the ListView and have it do it – jrhoads23 Aug 04 '17 at 18:20
  • For example, if it is multiselect and the Ctrl key is pressed, you would just select/unselect the item where the click happened (and leave all other items as is). If the Shift key is pressed you would need to know what the last item selected was and then select all items inbetween the two (and least all others). If neither the Ctrl nor the Shift keys were pressed you would select/unselect the item where the click happened and unselect any other items that were selected. I believe all this may be possible, but if there is a easy way to tell the ListView to figure it out, that would be ideal – jrhoads23 Aug 04 '17 at 18:30
  • 1
    Possible duplicate of [Pass-through mouse events to parent control](https://stackoverflow.com/questions/547172/pass-through-mouse-events-to-parent-control) – ErikusMaximus Aug 04 '17 at 19:33
  • This code will let you do what you want: https://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c. There are still quite a few complexities that you will have to address: focus management, marquee selection, coordinate transformation. Good luck – Grammarian Aug 14 '17 at 04:48

1 Answers1

2

You'll need to create a class that extends the control residing over the ListView and override one of its events. Here is a very similar question question to yours that should help you. https://stackoverflow.com/a/8635626/3508142

ErikusMaximus
  • 1,150
  • 3
  • 13
  • 28
  • Thank you. That gets me closer. That link shows an example how to put a custom Label class over it and it will pass the click through to the list view. However 1) it requires you to create custom classes for all objects you want to have this functionality (not a huge deal) and 2) doesnt allow you to have any OnClick functionality in those classes (probably an issue). Ideally you could handle the OnClick in the Label (or whatever it is) and then send a message to the ListView below it – jrhoads23 Aug 04 '17 at 20:19