简体   繁体   中英

Mouse Simulation In .exe window

I want to make a mouse macroer. Which can both do simulated mouse events, or using my computers own cursor on screen.

The macro shall be created by typing in methods in the IDE. These methods shall then execute mouse events on a certain .exe´s window. By using coordinates.

For example this is my goal of a method executing a simulated or not simulated mouse left click on a certain .exe´s window:

Psuedo code:

//Following method left clicks with the offset (x, y) 
//from the windows top left corner. If the bool isSimulated 
//is set to true the click will be simulated else the computers 
//own mouse cursor will be moved and execute the mouse event.

LeftMouseClickOnWindow(x, y, isSimulated);

To chrisp the problem even more, simulated mouse clicks should function while the window is minimized or when unfocused.

I am wondering what the best approach to create this kind of util is.

Is user32.dll´s functions a good approach?

Is it easier to do it in C++ rather than C#?

Any advices, sources, example codes and comments is warmly appreciated!

Both C++ and C# are great. AutoHotKey can do the job, but I'm like you - I love to write my own stuff. Another option is AutoIt , and you can use its dll in your C# project... but then you have to make sure that it's installed on every system... not a luxury that I've encountered often.

Here's something to play around with. Hopefully it'll get you going... note that it's C#. Before you run this code, make sure that you don't have anything important open where your mouse is at... this will move 20 times in a diagonal to the lower right and perform a click every time it moves. You don't want this to close your stuff accidentally. So, just minimize it all, prior to running this.

using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ConsoleApplication
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;

        //private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        //private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void DoMouseStuff()
        {
            Cursor.Current = new Cursor(Cursor.Current.Handle);
            var point = new Point(Cursor.Position.X, Cursor.Position.Y);
            for (int i = 0; i < 20; i++, point.X += 10, point.Y += 10)
            {
                Cursor.Position = point;
                Program.mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
                System.Threading.Thread.Sleep(100);
            }
        }

        static void Main(string[] args)
        {
            var prog = new Program();
            prog.DoMouseStuff();
        }
    }
}

You'll need to set references for System.Windows.Forms & System.Drawing , if you don't have those set already. I made it as a console app, so, setting for me was required. As you notice, I included System.Threading.Thread.Sleep(100); ... this is so that you can see what's going on. So, I'm basically slowing down the whole thing. It moves and it clicks every time it moves (which is approximately once every 100 milliseconds).

Familiarize yourself with the Cursor and user32.dll .

Last, but not least, here's MSDN documentation on mouse & keyboard simulation: http://msdn.microsoft.com/en-us/library/ms171548.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM