I am able to set a windows position as topmost and also setting it no topmost with SetWindowPos
. But i can't figure out how to check if a window is topmost or not. Is there any Method to check if a window is topmost or not with pinvoke?
Determine if a window is topmost or not
You can use the GetWindowLong()
function to check the Extended Window Styles.
Untested, but I believe it should work:
[DllImport("user32.dll", SetLastError=true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
const int GWL_EXSTYLE = -20;
const int WS_EX_TOPMOST = 0x0008;
public static bool IsWindowTopMost(IntPtr hWnd)
{
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
return (exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST;
}
i only think you need to change the type of exStyle to int and the same with ES_EX_TOPMOST –
Emmanuel
@Emmanuel : Why? It's a DWORD in C++, which translates to UInt32. So this is the correct. EDIT: Oh wait, the
GetWindowLong()
declaration. Okay I'm not sure which to use anymore. –
Overarm with WS_EX_TOPMOST you are correct but if i use uint it returns the error that i am missing a cast from int to uint –
Emmanuel
@Emmanuel : Fixed. The function declaration should be
int
, but in C++ the flags are of type DWORD
(uint
). I changed everything to int
now, like you said. Microsoft is confusing us. ;) –
Overarm ah now it's clear. thank you for the fast fixes and awnsers. –
Emmanuel
@Emmanuel : No problem, glad I could help! –
Overarm
You don't need to do useless check
(exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST
its enough to do return exStyle & WS_EX_TOPMOST
. Its just single bit check, after all. Also, I would better used DWORD exStyle = (DWORD)GetWindowLong(hWnd, GWL_EXSTYLE);
. Using signed integers for boolean operation makes no sense. –
Impend @Impend In this case you actually need to check
(exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST
as doing so makes the return value a bool
, otherwise the compiler will complain that it Cannot implicitly convert type 'int' to 'bool'
. –
Overarm As for using signed integers, it doesn't really matter in C# as they are treated more or less the same in cases like this (although unsigned integers aren't CLS-compliant, so if this is a requirement then you must use a signed int). Additionally,
GetWindowLong
returns a LONG
, which is a 32-bit signed integer, so it would seem Microsoft had no issue with using signed ints for flags. Thus, if we're being really technical about it, it's more correct to have the P/Invoke declaration return an int
, which is the technical equivalent to LONG
. –
Overarm Depending on the UI technology you*re using, you can choose of the following two:
- Windows Forms:
Form.TopMost
- WPF:
Window.TopMost
You can you those properties to check if a certain window is topmost and you can also use these to set a window topmost. I'd prefer these in favor of any win32 methods.
This only works for any window in his own application. I bet he's using the WinAPI functions to modify windows of external applications, or else it'd be unnecessary work to use them. :) –
Overarm
© 2022 - 2024 — McMap. All rights reserved.