Simulating mouse movements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bwahahahaha

    #1

    Simulating mouse movements

    How can I inject the MouseMove events to move the mouse programatically in
    C#, or is this not possible and I have to drop to Win32 for this?


  • Michael Bray

    #2
    Re: Simulating mouse movements

    "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
    news:OdswqAzkDH A.2000@TK2MSFTN GP12.phx.gbl:
    [color=blue]
    > How can I inject the MouseMove events to move the mouse
    > programatically in C#, or is this not possible and I have to drop to
    > Win32 for this?[/color]

    use the static property Cursor.Position

    -mbray

    Comment

    • bwahahahaha

      #3
      Re: Simulating mouse movements

      But thats per control right? I can only do this at the form level, but not
      globally on the desktop?


      "Michael Bray" <mbrayXXnoSPAMp leaseXX@SkPiAlM l.ctiusa.com> wrote in message
      news:Xns941574C 778D79mbrayxyzc tiusacom@207.46 .248.16...[color=blue]
      > "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
      > news:OdswqAzkDH A.2000@TK2MSFTN GP12.phx.gbl:
      >[color=green]
      > > How can I inject the MouseMove events to move the mouse
      > > programatically in C#, or is this not possible and I have to drop to
      > > Win32 for this?[/color]
      >
      > use the static property Cursor.Position
      >
      > -mbray[/color]


      Comment

      • Michael Bray

        #4
        Re: Simulating mouse movements

        "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
        news:OVis6IzkDH A.688@TK2MSFTNG P10.phx.gbl:
        [color=blue]
        > But thats per control right? I can only do this at the form level,
        > but not globally on the desktop?[/color]

        Cursor.Position will move the mouse but not simulate clicks. If you want
        to simulate a click on a control, such as 'Button1', and you have a click
        handler, such as:

        public void button1_Click(o bject sender, MouseEventArgs e)
        {
        ...
        }

        then just call (from wherever you need to simulate the click):

        button1_Click(n ull, null)

        as long as you don't process sender or the MouseEventArgs. If you do
        process them, then simulate them as well...

        MouseEventArgs mea = new MouseEventArgs( ...);
        <set the properties of mea>
        button1_Click(b utton1, mea);

        Comment

        • bwahahahaha

          #5
          Re: Simulating mouse movements

          SO there isnt an equivelent of PostMessage in C# to send events, we just
          call the eventhandler directly? hmm


          "Michael Bray" <mbrayXXnoSPAMp leaseXX@SkPiAlM l.ctiusa.com> wrote in message
          news:Xns9415774 4E9EFFmbrayxyzc tiusacom@207.46 .248.16...[color=blue]
          > "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
          > news:OVis6IzkDH A.688@TK2MSFTNG P10.phx.gbl:
          >[color=green]
          > > But thats per control right? I can only do this at the form level,
          > > but not globally on the desktop?[/color]
          >
          > Cursor.Position will move the mouse but not simulate clicks. If you want
          > to simulate a click on a control, such as 'Button1', and you have a click
          > handler, such as:
          >
          > public void button1_Click(o bject sender, MouseEventArgs e)
          > {
          > ...
          > }
          >
          > then just call (from wherever you need to simulate the click):
          >
          > button1_Click(n ull, null)
          >
          > as long as you don't process sender or the MouseEventArgs. If you do
          > process them, then simulate them as well...
          >
          > MouseEventArgs mea = new MouseEventArgs( ...);
          > <set the properties of mea>
          > button1_Click(b utton1, mea);[/color]


          Comment

          • bwahahahaha

            #6
            Re: Simulating mouse movements

            But, what if the eventhandler method isnt directly available?


            "Michael Bray" <mbrayXXnoSPAMp leaseXX@SkPiAlM l.ctiusa.com> wrote in message
            news:Xns9415774 4E9EFFmbrayxyzc tiusacom@207.46 .248.16...[color=blue]
            > "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
            > news:OVis6IzkDH A.688@TK2MSFTNG P10.phx.gbl:
            >[color=green]
            > > But thats per control right? I can only do this at the form level,
            > > but not globally on the desktop?[/color]
            >
            > Cursor.Position will move the mouse but not simulate clicks. If you want
            > to simulate a click on a control, such as 'Button1', and you have a click
            > handler, such as:
            >
            > public void button1_Click(o bject sender, MouseEventArgs e)
            > {
            > ...
            > }
            >
            > then just call (from wherever you need to simulate the click):
            >
            > button1_Click(n ull, null)
            >
            > as long as you don't process sender or the MouseEventArgs. If you do
            > process them, then simulate them as well...
            >
            > MouseEventArgs mea = new MouseEventArgs( ...);
            > <set the properties of mea>
            > button1_Click(b utton1, mea);[/color]


            Comment

            • Michael Bray

              #7
              Re: Simulating mouse movements

              "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in
              news:#a1ktPzkDH A.3320@tk2msftn gp13.phx.gbl:
              [color=blue]
              > But, what if the eventhandler method isnt directly available?[/color]

              Well I guess the real question is, what are you really trying to do? Or,
              why are you trying to simulate a mouse click?

              IF you are just trying to perform the action that the application would
              perform, then the method will work. You *could* make the event handler
              public, but that's not exactly the best programming practice. Better would
              be to put the code that runs in a separate public function, and have the
              handler call that code.

              IF you are actually trying to write a simulator (eg an automation package)
              then I'll have to admit that I don't have the knowledge to answer your
              question. I haven't personally noticed anything in .NET that will post a
              windows message to another application, but there may be one. Otherwise,
              P/Invoke isn't too difficult to get your arms around - give it a shot.

              -mbray

              Comment

              Working...