在按钮点击的Windows应用程序中,我需要刷新所有打开的Chrome浏览器实例或至少刷新我的计算机上的“活动”选项卡。
我的代码如下:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
private void btnGetBrowserProcess_Click(object sender, EventArgs e)
{
const UInt32 WM_KEYDOWN = 0x0100;
const int VK_F5 = 0x74;
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
if (chrome.MainWindowHandle != IntPtr.Zero)
{
PostMessage(chrome.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
}
}
}
这适用于IE和Mozilla,但不适用于Chrome
答案 0 :(得分:1)
尝试使用SendInput
命令。不幸的是,这意味着您需要添加更多代码来进行设置,并且您还需要在发送按键之前将焦点设置在chrome窗口上。
两个必需的外部功能是:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
然后所需的structs
是:
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct INPUT
{
public uint Type;
public MOUSEKEYBDHARDWAREINPUT Data;
}
/// <summary>
/// http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/f0e82d6e-4999-4d22-b3d3-32b25f61fb2a
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct MOUSEKEYBDHARDWAREINPUT
{
[FieldOffset(0)]
public HARDWAREINPUT Hardware;
[FieldOffset(0)]
public KEYBDINPUT Keyboard;
[FieldOffset(0)]
public MOUSEINPUT Mouse;
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT
{
public uint Msg;
public ushort ParamL;
public ushort ParamH;
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
public ushort Vk;
public ushort Scan;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
/// <summary>
/// http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/2abc6be8-c593-4686-93d2-89785232dacd
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
public int X;
public int Y;
public uint MouseData;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
然后最终改变你的点击事件:
private void btnGetBrowserProcess_Click(object sender, EventArgs e)
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
if (chrome.MainWindowHandle != IntPtr.Zero)
{
// Set focus on the window so that the key input can be received.
SetForegroundWindow(chrome.MainWindowHandle);
// Create a F5 key press
INPUT ip = new INPUT { Type =1};
ip.Data.Keyboard = new KEYBDINPUT();
ip.Data.Keyboard.Vk = (ushort)0x74; // F5 Key
ip.Data.Keyboard.Scan = 0;
ip.Data.Keyboard.Flags = 0;
ip.Data.Keyboard.Time = 0;
ip.Data.Keyboard.ExtraInfo = IntPtr.Zero;
var inputs = new INPUT[] { ip };
// Send the keypress to the window
SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT)));
// probably need to set focus back to your application here.
}
}
}
我已经为自己尝试了这一点,并且我已打开的当前Chrome标签已成功刷新。我使用以下作为参考来构建此示例:
不幸的是,我没有解释为什么PostMessage不能用于Chrome,但我怀疑它将与它的构建方式有关。