C# WinForm: Simulate Right Mouse Click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nuhfeken
    New Member
    • Sep 2007
    • 3

    C# WinForm: Simulate Right Mouse Click

    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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    you could try picking a static number like (10,10) and see if it performs the click 10,10 from the top left corner of screen, top left corner of window or top left corner of a control?

    Comment

    • nuhfeken
      New Member
      • Sep 2007
      • 3

      #3
      Well, I learned some more yesterday on this. I still haven't fixed the underlying problem which is a control won't release the mouse, which forces someone to click any other control (other than the one already selected) twice. Once to activate or set the focus to it, again to click the control. That is the underlying problem which I had hoped to fix by simulating a right mouse click (a horrible solution but the only one that seemed to work). I tried setting control properties, tried changing the focus, nothing seems to work. Except the right click. The real problem is a call to Application.DoE vents in the middle of a controls UI events. But it is required to keep the circular wait cursor moving.

      When I called the above "mouse_even t", the x,y coordinates are meaningless unless you add MOUSEEVENTF_MOV E to the code like this.

      public static void PerformSingleRi ghtMouseClick(/*Point cursorLocation*/)
      {
      const UInt32 MOUSEEVENTF_RIG HTDOWN = 0x0008; /* right button down */
      const UInt32 MOUSEEVENTF_RIG HTUP = 0x0010; /* right button up */
      //const UInt32 MOUSEEVENTF_ABS OLUTE = 0x8000;
      const UInt32 MOUSEEVENTF_MOV E = 0x0001;

      UInt32 x = Convert.ToUInt3 2(cursorLocatio n.X);
      UInt32 y = Convert.ToUInt3 2(cursorLocatio n.Y);

      mouse_event(MOU SEEVENTF_RIGHTD OWN | MOUSEEVENTF_MOV E , 0, 0, 0, new IntPtr());
      mouse_event(MOU SEEVENTF_RIGHTU P | MOUSEEVENTF_MOV E , 0, 0, 0, new IntPtr());
      }

      This will move the mouse to whatever the specified co-ordinates are. This really isn't the desired behavior I was seeking. I just want to instantly perform a right click at the exact time the user clicks the control.

      I also tried
      [DllImportAttrib ute("user32.dll ")]
      public static extern bool ReleaseCapture( );

      Hoping this would release the mouse capture but it didn't seem to fix the problem either.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I know you said not to ask, but if you're using mvc...why are you even bothering to come into the .NET world.
        That's like buying a $4000 couch so you can grind your muddy shoes on it.

        Comment

        • nuhfeken
          New Member
          • Sep 2007
          • 3

          #5
          Well, because I inherited the project. That is why. LOL!

          In their defense, the application is a rather complex, large application (UI layout is similer to Microsoft Outlook) with a ton of controls and no popup forms/separate forms. Minus some minor complexity and the occasional weird problem like this one I think the alternative might of been even scarier. A spaghetti-code nightmare with insane amounts of duplicate code, near identical forms and controls everywhere.

          Comment

          • chagbert
            New Member
            • May 2012
            • 1

            #6
            You need run your events under a different THREAD and use an await async to get the results from it (if you need results)

            Comment

            Working...