【发布时间】:2009-10-18 12:30:28
【问题描述】:
我有一个带有组合框的应用程序,其中包含当前正在运行的应用程序的名称。正如我从 msdn 库中了解到的,SendKeys 方法只能将密钥发送到活动应用程序。在.NET中是否有可能将密钥也发送到非活动应用程序?或者至少在 WinAPI 中?
【问题讨论】:
-
不是重复的,因为它专门询问非活动应用程序
我有一个带有组合框的应用程序,其中包含当前正在运行的应用程序的名称。正如我从 msdn 库中了解到的,SendKeys 方法只能将密钥发送到活动应用程序。在.NET中是否有可能将密钥也发送到非活动应用程序?或者至少在 WinAPI 中?
【问题讨论】:
您可以使用SendMessage() API 函数将击键发送到非活动窗口。
【讨论】:
使用 C#
如你所愿,无需成为活动窗口。
还有here 一个有用的虚拟键代码列表
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
const int WM_SYSKEYDOWN = 0x0104;
const int VK_KEY_A = 0x41;
IntPtr WindowToFind = FindWindow(null, "Window Name");
//In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy
PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0);
//PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
}
【讨论】: