P/Invoke SHRecognizeGesture

  • Thread starter Thread starter B McMurray
  • Start date Start date
B

B McMurray

Hi,

Ive been trying to p/invoke this function but always get
a System.NotSupportedException exception (yeah that old
chestnut). Can anyone spot what im doing wrong? :

[DllImport("coredll.dll", SetLastError=true)] private
static extern uint SHRecognizeGesture(SHRGINFO shrg);
....
[StructLayout(LayoutKind.Sequential)] private struct
SHRGINFO
{
public uint cbSize;
public IntPtr hwndClient;
public UInt16 ptX;
public UInt16 ptY;
public uint dwFlags;
}
....
SHRGINFO shrg = new SHRGINFO();
shrg.cbSize = (uint)Marshal.SizeOf(shrg.GetType
());
shrg.ptX = (UInt16)e.X;
shrg.ptY = (UInt16)e.Y;
shrg.dwFlags = SHRG_RETURNCMD;
this.Capture = true;
shrg.hwndClient = GetCapture();
this.Capture = false;

x = SHRecognizeGesture(shrg);

rgds
Bob
 
It's most likely the fact that you are using UInt16's for ptX and ptY. Here
is the code that I have used many times:

private const uint GN_CONTEXTMENU = 1000;
private const uint SHRG_RETURNCMD = 0x00000001;

[StructLayout(LayoutKind.Sequential)]
private 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;
}

[DllImport("aygshell.dll")]
private static extern uint SHRecognizeGesture(SHRGINFO shrg);

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Capture = true;
IntPtr hWnd = GetCapture();
this.Capture = false;

SHRGINFO shrginfo = new SHRGINFO();
shrginfo.cbSize = (uint)Marshal.SizeOf(shrginfo);
shrginfo.hwndClient = hWnd;
shrginfo.x = e.X;
shrginfo.y = e.Y;
shrginfo.dwFlags = SHRG_RETURNCMD;

if (SHRecognizeGesture(shrginfo) == GN_CONTEXTMENU)
{
// Do stuff
}
}

base.OnMouseDown(e);
}
 
Thanks Tim, that worked a treat. For some reason I
thought a C point was 32 bit.

I noticed that you used a class for SHRGInfo instead of a
struct - you learn something every day :)

Bob
-----Original Message-----
It's most likely the fact that you are using UInt16's for ptX and ptY. Here
is the code that I have used many times:

private const uint GN_CONTEXTMENU = 1000;
private const uint SHRG_RETURNCMD = 0x00000001;

[StructLayout(LayoutKind.Sequential)]
private 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;
}

[DllImport("aygshell.dll")]
private static extern uint SHRecognizeGesture(SHRGINFO shrg);

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

protected override void OnMouseDown
(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Capture = true;
IntPtr hWnd = GetCapture();
this.Capture = false;

SHRGINFO shrginfo = new SHRGINFO();
shrginfo.cbSize = (uint)Marshal.SizeOf(shrginfo);
shrginfo.hwndClient = hWnd;
shrginfo.x = e.X;
shrginfo.y = e.Y;
shrginfo.dwFlags = SHRG_RETURNCMD;

if (SHRecognizeGesture(shrginfo) == GN_CONTEXTMENU)
{
// Do stuff
}
}

base.OnMouseDown(e);
}

--
Tim Wilson
Windows Embedded MVP
B McMurray said:
Hi,

Ive been trying to p/invoke this function but always get
a System.NotSupportedException exception (yeah that old
chestnut). Can anyone spot what im doing wrong? :

[DllImport("coredll.dll", SetLastError=true)] private
static extern uint SHRecognizeGesture(SHRGINFO shrg);
...
[StructLayout(LayoutKind.Sequential)] private struct
SHRGINFO
{
public uint cbSize;
public IntPtr hwndClient;
public UInt16 ptX;
public UInt16 ptY;
public uint dwFlags;
}
...
SHRGINFO shrg = new SHRGINFO();
shrg.cbSize = (uint)Marshal.SizeOf(shrg.GetType
());
shrg.ptX = (UInt16)e.X;
shrg.ptY = (UInt16)e.Y;
shrg.dwFlags = SHRG_RETURNCMD;
this.Capture = true;
shrg.hwndClient = GetCapture();
this.Capture = false;

x = SHRecognizeGesture(shrg);

rgds
Bob


.
 
A struct is passed by value (by default) and a class is passed by reference.
The SHRecognizeGesture API expects a (from the documentation):
Pointer to a SHRGINFO structure.

So (as confusing as this may seem since the docs do mention a structure) you
need to pass a reference to satisfy the pointer, and it just so happens that
using a class is the easiest way to do this.

--
Tim Wilson
Windows Embedded MVP

B McMurray said:
Thanks Tim, that worked a treat. For some reason I
thought a C point was 32 bit.

I noticed that you used a class for SHRGInfo instead of a
struct - you learn something every day :)

Bob
-----Original Message-----
It's most likely the fact that you are using UInt16's for ptX and ptY. Here
is the code that I have used many times:

private const uint GN_CONTEXTMENU = 1000;
private const uint SHRG_RETURNCMD = 0x00000001;

[StructLayout(LayoutKind.Sequential)]
private 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;
}

[DllImport("aygshell.dll")]
private static extern uint SHRecognizeGesture(SHRGINFO shrg);

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

protected override void OnMouseDown
(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Capture = true;
IntPtr hWnd = GetCapture();
this.Capture = false;

SHRGINFO shrginfo = new SHRGINFO();
shrginfo.cbSize = (uint)Marshal.SizeOf(shrginfo);
shrginfo.hwndClient = hWnd;
shrginfo.x = e.X;
shrginfo.y = e.Y;
shrginfo.dwFlags = SHRG_RETURNCMD;

if (SHRecognizeGesture(shrginfo) == GN_CONTEXTMENU)
{
// Do stuff
}
}

base.OnMouseDown(e);
}

--
Tim Wilson
Windows Embedded MVP
B McMurray said:
Hi,

Ive been trying to p/invoke this function but always get
a System.NotSupportedException exception (yeah that old
chestnut). Can anyone spot what im doing wrong? :

[DllImport("coredll.dll", SetLastError=true)] private
static extern uint SHRecognizeGesture(SHRGINFO shrg);
...
[StructLayout(LayoutKind.Sequential)] private struct
SHRGINFO
{
public uint cbSize;
public IntPtr hwndClient;
public UInt16 ptX;
public UInt16 ptY;
public uint dwFlags;
}
...
SHRGINFO shrg = new SHRGINFO();
shrg.cbSize = (uint)Marshal.SizeOf(shrg.GetType
());
shrg.ptX = (UInt16)e.X;
shrg.ptY = (UInt16)e.Y;
shrg.dwFlags = SHRG_RETURNCMD;
this.Capture = true;
shrg.hwndClient = GetCapture();
this.Capture = false;

x = SHRecognizeGesture(shrg);

rgds
Bob


.
 
Back
Top