简体   繁体   English

c#SendKey到一个隐藏的窗口

[英]c# SendKey to a hidden window

I have a window which is hidden and I would like to send a keypress to it. 我有一个隐藏的窗口,我想向它发送一个按键。 An example of what I'm trying to achieve is an app that will send the key F5 to a web browser which wasn't the active window. 我要实现的一个示例是一个应用程序,该应用程序会将键F5发送到不是活动窗口的Web浏览器。 The web browser would know to refresh the current page when the F5 keystroke is received. 当收到F5按键时,Web浏览器将知道刷新当前页面。

I would also like to send a combination of keys to an application, eg Ctrl+S. 我还想将按键组合发送到应用程序,例如Ctrl + S。 One example of this usage could be a timed auto-save feature to use with applications which don't have autosave. 这种用法的一个例子可能是定时自动保存功能,可用于没有自动保存功能的应用程序。 This would spare me having to remember to save every 5 mins. 这将使我不必记住每5分钟保存一次。

C# is my technology, does this sound realistic? C#是我的技术,听起来很现实吗?

这篇CodeProject文章显示了如何将击键发送到外部应用程序(使用C#源代码)。

You can use WinApi for this purpose. 您可以为此使用WinApi。 SendMessage or PostMessage method to send desired message to your application. SendMessagePostMessage方法将所需的消息发送到您的应用程序。

Here's C# definition of SendMessage 这是SendMessage的C#定义

[DllImport("user32.dll")]
public static extern int SendMessage(
    int  hWnd,     // handle to destination window
    uint Msg,      // message
    long wParam,   // first message parameter
    long lParam    // second message parameter
  );

and define the message that you want to send like: 并定义您要发送的消息,例如:

public const uint %WM_MESSAGE_HERE% = %value%; 

Check PInvoke - great resource containing WinApi method definitions for .NET 检查的PInvoke -含WINAPI方法定义为.NET巨大的资源

这应该可行,请访问www.pinvoke.net并搜索sendinput ,该页面应告诉您所有您需要了解的内容,以及如何查找窗口(查找findwindow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM