.NET 程序的 Sendkeys 问题

发布于 2024-08-28 08:57:47 字数 971 浏览 9 评论 0原文

下面的代码是我从 MSDN 复制的,并做了一些修改:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
int cnt = 0;
private void button1_Click(object sender, EventArgs e)
{
     IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
      if (calculatorHandle == IntPtr.Zero)
      {
          MessageBox.Show("Calculator is not running.");
          return;
      }
      SetForegroundWindow(calculatorHandle);
      SendKeys.SendWait(cnt.ToString());
      SendKeys.SendWait("{ENTER}");
      cnt++;
      SendKeys.Flush();
      System.Threading.Thread.Sleep(1000);
}

问题是记事本中的数字顺序不是连续的。第一次单击的结果始终为 0(如预期)。但从第二次单击开始,结果是不可预测的(但序列仍然按顺序排列,例如 3, 4, 5, 10, 14, 15, ....)

如果我单击按钮的速度足够快,我就能得到结果按连续顺序 (0,1,2,3,4,....) 但有时它会产生 2 个以上相同的数字 (例如 0,1,2,3,3,3,4,5,6, 6,6,7,8,9,...)

THe code below I copied from MSDN with a bit of modification:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
int cnt = 0;
private void button1_Click(object sender, EventArgs e)
{
     IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
      if (calculatorHandle == IntPtr.Zero)
      {
          MessageBox.Show("Calculator is not running.");
          return;
      }
      SetForegroundWindow(calculatorHandle);
      SendKeys.SendWait(cnt.ToString());
      SendKeys.SendWait("{ENTER}");
      cnt++;
      SendKeys.Flush();
      System.Threading.Thread.Sleep(1000);
}

The problem is the number sequence in Notepad is not continuously. The first click always results 0 (as expected). but from the second click, the result is unpredictable (but the sequence is still in order, e.g. 3, 4, 5, 10, 14, 15, ....)

If I click the button fast enough, I was able to get the result in continuous order (0,1,2,3,4,....) but sometimes it produces more than 2 same numbers (e.g. 0,1,2,3,3,3,4,5,6,6,6,7,8,9,...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-09-04 08:57:47

SetForegroundWindow 不会等到指定的窗口实际位于前台。它只是“启动”该过程。因此,您的 SendKeys.SendWait 很可能没有将密钥发送到您期望的窗口。

另一个与您所看到的不太相关的问题是您在事件处理程序中调用了 Thread.Sleep 。这通常被认为是一种不好的做法:您不应该阻塞 UI 线程。它使您的应用程序显得无响应。

The SetForegroundWindow is not going to wait until the specified window is actually in the foreground. It just "kicks off" the process. So it's quite possible that your SendKeys.SendWait is not sending the key to the window that you expect it to be.

Another issue, not quite related to what you're seeing is that you've got a call to Thread.Sleep in your event handler. That's generally considered to be a bad practise: you shouldn't be blocking your UI thread. It makes your application appear unresponsive.

蓝海似她心 2024-09-04 08:57:47

第一个问题的发生是因为 SetForegroundWindow 可以在焦点切换之前返回,因此 Sendkeys 可能会在记事本未处于活动状态时运行。

The first problem happens because SetForegroundWindow can return before the focus was switched, so Sendkeys might run when notepad is not active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文