C# snippets - create a "transparent" click-through window
I ran into this at the C# channel today.. One guy wanted to create a window, which would be on top of another app and if you clicked that window, the window would not get activated and the event would be sent to the app below it instead.
Here's the code you need to accomplish this:
These two WinAPI functions are required
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern long GetWindowLong(IntPtr hWnd, int nIndex);
Then the actual form/window is made "transparent" with this call (this.Handle being the forms' handle)
SetWindowLong(this.Handle,-20,GetWindowLong(this.Handle,-20) | 0x00000020)
The values (-20, -20 and 0x00000020) are respectively the following from winuser.h: GWL_EXSTYLE, WS_EXSTYLE and WS_EX_TRANSPARENT
And as a side note, I did end up installing SC4... and what have I been doing?... Well, playing it. Not coding like I should be! :]
Labels: C#
5 Comments:
Excellent code - solved my problem quickly. Thanks.
By Unknown, at 8:34 PM
Thanks very much for this!
By Bicubic, at 5:04 AM
Thanks! :D
The google doesn't really have anything about this, and even though it's a winapi it's just hella hard to come across.
Thanks : )
By Unknown, at 10:57 PM
Hello there, I was wondering how to make it click-able again! Cheers :)
By Unknown, at 12:51 PM
c# make label background transparent
By Anonymous, at 5:33 AM
Post a Comment
<< Home