C
C#10mo ago
Ossie

❔ Making WPF window click thru / unclickable?

I have a overlay window, which is on top of some other programs, but right now, if you click on the overlay part on the window, it will try to focus the overlay and start blinking, and you will loose control of the program the overlay is on top off. So how do I make the overlay click thru or unclickable? Thanks
3 Replies
arion
arion10mo ago
There are various ways to do this. The simplest I can think of right now is window.IsHitTestVisible = false; Let me know if this works for you! If not, you could go a PInvoke route and set it using windows apis I'll post the alternative anyway To get a window handle you can use
new WindowInteropHelper(window).Handle
new WindowInteropHelper(window).Handle
to get a window handle and then use PInvoke's SetWindowLong to set the respective flags:
var style = (User32.WindowStylesEx)User32.GetWindowLong(hwnd, User32.WindowLongFlags.GWL_EXSTYLE);

style |= User32.WindowStylesEx.WS_EX_TRANSPARENT | User32.WindowStylesEx.WS_EX_LAYERED;

var nonZeroMeansSuccess = User32.SetWindowLong(hwnd, User32.WindowLongFlags.GWL_EXSTYLE, (int)style);
var style = (User32.WindowStylesEx)User32.GetWindowLong(hwnd, User32.WindowLongFlags.GWL_EXSTYLE);

style |= User32.WindowStylesEx.WS_EX_TRANSPARENT | User32.WindowStylesEx.WS_EX_LAYERED;

var nonZeroMeansSuccess = User32.SetWindowLong(hwnd, User32.WindowLongFlags.GWL_EXSTYLE, (int)style);
Here in the example i'm using the Vanara.PInvoke.User32 nuget package though you don't need to use a package as all the enums can be marshalled as ints and methods can be added via [LibraryImport] eg. for SetWindowLong: Note, your class thas has this method would also have to be partial (source code generation occurs)
[LibraryImport("User32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
[LibraryImport("User32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
Ossie
Ossie10mo ago
Thanks, I'm gonna try it out 🙂 Hm, can't get either of them to work
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts