C# Mouse Contrul In Visual Studio

前言

本章將講述mouse操作範例code(以下控制動作不限於在Form裡)

定義

命名空間:System.Windows.Forms組件:

表示用來繪製滑鼠指標的影像。

[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.CursorConverter))]
public sealed class Cursor : IDisposable, System.Runtime.Serialization.ISerializable

繼承Object->Cursor / 屬性TypeConverterAttribute / 實作IDisposableISerializable

範例code

控制滑鼠移動到某個位置

Cursor.Position = new Point(x, y);

控制滑鼠點擊就需要呼叫API了,第一步是先宣告滑鼠事件API

   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
            }
        }
控制滑鼠左鍵按下
 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)));
        }
控制滑鼠左鍵放開
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)));
        }

PS: 控制滑鼠右鍵/中鍵,只要改一下MOUSEFLAG就可以了

點一下(點擊)
static public void LeftClick()
{
    LeftDown();
    Thread.Sleep(20);
    LeftUp();
}
連點兩下
static public void LeftDoubleClick()
{
    LeftClick();
    Thread.Sleep(50);
    LeftClick();
}
拖曳
static public void DragTo(int sor_X, int sor_Y, int des_X, int des_Y)
{
    Cursor.Position = new Point(sor_X, sor_Y); //在來源按下左鍵
    LeftDown();
    Thread.Sleep(200);
    Cursor.Position = new Point(des_X, des_Y); //到目的放開左鍵
    LeftUp();
}

Doma結果

按下test後鼠標會移動到下籃框位置並顯示座標,這邊第一步是先抓取textbox在畫面上的座標位置並把他先設定零點

  Point p = textBox1.PointToScreen(new Point(0, 0));
            label1.Text = Convert.ToString(p.X);
            label2.Text = Convert.ToString(p.Y);
            
            Cursor.Position = new Point(p.X, p.Y);
            LeftClick();

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart