0

我在这里找到了这段代码,但是当我尝试将它实现到我的代码中但它不起作用时。我不是专业的编码员,因此向你们寻求帮助。是参考问题吗?还是有其他方法可以让光标单击?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MouseWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

         }
    }
    public class form1 : Grid 
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwflags, long dx, long dy, long cButtons, long 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 static Point Position { get; set; }
        public form1 ()
        {

        }
        public void DoMouseClick()
        {
//This is where the error is, System.Windows.Input.Cursor' does not contain a definition for 'Position' //
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

        }
    }
}
4

1 回答 1

0

在开发 winforms 游戏编辑器时,我遇到了同样的问题。我通过在要从中获取鼠标坐标的表单上创建一个 onMouseMove 事件来修复它。如果您不知道如何制作事件,请转到表单编辑器,单击表单,然后单击属性框中的迅雷。继续并将其命名为 onMouse,然后这里有一些代码来演示如何获得正确的坐标。

 private void onMove(object sender, MouseEventArgs e)
    {
        label1.Text = "X:" + e.X + "Y:" + e.Y;
    }
于 2013-10-22T16:31:41.490 回答