2010-06-15 144 views
28

我想模拟F5在我的c#程序中按键,当IE打开时我想能够自动刷新我的网站,我不知道从哪里开始。模拟键按c#

谢谢,

丹妮。

+0

这是一个web应用或一个桌面应用程序? – ckramer 2010-06-15 17:07:22

+0

一个普通的桌面应用程序。 – Dani 2010-06-15 17:08:35

+0

在这种情况下,我认为Bondy先生已经击中了头部。 – ckramer 2010-06-15 17:10:05

回答

47

下面是一个例子...

static class Program 
{ 
    [DllImport("user32.dll")] 
    public static extern int SetForegroundWindow(IntPtr hWnd); 

    [STAThread] 
    static void Main() 
    { 
     while(true) 
     { 
      Process [] processes = Process.GetProcessesByName("iexplore"); 

      foreach(Process proc in processes) 
      { 
       SetForegroundWindow(proc.MainWindowHandle); 
       SendKeys.SendWait("{F5}"); 
      } 

      Thread.Sleep(5000); 
     } 
    } 
} 

一个更好的......是烦人少...

static class Program 
{ 
    const UInt32 WM_KEYDOWN = 0x0100; 
    const int VK_F5 = 0x74; 

    [DllImport("user32.dll")] 
    static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam); 

    [STAThread] 
    static void Main() 
    { 
     while(true) 
     { 
      Process [] processes = Process.GetProcessesByName("iexplore"); 

      foreach(Process proc in processes) 
       PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0); 

      Thread.Sleep(5000); 
     } 
    } 
} 
+2

这个(尤其是“更好”的)帮助了我,+1。看到我的答案[这里](http://stackoverflow.com/a/16255097/385995)看到一个更复杂的例子,也涉及修饰键。 – 2013-04-27 20:13:03

9

您可以使用Win32 API FindWindowFindWindowEx找到打开的浏览器的窗口句柄,然后就打电话SendMessageWM_KEYDOWN。通常只需将窗口标题传递到FindWindowEx并让它找到相关的窗口句柄即可。

如果您通过Process process对象自己启动浏览器进程,则可以使用process.MainWindowHandle而不是调用FindWindowEx

当你想开始使用其他窗口时,Spy ++是一个非常有用的工具。它基本上允许您学习另一个程序的UI元素的层次结构。您还可以监控进入您正在监控的窗口的所有消息。我在this thread有更多信息。

的F5按键拥有该虚拟关键代码:

const int VK_F5 = 0x74; 

在P/C#中调用签名FindWindowEx是:

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 

您可以P/Invoke(带来)在Win32 API SendMessage这样的:

[DllImport("user32.dll", CharSet = CharSet.Auto)] 
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 

因此,要回顾一下,你叫FindWindowEx直接从你的C#代码上面的代码在你的班级内的某个地方。 FindWindowEx将返回一个窗口句柄。然后,一旦拥有窗口句柄,就可以将任何击键发送到窗口,或者调用窗口句柄上的许多其他Win32 API调用。甚至通过使用FindWindowEx的另一个电话找到子窗口。例如,您可以选择浏览器的编辑控件,然后更改它的文本。

如果一切都出错了,并且您认为您正在向窗口发送正确的密钥,则可以使用spy++来查看当您手动将焦点置于浏览器并手动按下F5时发送到窗口的消息。

+0

请问您能否更详细一点,我真的不知道如何使用Win32 API。 – Dani 2010-06-15 17:10:18

+1

到SO线程的链接非常完美。但也可能这个片段可能会有所帮助 - http://snippets.dzone.com/posts/show/4070 – Reddog 2010-06-15 17:13:43

+0

@Dani:我添加了更多信息,但是您可以使用lang:c#搜索Google代码搜索以获取更详细的示例。 http://www.google.com/codesearch?q=lang:c%23+FindWindowEx&hl=zh-CN&btnG=Search+Code – 2010-06-15 17:14:52

4

另一种模拟F5按键的替代方法是将窗口窗体应用程序中的WebBrowser控件简单托管。您使用WebBrowser.Navigate方法加载您的网页,然后使用标准Timer和您刚刚重新导航到将重新加载页面的URL的每个计时器的滴答。

+0

其非常简单的解决方案。谢谢 – Dani 2010-06-15 17:41:40

2

将KeyStrokes发送(模拟)到任何窗口的最简单方法是使用.NET Framework的SendKeys.Send方法。

结帐这种非常直观的MSDN文章http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx


特别是对于你的情况,如果你的浏览器窗口中的焦点,发送F5将只涉及到下面的代码行:

SendKeys.Send("{F5}"); 
+0

SendKeys未定义 – 2017-03-24 18:21:28

4

简单一,加主之前

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)] 
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
[DllImport("USER32.DLL")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

代码insid E MAIN /方法:

string className = "IEFrame"; 
string windowName = "New Tab - Windows Internet Explorer"; 
IntPtr IE = FindWindow(className, windowName); 
if (IE == IntPtr.Zero) 
{ 
    return; 
} 
SetForegroundWindow(IE); 
InputSimulator.SimulateKeyPress(VirtualKeyCode.F5); 

注:

  1. 添加InputSimulator作为参考。要下载Click here

  2. 要查找类&窗口名称,请使用WinSpy ++。要下载Click here