在记事本中记录我的鼠标坐标 [英] Record my mouse coordinates in notepad

查看:142
本文介绍了在记事本中记录我的鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在记录记事本中鼠标光标的坐标。但是在我的程序中发生的事情是我的光标的最后位置将是唯一记录的位置。我想要发生的是记录它以前的位置坐标和现在。



我尝试过:



i使用此代码

I am currently working on recording the coordinates of my mouse cursor in the notepad. But what happens in my program is that the last position of my cursor will be the only one recorded. What i would want to happen is to record all it's previous position coordinates and the present.

What I have tried:

i used this code

Point pos = PART_Cursor.GetPosition(PART_LoadedImageDisplay);
Point prev = PART_Cursor.GetPreviousPosition(PART_LoadedImageDisplay);
Draw(prev, pos, _pastCursorPosition);
_pastCursorPosition = prev;
            
textbox.Text = pos.X.ToString();
textbox2.Text = pos.Y.ToString();

using (System.IO.StreamWriter objWriter = new System.IO.StreamWriter("users.txt"))
{
    objWriter.WriteLine("X=" + textbox.Text + " " + "Y= " + textbox2.Text);
}

推荐答案

每次不附加时都会覆盖文件。



ref:如何:将文本写入文件| Microsoft Docs [ ^ ]



TL; DR,使用:

You are overwriting the file each time as you are not appending.

ref: How to: Write Text to a File | Microsoft Docs[^]

TL;DR, use:
using (System.IO.StreamWriter objWriter = new StreamWriter("users.txt", append: true))


使用默认选项创建StreamWriter时,它会删除任何现有文件并创建一个新文件。

您可以更改代码以创建通过向其添加 true 参数来追加StreamWrite: StreamWriter构造函数(String,Boolean)(System.IO) [ ^ ]

B更好的解决方案是使用File.AppendAllText: File.AppendAllText Method(字符串,字符串)(System.IO) [ ^ ]

When you create a StreamWriter using the default options, it deletes any existing file and creates a new one.
You could change your code to create the StreamWrite for Append by adding a true parameter to it: StreamWriter Constructor (String, Boolean) (System.IO)[^]
But a better solution would be to use File.AppendAllText: File.AppendAllText Method (String, String) (System.IO)[^]
File.AppendAllText("users.txt", "X=" + textbox.Text + " " + "Y= " + textbox2.Text);


这篇关于在记事本中记录我的鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆