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:
Thanks for the sample, it was very helpful.
Post a Comment