Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

.net - Transparent Window (or Draw to screen) No Mouse Capture

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...