Windows Forms: clicks through a partially transparent always-on window

I design a window that is always on the screen and about 20% opaque. It is designed to be a kind of status window, so it’s always at the top, but I want people to be able to click on the window in any other application below. Here is an opaque window located on top of this SUCH message that I am currently printing:

Example

See this gray stripe? This would prevent me from typing in the tag field.

+11
source share
1 answer

You can create a click-through window by adding the WS_EX_LAYERED and WS_EX_TRANSPARENT to its advanced styles. Also, so that it is always on top, set its TopMost to true and make it translucent, use the appropriate Opacity value:

 using System; using System.Windows.Forms; using System.Runtime.InteropServices; public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Opacity = 0.5; this.TopMost = true; } [DllImport("user32.dll", SetLastError = true)] static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); const int GWL_EXSTYLE = -20; const int WS_EX_LAYERED = 0x80000; const int WS_EX_TRANSPARENT = 0x20; protected override void OnLoad(EventArgs e) { base.OnLoad(e); var style = GetWindowLong(this.Handle, GWL_EXSTYLE); SetWindowLong(this.Handle,GWL_EXSTYLE , style | WS_EX_LAYERED | WS_EX_TRANSPARENT); } } 

Sample Result

enter image description here

+20
source

Source: https://habr.com/ru/post/1257692/


All Articles