通过发送信号向下滚动Web浏览器

时间:2013-11-18 04:27:41

标签: c# winforms api

我正在尝试将{PGDN}信号发送到网络浏览器。这就是我打开网络浏览器的方式

curProcess = Process.Start("chrome.exe", "file:///D:/sample.htm");

我正试图获得该窗口的屏幕截图。因为我没有尝试保存整页图像,想要向下滚动该页面并拍摄快照。我想发送'页面'会做。但是仍然不知道该怎么做。 如何将密钥发送到curProcess

2 个答案:

答案 0 :(得分:2)

此代码为您提供了一个很好的解决方案!

 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 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);
    }


}

答案 1 :(得分:0)

这是另一个使用sendKeys类的答案。

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" &&
                p.MainWindowHandle != IntPtr.Zero)
            {
                KeyHandle.SetForeGround(p.MainWindowHandle);
                SendKeys.Send("{PGDN}");
            }
        }

上课:

class KeyHandle
{
    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    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);
    }
    public static void SetForeGround(IntPtr hWnd)
    {
        SetForegroundWindow(hWnd);
    }


}