views:

315

answers:

3

I have a dialog implemented in WinForms that is shown as a notify dialog on the bottom right of the screen. The problem is that whenever is shown it takes the focus and this happens only when TopMost = true. How can I solve this?

+1  A: 

Show the dialog with Show instead of ShowDialog. ShowDialog will be topmost, user has to click it before doing something else (modal) Show will show it as normal.

PoweRoy
I open it using Show() method already
Cornel
A: 

How about this strategy:

  1. Show it at startup, then immediately:
  2. Hide it with ShowWindow( SW_HIDE )
  3. Never ever close the form, just let it be invisible
  4. Show it with ShowWindow( SW_SHOWNOACTIVATE )
danbystrom
+1  A: 

You need to inherit from Form and override a couple of properties:

[Flags]
enum WS_EX
{
    TOPMOST = 0x00000008,
}

class TopMostForm : Form
{
    protected override CreateParams CreateParams
    {
        get
        {
            var baseParams = base.CreateParams;
            baseParams.ExStyle |= (int)WS_EX.TOPMOST;
            return baseParams;
        }
    }

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

Then just simply Show() on this form and it will be displayed as topmost and inactive.

Konstantin Spirin
It does not work if TopMost=true. Works only when TopMost=false.
Cornel
Ups, I think it is shown TopMost even if TopMost=false. Right?
Cornel
Window style WS_EX_TOPMOST makes your window top-most. If you look at `Form.TopMost` property in Reflector, you'll see that it uses different approach that makes your window active.
Konstantin Spirin