2

Is there a way to use Firefox or Chrome (whichever is installed) in a .NET application? I am not thinking about putting gecko or webkit engine in my application but using the browser installed on the system instead (just like the WebBrowser control uses IE). I heard that it is possible via ActiveX controls but didn't find any more info about it.

devavx
  • 1,035
  • 9
  • 22
Kidades
  • 600
  • 2
  • 9
  • 26
  • 1
    Is the requirement to have the pages render in the desktop application ? Otherwise why not simply put a link that opens in the users default browser – John Sep 13 '13 at 12:42
  • 3
    take a look at http://mono-project.com/WebBrowser this is a nice alternative for the WebBrowser control of microsoft – Jordy van Eijk Sep 13 '13 at 12:49
  • 1
    I am not sure why you would want to do this, but you can try the approach described [here](http://stackoverflow.com/a/758772/69809). I don't see, however, why you would prefer such a path vs having an actual managed web browser control (like the one @Jordy mentioned). – vgru Sep 13 '13 at 12:49
  • @John - yes it is a requirement – Kidades Sep 13 '13 at 13:17
  • @Groo - Because I want my application to be a 20KB executable. That is an interesting approach but how do I control the docked application? – Kidades Sep 13 '13 at 13:19
  • @Kidades: keep in mind that .NET runtime takes up megabytes of RAM for even the simplest of apps. – vgru Sep 13 '13 at 13:33
  • Why won't you use user32.dll in connection with window handle? – Daniel Abou Chleih Sep 13 '13 at 19:43

1 Answers1

2

Well you could use user32.dll to set the parent window of the specified child window (here firefox or chrome).

This is what the result look likes: enter image description here First of all I have two small problems and 1 bigger one:

  1. As you can see firefox is not maximized, therefore some content is missing (still looking for a way to fix that [help appriciated])

  2. Because you have to start the process first and then set the parent of the window, Firefox is running outside of your application for a small amount of time.

  3. The biggest problem: The program your are trying to "bind" to your application mustn't run when running your application, because it cannot set the parent of firefox to your program

MSDN for explanation of the methods: http://msdn.microsoft.com/en-us/library/windows/desktop/ff468919(v=vs.85).aspx

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool repaint);
//hwnd: A handle to the window | x: The new position of the left side of the window. | y: The new position of the top of the window.
//nWidth: The new width of the window. | nHeight: The new height of the window. 
//repaint: Indicates whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs.
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

Process ffprocess = new Process();
        private void openProgramAndSetParent()
        {
            string str = @"C:\Program Files\Mozilla Firefox\firefox.exe"; //Enter full path to Firefox or Chrome

            ffprocess.StartInfo.FileName = str;
            ffprocess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            ffprocess.Start();

            IntPtr ptr = IntPtr.Zero;
            while ((ptr = ffprocess.MainWindowHandle) == IntPtr.Zero)
            {
                Application.DoEvents();
            }
            Thread.Sleep(1000);
            SetParent(ffprocess.MainWindowHandle, panel1.Handle);
            MoveWindow(ffprocess.MainWindowHandle, 0, 0, this.Width, this.Height -100, true);
        }

    private void Form1_Resize(object sender, EventArgs e)
    {
        try
        {
            MoveWindow(ffprocess.MainWindowHandle, 0, 0, this.Width, this.Height, true);
        }
        catch (Exception)
        {}
    }
Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31