问题描述
- c#winform鼠标双击问题。放在button中可以响应,放在构造函数响应出错
-
双击代码如下。读取txt文件中的字符来获取坐标。假如放在按钮中进行响应没有问题,可以双击打开,放在form的构造函数中就只是把鼠标移动到了坐标位置,但是无法做出双击事件
StreamReader reader = new StreamReader(str0 + str_once, System.Text.Encoding.Default);String str; int[] px; px = new int[2]; int i = 0; while ((str = reader.ReadLine()) != null) { Regex reg = new Regex(@"-?[d]+?[d]+"); Match mm = reg.Match(str); MatchCollection mc = reg.Matches(str); foreach (Match m in mc) { px[i] = int.Parse(m.Value.Trim()); i++; } } reader.Close(); SetCursorPos(px[0], px[1]); System.Threading.Thread.CurrentThread.Join(delay_ms); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); System.Threading.Thread.CurrentThread.Join(delay_ms); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
解决方案
放在Form_Load中,在你的代码最前面加上
this.Show();
Application.DoEvents();
解决方案二:
放在load方法里面,不要放在构造函数里面
解决方案三:
Load中也试过了,同样不行
解决方案四:
贴出错误信息看看再说
解决方案五:
Load添加this.show也不行。这个不是报错,程序没有问题,但是无法做法打开form就正确执行。SetCursorPos(px[0], px[1])移动鼠标执行了,但是mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)这个没有执行。就是鼠标移动到了那个位置,然后form窗口被打开。就没有然后了……
时间: 2024-09-17 03:51:08