We have a C# winform that uses the MVP design pattern for the user interface. For reasons I'd rather not explain we need to simulate a right mouse click on a specific control to deactivate the control. I found code that almost does what we need.
[DllImport("user 32.dll")]
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);
public static void PerformSingleRi ghtMouseClick(P oint cursorLocation)
{
const UInt32 MOUSEEVENTF_RIG HTDOWN = 0x0008; /* right button down */
const UInt32 MOUSEEVENTF_RIG HTUP = 0x0010; /* right button up */
UInt32 x = Convert.ToUInt3 2(cursorLocatio n.X);
UInt32 y = Convert.ToUInt3 2(cursorLocatio n.Y);
mouse_event(MOU SEEVENTF_RIGHTD OWN, x, y, 0, new IntPtr());
mouse_event(MOU SEEVENTF_RIGHTU P, x, y, 0, new IntPtr());
}
When PerformSingleRi ghtMouseClick is called, it does simulate a right mouse click but there is one problem. It performs the mouseclick from wherever the mouse is currently. So if the user clicks outside of the form, say on the desktop, the windows popup menu displays.
How can I get it so the coordinates I pass to the mouse_event actually use the position I'm passing in.
I tried passing both Cursor.Position and Control.ClientT oPoint and they both still did the same thing.
[DllImport("user 32.dll")]
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);
public static void PerformSingleRi ghtMouseClick(P oint cursorLocation)
{
const UInt32 MOUSEEVENTF_RIG HTDOWN = 0x0008; /* right button down */
const UInt32 MOUSEEVENTF_RIG HTUP = 0x0010; /* right button up */
UInt32 x = Convert.ToUInt3 2(cursorLocatio n.X);
UInt32 y = Convert.ToUInt3 2(cursorLocatio n.Y);
mouse_event(MOU SEEVENTF_RIGHTD OWN, x, y, 0, new IntPtr());
mouse_event(MOU SEEVENTF_RIGHTU P, x, y, 0, new IntPtr());
}
When PerformSingleRi ghtMouseClick is called, it does simulate a right mouse click but there is one problem. It performs the mouseclick from wherever the mouse is currently. So if the user clicks outside of the form, say on the desktop, the windows popup menu displays.
How can I get it so the coordinates I pass to the mouse_event actually use the position I'm passing in.
I tried passing both Cursor.Position and Control.ClientT oPoint and they both still did the same thing.
Comment