根据给定的坐标移动鼠标

Moving the mouse according to the given coordinates

我想要的是,在记录鼠标移动并保存坐标/索引/位置之后,我将不得不加载鼠标坐标并使鼠标根据加载的坐标移动 我没有代码可以给你看,因为我卡在了这一点上

     ' private void button3_Click_1(object sender, EventArgs e)
        {
            StreamWriter writer;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string[] cvsArray = new string[10000];
            saveFileDialog1.Filter = "All files (*.All)|*.All|All files (*.*)|*.*";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                writer = File.CreateText(saveFileDialog1.FileName);
                // if ((myStream = saveFileDialog1.OpenFile()) != null)
                //{
                for (int i = 0; i < cvsArray.Length; i++)
                {
                    string text = richTextBox1.Text;
                    writer.WriteLine(text);
                }
                // Code to write the stream goes here.
                writer.Close();
                //}
            }
        }
 private void button11_Click(object sender, EventArgs e)
        {
            StreamReader reader;
            string Line;
            string[] cvsArray;
            string position;

            //set the filter to dialog control
            openFileDialog1.Filter = FILTER;
            //check if the user selected the right file
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //open the selected file
                reader = File.OpenText(openFileDialog1.FileName);
                //Read the entireline form the file
                Line = reader.ReadLine();
                //read while it is still not the end of the file 
                while (!reader.EndOfStream)
                {
                    Line = reader.ReadLine();
                    //Splite the line using array
                    cvsArray = Line.Split(':');

                    position = cvsArray[0];
                    //position = cvsArray[1];
                    listBox1.Items.Add(position);
                }
            }
        }'

这是一个快速但相当肮脏的解决方案:

让我们从几个变量开始:

 List<Point> points = null;
 Timer tt = null;
 int index = 0;

现在有一个开始重新编码的按钮;它初始化一个 List<Point> 来收集位置并创建并启动一个 Timer 以及 lambda 代码来在 Timer.Tick 事件中进行重新编码:

private void btn_Record_Click(object sender, EventArgs e)
{
    points = new List<Point>();
    index = 0;
    tt = new Timer()
    { Interval = 50, Enabled = true };
    tt.Tick += (ss, ee) => 
    {
        if (!points.Any() || points.Last() != Control.MousePosition) 
           points.Add(Control.MousePosition); 
    };
}

下一个停止录制的按钮:

private void btn_Stop_Click(object sender, EventArgs e)
{
    if (tt!=null) tt.Stop();
}

最后是重播按钮;它使用索引在新的 Timer.Tick 代码中循环遍历点集合,但使用相同的计时器:

private void btn_Replay_Click(object sender, EventArgs e)
{
    index = 0;
    tt = new Timer() { Interval = 50, Enabled = true };
    tt.Tick += (ss, ee) =>
    {
        if (index < points.Count)
          { System.Windows.Forms.Cursor.Position =  points[index++]; }
        else tt.Stop();
}

一些注意事项:

  • 如问题中所问,这将记录并回复鼠标坐标。它将以固定的间隔进行,因此回放看起来与原始动作非常相似;事实上很难区分,所以我添加了一个间隔较长的慢动作按钮来演示..(但是gif太大了)

  • 该代码将在屏幕坐标中记录鼠标位置,并且无论它走到哪里都应该捕获它们,而不仅仅是在您的应用程序中。看看 VS 如何激活代码放大镜!

  • 它不会记录任何其他鼠标事件,如向上、向下、单击、双击或滚轮。对于那些你需要一个全局鼠标钩子来捕获和一些外部调用来重播。

  • 要包含其他鼠标事件,您还需要一个不同的扩展数据结构;而且您还必须从计时器驱动模型转到鼠标事件驱动模型。

  • 该示例使用按钮来启动和停止。当然,进出这些按钮的运动会包含在记录的位置列表中。相反,可以使用定时启动,几秒钟后开始录制,几秒钟不活动后停止录制..

您可以通过多种方式保存和加载积分;最简单的一种是序列化为xml;使用 string path = @"..." 它可能看起来像这样简单:

private void btn_save_Click(object sender, EventArgs e)
{
    if (points == null) points = new List<Point>();
    XmlSerializer xs = new XmlSerializer((points).GetType());
    using (TextReader tr = new StreamReader(path))
    {
        points =  (List<Point>)xs.Deserialize(tr);
        tr.Close();
    }
}

private void btn_load_Click(object sender, EventArgs e)
{
    XmlSerializer xs = new XmlSerializer((points).GetType());
    using (TextWriter tw = new StreamWriter(path))
    {
        xs.Serialize(tw, points);
        tw.Close();
    }
}

其他方法是使用二进制格式化程序或自定义转换例程。 Xml比较稳定。

这是一个短片: