-4

I overrided a form(System.Windows.Forms.Form and I will call it Form0)'s CreateParams property like this

protected override CreateParams CreateParams
{
    get
    {
        CreateParams _CreateParams = base.CreateParams;
        _CreateParams.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
        _CreateParams.Parent = IntPtr.Zero;
        return _CreateParams;
    }
}

This window should not be able to be activated(WS_EX_NOACTIVATE) and have no icon shown in the taskbar(WS_EX_TOOLWINDOW).

It works WELL when I use

Application.Run(new Form0());

But it doesn't work as what I expected when I use

Form0.Show()

I want to know why and how to make it take effect while using Show().

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • See [the difference](http://stackoverflow.com/q/20859048/1997232). – Sinatr Dec 13 '16 at 14:01
  • Possible duplicate of [What's the difference between Application.Run() and Form.ShowDialog()?](http://stackoverflow.com/questions/2314514/whats-the-difference-between-application-run-and-form-showdialog) – Yagami Light Dec 13 '16 at 14:02
  • Not sure how either of those questions answer this one. The question is, *where* are you calling `Form0.Show()`? Are you trying to do it in your `main` method? If so, then no, that won't work. – Cody Gray - on strike Dec 13 '16 at 14:03
  • @CodyGray I have another form(I will call it Form1) to put on foreground,and there's an Textbox in Form1,I call Form0.Show() when that Textbox got focus. – Ragnarokkr Xia Dec 13 '16 at 14:06
  • And what happens after you call `Form0.Show()` in the `OnFocus()`? – andlabs Dec 13 '16 at 14:51
  • @andlabs It shows an icon in the taskbar and can be activated – Ragnarokkr Xia Dec 13 '16 at 16:00

1 Answers1

1

You should disable the WS_EX_APPWINDOW style of your window. You don't need to add the WS_EX_TOOLWINDOW. WS_EX_NOACTIVATE takes care of it by default:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams _CreateParams = base.CreateParams;
        _CreateParams.ExStyle |= WS_EX_NOACTIVATE;
        _CreateParams.ExStyle &= (~WS_EX_APPWINDOW); //<----
        _CreateParams.Parent = IntPtr.Zero;
        return _CreateParams;
    }
}