gpt4 book ai didi

c# - 专注于一个程序并在 C# 中做一些事情

转载 作者:行者123 更新时间:2023-11-30 18:46:38 25 4
gpt4 key购买 nike

我使用这段代码在控制台中做一些事情:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com