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?
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 – Backs Apr 30 '16 at 9:20
-
@Backs : 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. – Visual Vincent Apr 30 '16 at 9:21 -
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 – Backs Apr 30 '16 at 9:23
-
1@Backs : Fixed. The function declaration should be
int
, but in C++ the flags are of typeDWORD
(uint
). I changed everything toint
now, like you said. Microsoft is confusing us. ;) – Visual Vincent Apr 30 '16 at 9:27 -
1
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. :) – Visual Vincent Apr 30 '16 at 8:54