作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用这段代码在控制台中做一些事情:
System.Diagnostics.Process Process = new System.Diagnostics.Process();
Process.StartInfo.FileName = @"MyDir\MyApp.exe";
Process.StartInfo.WorkingDirectory = @"MyDir\MyApp.exe";
Process.Start();
System.Threading.Thread.Sleep(10000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Process.Kill();
打开一个程序然后使用SendKeys
。它由可能单击另一个程序的用户运行,因此它会出现问题,因为焦点可能会设置到另一个程序。
那么,我怎样才能将 key 专门发送到我的应用程序呢?
最佳答案
使用 PInvoke 调用 PostMessage
:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
将进程的 MainWindowHandle
作为第一个 arg 传递,0x0D
作为第二个 - 参见 the full list of virtual key codes here我很确定您可以将 IntPtr.Zero
作为最后两个参数传递。
无论当前焦点在哪个窗口,PostMessage 都会起作用。
关于c# - 专注于一个程序并在 C# 中做一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496480/
我有一个 Collection View 并以这样一种方式排列它,即在屏幕的一侧有一个单独的列,并且根据焦点中的集合项替换内容。 如果对某个项目的关注超过 0.5 秒,我希望能够换出内容。 这是我目前
这是一种经常出现的情况,对我来说永远不会太容易。我想我会问其他人如何处理它。 想象一下,如果 demo=60 命令行参数的处理是这样完成的: if DemoOptionSpecified() {
我是一名优秀的程序员,十分优秀!