开发者

How to use an invisible System.Windows.Forms.WebBrowser?

I tried to run it without a container form, but the DocumentCompleted event doesn't fire.

I tried to run it in a Form with opacity set to 0% but the开发者_运维技巧 process isn't completely hidden, since it appears to the user when he uses Alt+Tab

I don't mind if the proccess appears on the Task Manager though.


I'm guessing you're trying to do some automated task like scrapping data or some such. In that case you might want to look at this question and the provided answer:

Using BrowserSession and HtmlAgilityPack to login to Facebook through .NET

Basically it shows how to use a headless browser to load HTML pages and interact with them. It's a better solution than automating a WebBrowser control.


To prevent the window from being shown, paste this code into your form:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
        }
        base.SetVisibleCore(value);
    }

Beware that the Load event won't run until you explicitly make your form visible so move any code you've got there inside the if statement.

Not getting the DocumentCompleted event to run is usually caused by not running a message loop (Application.Run). WebBrowser requires one, and a thread that's marked with [STAThread], in order to fire its events. The message loop is very important for COM components.

Is it also important to prevent the invisible form from stealing focus, with the code below:

protected override bool ShowWithoutActivation
{
    get { return true; } // prevents form creation from stealing focus
}

and

form1.Enabled = false; // prevents inner controls from stealing focus


Set ShowInTaskbar to false, FormBorderStyle to None, and ControlBox to false.


It is accomplished with 3 steps:

  1. To hide the Form use Opacity = 0
  2. To hide from the taskbar use ShowInTaskbar = false
  3. To hide from Alt+Tab use the following code

.

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr window, int index, int
    value);

    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr window, int index);

    const int GWL_EXSTYLE = -20;
    const int WS_EX_TOOLWINDOW = 0x00000080;

    public Form1()
    {
        InitializeComponent();
        int windowStyle = GetWindowLong(Handle, GWL_EXSTYLE);
        SetWindowLong(Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);
    }
}

Or as suggested:

public partial class Form1 : Form
{
    const int WS_EX_TOOLWINDOW = 0x00000080;

    protected override CreateParams CreateParams
    {
        get
        {
            var createParams = base.CreateParams;
            createParams.ExStyle |= WS_EX_TOOLWINDOW;
            return createParams;
        }
    }
}


Have you tried combining a 0% opacity hidden (Form.Hide()) Form with it's ShowInTaskbar property set to false?

It's been a while, but I believe ShowInTaskbar = false will also hide the window from Alt-Tab

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜