I found the solution to our problem.
This is the c# code, it is based on sample code found here:
http://www.**--****.com/
[DllImport("Coredll.dll", SetLastError = true)]
private static extern bool PeekMessage(
out PeekMsg msg,
IntPtr hWnd,
uint messageFilterMin,
uint messageFilterMax,
uint flags);
[DllImport("Coredll.dll", SetLastError = true)]
private static extern Int32 DispatchMessage(ref PeekMsg lpMsg);
[DllImport("Coredll.dll", SetLastError = true)]
private static extern Int32 TranslateMessage(ref PeekMsg lpMsg);
[DllImport("coredll.dll")]
public static extern int PostMessage(IntPtr hWnd, uint Msg, int wParam, int
lParam);
[DllImport("aygshell.dll")]
static extern uint SHRecognizeGesture(SHRGINFO shrg);
[StructLayout(LayoutKind.Sequential)]
class SHRGINFO
{
public uint cbSize = 0;
public IntPtr hwndClient = IntPtr.Zero;
public int x = 0; // POINT
public int y = 0; // POINT
public uint dwFlags = 0;
}
[StructLayout(LayoutKind.Sequential)]
public struct PeekMsg
{
public IntPtr hwnd;
public Int32 message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point pt;
}
internal const int WM_LBUTTONUP = 0x0202,
WM_RBUTTONUP = 0x0205,
WM_LBUTTONDOWN = 0x0201,
MK_LBUTTON = 0x01,
WM_LBUTTONDBLCLK = 0x0203,
WM_CONTEXTMENU = 0x7b;
private const int WM_NOREMOVE = 0x0;
private const uint GN_CONTEXTMENU = 1000;
private const uint SHRG_RETURNCMD = 0x00000001;
private const uint SHRG_NOTIFYPARENT = 0x00000002;
private const uint SHRG_LONGDELAY = 0x00000008;
private const uint SHRG_NOANIMATION = 0x00000010;
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
//instead of the normal Application.Run(new Form1); we must manually display
the form
Form1 formInstance = new Form1();
formInstance.Visible = true;
PeekMsg msg = new PeekMsg();
while (1 == 1)
{
PeekMessage(out msg, formInstance.Handle, 0, 0, WM_REMOVE);
switch (msg.message)
{
case WM_LBUTTONDOWN:
Debug.WriteLine(string.Format("lbuttondown: msg:{0}, lparam:{1},
wParam:{2}, hwnd:{3}, time:{4}", msg.message, msg.lParam, msg.wParam, msg.hwnd,
msg.time));
SHRGINFO shrginfo = new SHRGINFO();
shrginfo.cbSize = (uint)Marshal.SizeOf(shrginfo);
shrginfo.hwndClient = msg.hwnd;
shrginfo.x = msg.lParam.ToInt32() & 0x0000ffff;
shrginfo.y = (msg.lParam.ToInt32() & 0x0fff0000) >> 16;
shrginfo.dwFlags = SHRG_RETURNCMD;
if (SHRecognizeGesture(shrginfo) == GN_CONTEXTMENU)
{
Debug.WriteLine("in context menu loop");
PostMessage(msg.hwnd, WM_LBUTTONUP, 0, 0);
}
break;
default:
break;
}
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}