C
C#9mo ago
Ossie

❔ WPF Window click-thru/non-clickable

Hey, how is it possible to make a WPF Window click-thru/non-clickable. I don't want to highlight or click on it in any way. The window is for an overlay, so I needs to be uninteractable, so you don't click it accidently. I already tried the following without success: 1. Using SetWindowLong
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = (-20);

public static void MakeTransParent(IntPtr hWnd)
{
int extendedStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = (-20);

public static void MakeTransParent(IntPtr hWnd)
{
int extendedStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
This didn't work, and its still clickable for some reason. Is it because im trying to do it from inside the WPF windows own code? 2. IsHitTestVisible I tried setting IsHitTestVisible to false, both in the XAML and after instancing the window. Still clickable... Any help is appreciated, as I can't seem to find anything online that is not the methods listed above. Thanks
22 Replies
arion
arion9mo ago
Here's what I've done in the past to set windows as transparent
private static void SetTransparent(HWND hwnd, WindowStylesEx style)
{

style |= WindowStylesEx.WS_EX_TRANSPARENT | WindowStylesEx.WS_EX_LAYERED;

var retVal = SetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE, (int)style);

if (retVal != 0)
return;

Log.Logger.Error(new Win32Exception(Marshal.GetLastWin32Error()), "Error setting window style");
}
private static void SetTransparent(HWND hwnd, WindowStylesEx style)
{

style |= WindowStylesEx.WS_EX_TRANSPARENT | WindowStylesEx.WS_EX_LAYERED;

var retVal = SetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE, (int)style);

if (retVal != 0)
return;

Log.Logger.Error(new Win32Exception(Marshal.GetLastWin32Error()), "Error setting window style");
}
style is gotten from
private static WindowStylesEx GetWindowFlags(HWND hwnd) => (WindowStylesEx)GetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE);
private static WindowStylesEx GetWindowFlags(HWND hwnd) => (WindowStylesEx)GetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE);
Differences I see are, you're fully setting, not appending, and not using WS_EX_Layered Hope it helps! Possibly also check if the PInvokes are failing too
Mayor McCheese
Mayor McCheese9mo ago
I'm assuming this is like a HUD assist or other type application; so that you can decorate various parts of the HUD decorator without impacting/interacting with the game?
Ossie
Ossie9mo ago
Yes, it just needs to stay on top of the application 🙂 Hi, do I need some special package for this or? WindowStylesEx isnt a type and also HWND is the type IntPtr in C#, so I'm not really sure if your code is for a nuget package or smth
Mayor McCheese
Mayor McCheese9mo ago
So if they violates the game TOS it can't really be discussed here; but this isn't something that wpf handles out of the box; and iirc a lot of it involves poorly documented windows apis.
Ossie
Ossie9mo ago
Its not violating any TOS
Mayor McCheese
Mayor McCheese9mo ago
I don't know the poorly documented windows apis myself; other then they're poorly documented.
Ossie
Ossie9mo ago
hm :/
arion
arion9mo ago
it is a nuget package but can easily be inferred what is what. like HWND is just an IntPtr and WindowStylesEx is an enum that has all the values, i believe in C++ they're constants
arion
arion9mo ago
(32U is the same as your 0x00000020 btw)
arion
arion9mo ago
so layered is 0x00080000 so instead, you could just substitute them out as
style |= 32 | 524288;

var sr = SetWindowLong(hwnd, -20, style);
style |= 32 | 524288;

var sr = SetWindowLong(hwnd, -20, style);
then just check if sr != 0 as a success check Vanara.PInvoke has their method summaries scraped from https://learn.microsoft.com/en-us/windows/win32/api/ i believe
arion
arion9mo ago
PowerToys .
arion
arion9mo ago
Some processes run elevated, so the method SetWindowLong can fail if your process is not admin too
arion
arion9mo ago
Seems WPF is a type of exception to this, I found a stackoverflow post about it here
Stack Overflow
Making a WPF window click-through, but not its controls
First of all I want to define what I mean by transparent to clicks in the text bellow: It means the clicks go through my window without any processing, directly to whichever window is bellow it. I ...
Ossie
Ossie9mo ago
Hm I tried rewriting the one you send yesterday to plain C# types, but it didn't work, but I was still using the hex values, I will try with the plain integer ones, I also tried using Vanara.PInvoke since they have the types your example used, but didn't work either. My application runs as adminstrator so it shouldn't be a problem with the elevated thing
arion
arion9mo ago
Could you try this possible solution from StackOverflow?
Stack Overflow
Making a WPF window click-through, but not its controls
First of all I want to define what I mean by transparent to clicks in the text bellow: It means the clicks go through my window without any processing, directly to whichever window is bellow it. I ...
arion
arion9mo ago
What It basically does Is create a transparent window.(Click through and Invisble). But controls are visible and not-click through. Or you can try How to create a semi transparent window in WPF that allows mouse events to pass through
Ossie
Ossie9mo ago
I dont need controls or anything on the overlay/window, but I'll try it
Accord
Accord9mo 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