Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent Window (or Draw to screen) No Mouse Capture

Tags:

.net

winforms

In an app I'm coding I would like to make an alert message to appear that displays a large semi-transparent warning message without affecting the users work. Basically I will fade in the message but never set it's opacity to 1 and I want the user to be able to click 'through' the message as if it isn't there.

I have started by using an always on top window and setting the window style to none and setting the background and transparency key to white. On this window there is a label with a large font which contains the alert message (later I'll probably override the paint event and paint the message using GDI). I use a timer to fade in the message by dialling up it's opacity before dialling it back down again. It works OK in that the focus isn't stolen from any apps but the transparent form captures the mouse events, not the form below it (actually most of the form which is transparent doesn't capture the mouse events, only the label text does).

Also I'm not sure it's the optimal approach, maybe I should be painting straight to the screen somehow.

How should I improve things.

like image 309
Christopher Edwards Avatar asked Nov 25 '08 18:11

Christopher Edwards


1 Answers

Override the CreateParams property on your Form class and make sure the WS_EX_NOACTIVATE extended style is set. Mine looks like this:

protected override CreateParams CreateParams
{
  get
  {
    CreateParams baseParams = base.CreateParams;

    baseParams.ExStyle |= ( int )(
      Win32.ExtendedWindowStyles.WS_EX_LAYERED |
      Win32.ExtendedWindowStyles.WS_EX_TRANSPARENT |
      Win32.ExtendedWindowStyles.WS_EX_NOACTIVATE |
      Win32.ExtendedWindowStyles.WS_EX_TOOLWINDOW );

    return baseParams;
  }
}

Values for ExtendedWindowStyles used above are:

WS_EX_LAYERED = 0x00080000,
WS_EX_NOACTIVATE = 0x08000000,
WS_EX_TOOLWINDOW = 0x00000080,
WS_EX_TRANSPARENT = 0x00000020,
like image 137
Martin Plante Avatar answered Oct 31 '22 21:10

Martin Plante