2

Hi I am trying to use

process start 'chrome.exe'

It is working:

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

Question:

I wanna resize the chrome page when btnSTBAutoLogin clicked.

Could I resize chrome page? ex:

  • height : 1280
  • width : 800
Artem Kulikov
  • 2,250
  • 19
  • 32
Gorden
  • 65
  • 1
  • 6

1 Answers1

2

You can use MoveWindow - to change window size and GetWindowRect - to get current position of window.

First of all add this namespaces:

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

Next - add P/Invoke code:

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

Finally after your Process.Start you can use this procedure, which changes chrome window size:

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;
   }
}
Artem Kulikov
  • 2,250
  • 19
  • 32
  • I adjust this code. But when I first click the button, SetChromeSize doesn't work. So I use delay time. Thanks for your reply! – Gorden Aug 21 '15 at 06:37