C# Process.Start "chrome.exe"/声明高度

标签 c# google-chrome

嗨,我正在尝试使用

process start 'chrome.exe'

它正在工作:

private void btnSTBAutoLogin_Click(object sender, EventArgs e)
{
        try
        {
            System.Diagnostics.Process.Start("CHROME.EXE", "https://www.google.com/chrome/browser/desktop/index.html");
        }
        catch
        {
            System.Diagnostics.Process.Start("IEXPLORE.EXE", "https://www.google.com/chrome/browser/desktop/index.html");
        }
}

问题:

我想在单击 btnSTBAutoLogin 时调整 chrome 页面的大小。

我可以调整 Chrome 页面的大小吗? 例如:

  • 高度:1280
  • 宽度:800

最佳答案

您可以使用MoveWindow - 更改窗口大小和 GetWindowRect - 获取窗口的当前位置。

首先添加此命名空间:

 using System.Diagnostics;
 using System.Runtime.InteropServices;

下一步 - 添加 P/Invoke 代码:

[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref Rect lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
   public int Left;
   public int Top;
   public int Right;
   public int Bottom;
}

最后,在 Process.Start 之后,您可以使用此过程来更改 chrome 窗口大小:

public void SetChromeSize(int width, int height)
{
   var procsChrome = Process.GetProcessesByName("chrome");   // there are always many chrome processes, so we have to find the process with a WindowHandle
   foreach (var chrome in procsChrome)
   {
      if (chrome.MainWindowHandle == IntPtr.Zero)    // the chrome process must have a window
         continue;

      var rct = new Rect();
      GetWindowRect(chrome.MainWindowHandle, ref rct);            // find and use current position of chrome window

      MoveWindow(chrome.MainWindowHandle, rct.Left, rct.Top, width, height, true);        //use MoveWindow to change size and save current position
      break;
   }
}

关于C# Process.Start "chrome.exe"/声明高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32108728/

相关文章:

docker - 调用 puppeteer.connect() 时出现协议(protocol)错误

android - 在安装了 Google Play 的 Genymotion 模拟器中运行 Chrome 应用程序时遇到问题

c# - 错误 CS5001 : exe does not contain a static `Main' method suitable for an entry point

c# - 如何绑定(bind)到 WPF 应用程序中的 subview 模型属性?

c# - Foreach 循环只验证第一个元素

css - 使用 CSS,Google Chrome 可以创建类似于 24 位 PNG 的 alpha channel 效果吗?

android - 如何在 Android 模拟器上运行新的 Chrome for Android 4?

c# - 结构值列表和数组与变量

c# - 如何重定向到 FluentSecurity 中的特定页面?

javascript - Chrome 在加载页面时卡住