Sunday, March 25, 2007

Implementing Tap and Hold funtionality in CF

When creating your own user control maybe an owner drawn list of items in either CF1 or CF2 you might want to implement the standard Windows Mobile tap and hold little red dots that appear can be achieved by P/invoking SHRecognizeGesture which can be found in the aygshell.dll module.

The following code shows how this can be achieved. You would typically call the ShowMenu() method from an OnMouseDown event in your user control.



using System;

namespace RecognizeGesture
{

public class RGesture
{
///
/// Structure used by SHRecognizeGesture
///

internal struct SHRGINFO
{
public int cbSize;
public IntPtr hwndClient;
public int ptDownX;
public int ptDownY;
public SHRGFLags dwFlags;
}

///
/// SHRGINFO flags
///

[Flags]
internal enum SHRGFLags
{
SHRG_RETURNCMD = 0x00000001,
SHRG_NOTIFYPARENT = 0x00000002,
///
/// use the longer (mixed ink) delay timer
/// useful for cases where you might click down first, verify you're
/// got the right spot, then start dragging... and it's not clear
/// you wanted a context menu
///

SHRG_LONGDELAY = 0x00000008,
SHRG_NOANIMATION = 0x00000010,
}

[DllImport("aygshell")]
extern private static int SHRecognizeGesture(ref SHRGINFO shr);

private void ShowMenu()
{
SHRGINFO shr = new SHRGINFO();
shr.cbSize = Marshal.SizeOf(typeof(SHRGINFO));
shr.dwFlags = SHRGFLags.SHRG_RETURNCMD;
shr.ptDownX = x;
shr.ptDownY = y;
shr.hwndClient = Handle;

//Ask shell to track the gesture.
int ret = SHRecognizeGesture(ref shr);

//If user used tap-and-hold - track popup menu.
if (ret == 1000)
{
//Display menu.
}
}
}
}

1 comment:

luke said...

Thanks for the sample, it was very helpful.