Results 1 to 4 of 4

Thread: [2.0] Stimulate a mouseclick

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] Stimulate a mouseclick

    Alright, ALL I want to do is stimulate a mouse click. Something like.

    SendKeys.Send("{LEFTMOUSECLICK}");

    but of course that doesn't work. Now, in VB6 it was easy. Something like this:

    Private m_Mouse As CMouseEvent
    m_Mouse.Location = 0,0
    m_Mouse.ButtonClick


    whatever it was something like that i forget. But anyways I can't find anything about doing this in C#. Any ideaS?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Stimulate a mouseclick

    This is what I have so far.

    Code:
            MouseEventArgs m;
    
            private void btnTest_Click(object sender, EventArgs e)
            {
                m.Button = MouseButtons.Left;
                OnMouseClick(m);
            }
    Because that is what it says on:
    http://msdn2.microsoft.com/en-us/library/ms171548.aspx

    However, m.Button is read-only even though in the second paragraph they say to assign it to a left or right click or whatever. Do I need to ovveride it? If so, how? I have never overrided anything before nor do I know how to do it or how to use it. Thanks

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Stimulate a mouseclick

    Finally, after 5 hours of looking and searching google I found this. However, is it the best way?

    Code:
            private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    
            [DllImport("user32.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 SendClick(Point location)
            {
                Cursor.Position = location;
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
            }
    
            private void btnTest_Click(object sender, EventArgs e)
            {
                Timer.Enabled = true;
            }
    
            private void Timer_Tick(object sender, EventArgs e)
            {
                Point location = new Point(MousePosition.X, MousePosition.Y);
                SendClick(location);
            }

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Stimulate a mouseclick

    If you want to affect applications outside your own, or be affected by applications outside your own in the case of the keyboard hook, then it is almost always going to involve platform invoke (Windows API). What I would suggest is wrapping this sort of functionality in a class of its own, which you can then use whenever and wherever you need to with a couple of lines of code. That's the advantage with OOP. Maybe something does take a reasonable amount of code, but once you've written that code once it is a very simple thing to invoke it in many different places.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width