.Net 控制滑鼠 .Net 控制滑鼠
  .NET       ez      2012-06-04

.Net 已經內建控制滑鼠位置的指令:

Cursor.Position = new Point(30, 60); //X Y軸位置

如果要點擊滑鼠就需要透過WinAPI才能達到:

首先建立一個Class:

using System.Threading;
using System.Runtime.InteropServices;
using System;

namespace Test
{
    static public class Mouse
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern Int32 SendInput(Int32 cInputs, ref INPUT pInputs, Int32 cbSize);

        [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 28)]
        public struct INPUT
        {
            [FieldOffset(0)]
            public INPUTTYPE dwType;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBOARDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct MOUSEINPUT
        {
            public Int32 dx;
            public Int32 dy;
            public Int32 mouseData;
            public MOUSEFLAG dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct KEYBOARDINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public KEYBOARDFLAG dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct HARDWAREINPUT
        {
            public Int32 uMsg;
            public Int16 wParamL;
            public Int16 wParamH;
        }

        public enum INPUTTYPE : int
        {
            Mouse = 0,
            Keyboard = 1,
            Hardware = 2
        }

        [Flags()]
        public enum MOUSEFLAG : int
        {
            MOVE = 0x1,
            LEFTDOWN = 0x2,
            LEFTUP = 0x4,
            RIGHTDOWN = 0x8,
            RIGHTUP = 0x10,
            MIDDLEDOWN = 0x20,
            MIDDLEUP = 0x40,
            XDOWN = 0x80,
            XUP = 0x100,
            VIRTUALDESK = 0x400,
            WHEEL = 0x800,
            ABSOLUTE = 0x8000
        }

        [Flags()]
        public enum KEYBOARDFLAG : int
        {
            EXTENDEDKEY = 1,
            KEYUP = 2,
            UNICODE = 4,
            SCANCODE = 8
        }
    }
}

將以下Function放到程式碼內:

        /// <summary>
        /// 點擊滑鼠左鍵
        /// </summary>
        static public void LeftClick()
        {
            LeftDown();
            Thread.Sleep(200);
            LeftUp();
        }

        /// <summary>
        /// 雙擊滑鼠左鍵
        /// </summary>
        static public void LeftDoubleClick()
        {
            LeftClick();
            Thread.Sleep(500);
            LeftClick();
        }

        /// <summary>
        /// 按下滑鼠左鍵
        /// </summary>
        static public void LeftDown()
        {
            Mouse.INPUT leftdown = new Mouse.INPUT();
            leftdown.dwType = 0;
            leftdown.mi = new Mouse.MOUSEINPUT();
            leftdown.mi.dwExtraInfo = IntPtr.Zero;
            leftdown.mi.dx = 0;
            leftdown.mi.dy = 0;
            leftdown.mi.time = 0;
            leftdown.mi.mouseData = 0;
            leftdown.mi.dwFlags = Mouse.MOUSEFLAG.LEFTDOWN;
            Mouse.SendInput(1, ref leftdown, Marshal.SizeOf(typeof(Mouse.INPUT)));
        }

        /// <summary>
        /// 放開滑鼠左鍵
        /// </summary>
        static public void LeftUp()
        {
            Mouse.INPUT leftup = new Mouse.INPUT();

            leftup.dwType = 0;
            leftup.mi = new Mouse.MOUSEINPUT();
            leftup.mi.dwExtraInfo = IntPtr.Zero;
            leftup.mi.dx = 0;
            leftup.mi.dy = 0;
            leftup.mi.time = 0;
            leftup.mi.mouseData = 0;
            leftup.mi.dwFlags = Mouse.MOUSEFLAG.LEFTUP;
            Mouse.SendInput(1, ref leftup, Marshal.SizeOf(typeof(Mouse.INPUT)));
        }

點擊滑鼠只需要輸入以下:

LeftClick()

雙擊滑鼠只需要輸入以下:

LeftDoubleClick()

標籤:   .NET

我要留言