11

I am creating some always-on-top toasts as forms and when I open them I'd like them not to take away focus from other forms as they open. How can I do this?

Thanks

Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
  • 1
    Possible duplicate of [Show a Form without stealing focus?](http://stackoverflow.com/questions/156046/show-a-form-without-stealing-focus) – Breeze Mar 22 '17 at 09:16

2 Answers2

16
protected override bool ShowWithoutActivation
{
    get
    {
        return true;
    }
}

Override this property in your form code and it should do the trick for you.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 6
    It should be noted that this will NOT work if your form has TopMost property set to true, to use this override with a topmost form you must override CreateParams and set WS_EX_TOPMOST on the ExStyle as well. – Aaron Murgatroyd Nov 25 '11 at 14:30
14

It took me a few minutes using Adam's & Aaron's info above, but I finally got it to work for me. The one thing I had to do was make sure the form's Top Most property is set to false. Here is the code I used...

    protected override bool ShowWithoutActivation { get { return true; } }

    protected override CreateParams CreateParams
    {
        get
        {
            //make sure Top Most property on form is set to false
            //otherwise this doesn't work
            int WS_EX_TOPMOST = 0x00000008;
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= WS_EX_TOPMOST;
            return cp;
        }
    }
Mark
  • 141
  • 1
  • 2
  • It should also be noted that all the above works only when the form is shown with Show() -- does not work with ShowDialog() – PaulTheHacker Feb 26 '20 at 08:19