none
Open and Close internet browser RRS feed

  • Question

  • I am trying to find a way to open the user's default browser, navigate to a page within that window, and then close the window. I have found ways to do all of these things but I run into a problem when there is more than one browser open at one time. For instance, if there are three Firefox windows open at once...I cannot figure out how to find a specific window so that I perform actions on its process. I am using C#.

    Any help or suggestions would be greatly appreciated. Thanks.
    Monday, July 28, 2008 4:28 PM

Answers

  • You can use the FindWindow API to get the handle for the specified WebBrowser window you need, and call SendMessage API to send WM_CLOSE message to the window to close it, something like this:


            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            [DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr p = FindWindow(null, "Microsoft Corporation - Mozilla Firefox");
                SendMessage(p, 0x10, 0, 0); //WM_CLOSE
            }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Tuesday, August 5, 2008 9:15 AM
    Monday, August 4, 2008 7:53 AM

All replies

  • You can use the FindWindow API to get the handle for the specified WebBrowser window you need, and call SendMessage API to send WM_CLOSE message to the window to close it, something like this:


            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            [DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

            private void button1_Click(object sender, EventArgs e)
            {
                IntPtr p = FindWindow(null, "Microsoft Corporation - Mozilla Firefox");
                SendMessage(p, 0x10, 0, 0); //WM_CLOSE
            }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Tuesday, August 5, 2008 9:15 AM
    Monday, August 4, 2008 7:53 AM
  • Does it really matter which instance of FF you use to display the page?  If the Z-order of the windows is important so that the user actually sees the page, you'll need to use SetForegroundWindow().  That doesn't always work, producing a blinking taskbar button instead.
    Hans Passant.
    Monday, August 4, 2008 12:02 PM
    Moderator
  • Hello....what is the difference between open and close internet browser? Please can anyone tell me in detail.

    Gracie Sh
    http://hdtvlcdplasma.com

    • Edited by Gracie Sh Thursday, August 14, 2008 6:36 AM For changes
    Thursday, August 14, 2008 6:26 AM