【问题标题】:simulate window to focus [duplicate]模拟窗口聚焦[重复]
【发布时间】: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 a WM_CHAR message to a window's message queue,模拟按键到没有焦点的窗口。

标签: c# winapi


【解决方案1】:

最好的解决方案是获取您想要控制的窗口的HWND 并直接通过SendMessagePostMessage

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多