我正在尝试向网络浏览器发送 {PGDN} 信号。这就是我打开网络浏览器的方式
curProcess = Process.Start("chrome.exe", "file:///D:/sample.htm");
我正在尝试获取该窗口的屏幕截图。因为我尝试保存整页图像失败,所以想向下滚动该页面并获取快照。我认为发送“向下翻页”就可以了。但仍然不知道该怎么做。
如何将 key 发送到 curProcess
?
最佳答案
这段代码为您提供了一个很好的解决方案!
Process.Start("chrome.exe", "D:/sample.htm");
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName == "chrome" && p.MainWindowTitle == "sample.htm - Google Chrome")
{
KeyHandle.SendKey(p.MainWindowHandle, Keys.PageDown);
}
}
您也需要这个类(class)
class KeyHandle
{
private static Int32 WM_KEYDOWN = 0x100;
private static Int32 WM_KEYUP = 0x101;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);
public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
{
PostMessage(hWnd, WM_KEYUP, key, 0);
}
}
关于c# - 通过发送信号向下滚动网络浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040086/