views:

95

answers:

5

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.

A: 

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

STW
maybe combining Form.Hide() with ShowInTaskbar = false? I'm pretty sure I've done it before without having to resort to P/Invoke calls
STW
`ShowInTaskbar = false` doesn't hide it from Alt+Tab in Windows 7
Jader Dias
When should I call `this.Hide()`? I tried in the constructor and in the Load event, without success
Jader Dias
+3  A: 

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.

ShaderOp
I didn't want to learn a new API, but your solution will do for me. I am still waiting for someone to come with a `WebBrowser` based solution though
Jader Dias
You should avoid `WebBrowser` at all costs. It will be an order of magnitude slower (or worse), and far more fragile.
SLaks
@Jader: I totally understand your concern. But I did try to do some UI automation using the WebBrowser control, and I found it to be very unreliable, although I can't remember the details. I agree with SLaks that you should try to avoid it if possible.
ShaderOp
+1 @SLaks -- WebBrowser is a wrapper around `IWebBrowser2` from `shdocvw.dll`, and it's a poor wrapper at that. My experiences with it were that, without a good amount of extra extending a plumbing it barely functions as a browser for any site beyond basic HTML
STW
The problem with BrowserSession is that it doesn't parse nor executes Javascript.
Jader Dias
@Jader: I wasn't suggesting using the `HtmlAgilityPack` or `BrowserSession`. See my answer to the question linked to above.
ShaderOp
+1  A: 

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

SLaks
It still appears on `Alt+Tab` on Windows 7
Jader Dias
+1  A: 

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;
        }
    }
}
Jader Dias
You should override the `CreateParams` property instead of the P/Invoke calls.
SLaks
@SLaks thanks, I edited to include your solution
Jader Dias
+1  A: 

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
Hans Passant
@Hans please note that I added a fix to your solution
Jader Dias
How could it steal the focus when it is prevented from becoming visible in the first place?
Hans Passant
@Hans I don't know how, it just happens, and it is annoying
Jader Dias
Come to think of it, I have seen at least one other thread that mentioned a focus-stealing problem with IE. Don't remember the exact details. Let's leave it in the answer.
Hans Passant