1

I want to be able to start a default web browser of a client and to be able to minimize and maximize its window as I need it.

Currently if I do this:

var proc = Process.Start("http://www.google.com");

proc is null.

And if I do this:

var proc = new Process();
proc.StartInfo.FileName = "http://www.google.com";
proc.Start();

The proc isn't null, but when I try to minimize it, or maximize it, i get an exception:

No process is associated with this object.

This is my current code:

public class WebPageHelper
{
    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;
    private Process currentProcess;


    public WebPageHelper(String url)
    {
        currentProcess = new Process();
        currentProcess.StartInfo.FileName = url;
        currentProcess.Start();
    }

    public void MaximizeWebPage()
    {
        IntPtr hWnd = currentProcess.MainWindowHandle;
        if (!hWnd.Equals(IntPtr.Zero))
        {
            ShowWindowAsync(hWnd, SW_SHOWMAXIMIZED);
        }
    }

    public void MinimizeWebPage()
    {
        IntPtr hWnd = currentProcess.MainWindowHandle;
        if (!hWnd.Equals(IntPtr.Zero))
        {
            ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
        }
    }

    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
}

Is there any solution to this?

Phoenix
  • 1,045
  • 1
  • 14
  • 22
Nikola.Lukovic
  • 1,256
  • 1
  • 16
  • 33
  • And the exception is?? – Steve Feb 02 '15 at 11:04
  • @Nikola.Lukovic Don't add it as a screenshot. Write the exception message as a text. – Soner Gönül Feb 02 '15 at 11:05
  • added the exception text – Nikola.Lukovic Feb 02 '15 at 11:08
  • What is the default web browser in your PC?. Probably is something related at how your particular browser starts its process (more than one?) – Steve Feb 02 '15 at 11:10
  • it's chrome, and when i try this out chrome isn't started so it can't use it's resources – Nikola.Lukovic Feb 02 '15 at 11:11
  • 1
    This is problematic because once the browser window is open, the user will think of it as "theirs" (based on usual experience of browser windows opened by programs) - they may open additional tabs, etc. They'll then probably be quite annoyed if your program starts messing around with "their" window. Why does it have to be the default browser and/or why does your program have to then keep control of it? – Damien_The_Unbeliever Feb 02 '15 at 11:16
  • 1
    Not your answer, but probably you could get an idea of the complexity behind this simple kind of **Control a different Process** question. http://stackoverflow.com/questions/16958051/get-chrome-browser-title-using-c-sharp/16958798#16958798 – Steve Feb 02 '15 at 11:18
  • basically, i'm working on a voip application. my part of a job is to create a desktop client that will work from a system tray and which will notify that an external call is being made. the client would click on a notification and a web browser with a js client would open where a client can join the external call with an internal phone. the browser would than remain open. when a next call is being made it would be maximized. this is one option, the other would be to create a simple wrapper for a chromium api in a simple window – Nikola.Lukovic Feb 02 '15 at 11:24

0 Answers0