views:

483

answers:

4

How can I implement the "tap and hold" behavior in an MFC CListCtrl?

I have an MFC CListCtrl in a CDialog and I need to add to the CListCtrl the "tap and hold" behavior.

That is what I mean for "tap and hold":

If the user clicks (a "standard" click with the left button of the mouse) on a CListCtrl item then function A will be called.

If the user click on a CListCtrl item and keep pressed down the left button of the mouse for more than one second then function B will be called.

+2  A: 

Inherit CListCtrl and implement OnLButtonDown and OnLButtonUp. Set a timer in OnLButtonDown for the length you want to wait before doing a "tab and hold."

In your timer handler, perform the "tap and hold" action.

If you get OnLButtonUp before the timer expires, cancel the timer and perform the "click" action (if the CPoint parameter of OnLButtonUp is over a list item.)

Aidan Ryan
You may want to capture the mouse, or try to detect when the mouse leaves the window (or the item) - in case the user mouse-down, moves, and mouse-up.
Aardvark
Thank you for your suggestion. Unfortunately it is not so easy because CListCtrl seems to have its own "tap and hold" behavior, see http://groups.google.com/group/microsoft.public.vc.mfc/browse_thread/thread/9add59cb5260f175/ – sacmi.myopenid.com
uvts_cvs
Have you tried implementing the above methods? Are they being called?
Aidan Ryan
Yes, I did implement the methods and I got what is described in the newsgroup.
uvts_cvs
+1  A: 

In addition to ajryans suggestion, you may also need to subclass your CListBox derived class in order for it to pick up mouse messages from you dialog. Something like;

BOOL CMyDialog::OnInitDialog() 
{
  MyListboxCtrl.SubclassDlgItem(ID_MYLISTBOX,this);
  CDialog::OnInitDialog();
}
Shane MacLaughlin
A: 

Here is another approach without timers http://www.codeproject.com/KB/mobile/tapandhold.aspx.

Ismael
A: 

It can be done by pinvoking

http://www.ceveni.com/2009/10/sample-code-for-context-menu-in-c.html

i think this is what you are looking for?

Sagar