【发布时间】:2014-09-23 11:22:57
【问题描述】:
如何模拟一个外来窗口作为焦点窗口? 我需要这个,因为我想将密钥发送到记事本窗口,并且它仅在记事本窗口具有焦点时才有效。但我不想失去对自己窗口的关注。
public IntPtr Find(string taskName, string windowName)
{
return Fenster.FindWindow(taskName, windowName);
}
public void Send(IntPtr hwnd, string text)
{
if (!hwnd.Equals(IntPtr.Zero))
{
SendKeys.Send(text);
}
}
public bool SetForeground(IntPtr hwnd)
{
return SetForegroundWindow(hwnd);
}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
非常感谢!
【问题讨论】:
-
你不能,或者至少你不想。将正确的消息发送到记事本,不要使用 SendKeys。
-
是否有其他方法可以不使用 Sendkeys 发送密钥?
-
现在您问对了问题。见重复。 :) 使用
SendMessage()你可以send aWM_CHARmessage to a window's message queue,模拟按键到没有焦点的窗口。