vdk*_*vdk 59
这是一个例子......
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
一个更好的...不那么讨厌......
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Win32 API FindWindow或FindWindowEx查找打开的浏览器的窗口句柄,然后使用WM_KEYDOWN调用SendMessage.通常,最简单的方法是将窗口标题传递给它并让它为您找到相关的窗口句柄.FindWindowEx
如果您通过Process process对象自己启动浏览器进程,则可以使用process.MainWindowHandle而不是调用FindWindowEx.
当你想开始使用其他窗口时,Spy ++是一个非常有用的工具.它基本上允许您学习另一个程序的UI元素层次结构.您还可以监控进入您正在监控的窗口的所有消息.我在这个帖子中有更多信息.
F5击键有这个虚拟键码:
const int VK_F5 = 0x74;
Run Code Online (Sandbox Code Playgroud)
FindWindowExC#中的p/invoke签名是:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
Run Code Online (Sandbox Code Playgroud)
您可以SendMessage像这样p/invoke(引入)Win32 API :
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
Run Code Online (Sandbox Code Playgroud)
所以回顾一下,FindWindowEx在类中的某个地方使用上面的代码后,可以直接从C#代码中调用. FindWindowEx将返回一个窗口句柄.然后,一旦有了窗口句柄,就可以向窗口发送任何击键,或者在窗口句柄上调用许多其他Win32 API调用.甚至可以通过另一个电话来找到一个子窗口FindWindowEx.例如,您可以选择浏览器的编辑控件,然后更改它的文本.
如果所有其他方法都出错并且您认为您正在向窗口发送正确的密钥,则可以spy++在手动将焦点设置到浏览器并手动按下时查看发送到窗口的消息F5.
使用 mouse_event 或 keybd_event。他们说不再使用它们,但你根本不必找到窗户。
using System;
using System.Runtime.InteropServices;
public class SimulatePCControl
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);
private const int VK_LEFT = 0x25;
public static void LeftArrow()
{
keybd_event(VK_LEFT, 0, 0, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
虚拟键代码在这里:http ://www.kbdedit.com/manual/low_level_vk_list.html
也适用于鼠标:
using System.Runtime.InteropServices;
using UnityEngine;
public class SimulateMouseClick
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public static void Click()
{
//Call the imported function with the cursor's current position
uint X = (uint)0;
uint Y = (uint)0;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Debug.LogError("SIMULATED A MOUSE CLICK JUST NOW...");
}
//...other code needed for the application
}
Run Code Online (Sandbox Code Playgroud)
将KeyStrokes发送(模拟)到任何窗口的最简单方法是使用.NET Framework的SendKeys.Send方法。
查看这篇非常直观的MSDN文章http://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.aspx
特别是对于您的情况,如果您的浏览器窗口处于焦点位置,则发送F5仅涉及以下代码行:
SendKeys.Send("{F5}");
Run Code Online (Sandbox Code Playgroud)
另一种模拟F5按键操作的替代方法是在 Window Forms 应用程序中简单地承载WebBrowser控件。您使用WebBrowser.Navigate方法加载网页,然后使用标准计时器,在计时器的每个滴答声中,您只需重新导航到将重新加载页面的 url。
简单的一个,在Main之前添加
[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);
Run Code Online (Sandbox Code Playgroud)
主要/方法内的代码:
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);
Run Code Online (Sandbox Code Playgroud)
注意: