C#拖动鼠标

时间:2018-03-20 00:08:09

标签: c#

我试图创建一个程序,当我点击左键单击它时拖动鼠标,而不是我将鼠标向下移动游戏,但它根本不会移动游戏。它将光标移动到游戏之外,但不会在游戏中移动。

    public static Point Position { get; set; }
    public Anti_Recoil()
    {
        InitializeComponent();
    }

    private void Anti_Recoil_Load(object sender, EventArgs e)
    {
        this.BackColor = Color.Wheat;
        this.TransparencyKey = Color.Wheat;
        this.TopMost = true;

        int initialStyle = GetWindowLong(this.Handle, -20);
        SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);



        PointConverter pc = new PointConverter();

        Point pt = new Point();

        pt = (Point)pc.ConvertFromString("765, 500");

        Cursor.Position = pt;
    }

    private void Anti_Recoil_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Z)
            Hide();
    }

1 个答案:

答案 0 :(得分:0)

使用我的班级

  

首先,将我的课程添加到您的项目中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Input;

namespace Hector.Framework.Utils
{
    public class Peripherals
    {
        public class Mouse
        {
            public static int X
            {
                get => Cursor.Position.X;
            }

            public static int Y
            {
                get => Cursor.Position.Y;
            }

            public static void MoveToPoint(int X, int Y)
            {
                Win32.SetCursorPos(X, Y);
            }

            public static void Hide()
            {
                Cursor.Hide();
            }

            public static void Show()
            {
                Cursor.Show();
            }

            public static bool IsHidden
            {
                get => Cursor.Current == null;
            }

            /// <summary>
            /// ButtonNumber: 0-None 1-Left 2-Middle 3-Right 4-XButton1 5-XButton2
            /// </summary>
            public static bool MouseButtonIsDown(int ButtonNumber)
            {
                bool outValue = false;
                bool isLeft = Control.MouseButtons == MouseButtons.Left;
                bool isRight = Control.MouseButtons == MouseButtons.Right;
                bool isMiddle = Control.MouseButtons == MouseButtons.Middle;
                bool isXButton1 = Control.MouseButtons == MouseButtons.XButton1;
                bool isXButton2 = Control.MouseButtons == MouseButtons.XButton2;

                switch (ButtonNumber)
                {
                    case 0:
                        outValue = false;
                        break;
                    case 1:
                        outValue = isLeft;
                        break;
                    case 2:
                        outValue = isMiddle;
                        break;
                    case 3:
                        outValue = isRight;
                        break;
                    case 4:
                        outValue = isXButton1;
                        break;
                    case 5:
                        outValue = isXButton2;
                        break;
                }

                return outValue;
            }

            /// <summary>
            /// This function is in Alpha Phase
            /// </summary>
            /// <param name="FocusedControl">The control that is scrolled; If the control has no focus, it will be applied automatically</param>
            /// <param name="FontSize">This is used to calculate the number of pixels to move, its default value is 20</param>
            static int delta = 0;
            static int numberOfTextLinesToMove = 0;
            static int numberOfPixelsToMove = 0;
            public static bool GetWheelValues(Control FocusedControl, out int Delta, out int NumberOfTextLinesToMove, out int NumberOfPixelsToMove, int FontSize = 20)
            {
                try
                {
                    if (FocusedControl == null) throw new NullReferenceException("The FocusedControl can not bel null");
                    if (!FocusedControl.Focused) FocusedControl.Focus();
                    FocusedControl.MouseWheel += (object sender, MouseEventArgs e) =>
                    {
                        delta = e.Delta;
                        numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
                        numberOfPixelsToMove = numberOfTextLinesToMove * FontSize;
                    };

                    Delta = delta;
                    NumberOfTextLinesToMove = numberOfTextLinesToMove;
                    NumberOfPixelsToMove = numberOfPixelsToMove;

                    return true;
                }
                catch
                {
                    Delta = 0;
                    NumberOfTextLinesToMove = 0;
                    NumberOfPixelsToMove = numberOfPixelsToMove;

                    return false;
                }
            }

            private class Win32
            {
                [DllImport("User32.Dll")]
                public static extern long SetCursorPos(int x, int y);

                [DllImport("User32.Dll")]
                public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

                [StructLayout(LayoutKind.Sequential)]
                public struct POINT
                {
                    public int x;
                    public int y;
                }
            }
        }
    }
}

然后你需要检查鼠标的状态,

bool MouseLeftButton = Hector.Framework.Utils.Peripherals.Mouse.MouseButtonIsDown(1);

试试这个:

private void Form2_Load(object sender, EventArgs e)
{
    Hector.Framework.Utils.Peripherals.Mouse.MoveToPoint(300, 0); //Move the cursor to Top
}

//Then use timer to move the cursor
int a = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    Hector.Framework.Utils.Peripherals.Mouse.MoveToPoint(300, a += 1);
}

if(MouseLeftButton)
{
   timer1.Start(); //timer is initialized
}