Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can I find out if an external window's topmost state is set to true
Posted
Comments
Nelek 6-Oct-14 7:13am    
Craig Haywood 6-Oct-14 7:30am    
Nothing as yet as I do not know how. That's why I am asking
BillWoodruff 6-Oct-14 7:16am    
What exactly do you mean by "external window" ? Is this WinForms ? WPF ? or ... ?
Craig Haywood 6-Oct-14 7:21am    
This can be any external window that is not part of my application. Whether it is winforms or wpf is irrelevant. I know how to set the topmost state of an external window as well as how to remove it, but in order to create a toggle function in my application, I first need to get the current topmost state for the external window.
BillWoodruff 6-Oct-14 7:30am    
Assuming you are using API code from C# to get a pointer to all open windows, and that you then discriminate which open windows are not your own app's windows ... doesn't the API you are using return some information about the windows state ?

What is your goal here ? To make sure one, or all, of you own app's windows are not behind some external app's windows ?

You can use GetWindowLong with second argument of GWL_EXSTYLE:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633584%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx[^].

AND the result (actual "extended style") with WS_EX_TOPMOST to see if the window in question topmost or not.

—SA
 
Share this answer
 
v2
Comments
Craig Haywood 6-Oct-14 10:52am    
From everything I have read, this seems to be the approach, in my tests, GetWindowLong always returns 0. Maybe somebody here can get it to work on both 32bit and 64bit machines
Sergey Alexandrovich Kryukov 6-Oct-14 11:40am    
It should not return 0. Check up your HWND.
Please see, for 64/32 bit platform compatibility: GetWindowLongPtr.
This function superseded GetWindowLong.
—SA
Craig Haywood 7-Oct-14 3:03am    
GetWindowLongPtr gives a first chance exception of type 'System.EntryPointNotFoundException'

Imports System.Runtime.InteropServices

Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetWindowLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim notepad As Process = Process.GetProcessesByName("notepad")(0)
Dim hWnd As IntPtr = notepad.MainWindowHandle
Dim GWL_EXSTYLE As IntPtr = -20
Dim WS_EX_TOPMOST As IntPtr = 8
Dim Style As IntPtr = GetWindowLongPtr(hWnd, GWL_EXSTYLE)
End Sub
End Class
Sergey Alexandrovich Kryukov 7-Oct-14 11:20am    
The API name is GetWindowLongPtrW (Unicode)
—SA
Craig Haywood 7-Oct-14 12:43pm    
I'm starting to think this is just not possible in vb or C#

Imports System.Runtime.InteropServices

Public Class Form1
<DllImport("user32.dll", EntryPoint:="GetWindowLongPtrW")> _
Public Shared Function GetWindowLongPtrW(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr

End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim notepad As Process = Process.GetProcessesByName("notepad")(0)
Dim hWnd As IntPtr = notepad.MainWindowHandle
Dim notepad2 As New HandleRef(0, hWnd)
Dim Style As IntPtr = GetWindowLongPtrW(notepad2, -20)
MsgBox(Style.ToString)

End Sub
End Class

Additional information: Unable to find an entry point named 'GetWindowLongPtrW' in DLL 'user32.dll'.
Just want to say thanks to everybody who helped. Just when I was about to give up Sergey pointed me to code I used incorrectly yesterday, just to have success today :-)



VB
Public Class Form1
    <DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
    Private Shared Function GetWindowLongPtr32(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="GetWindowLongPtr")> _
    Private Shared Function GetWindowLongPtr64(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
    End Function

    ' This static method is required because Win32 does not support GetWindowLongPtr dirctly
    Public Shared Function GetWindowLongPtr(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
        If IntPtr.Size = 8 Then
            Return GetWindowLongPtr64(hWnd, nIndex)
        Else
            Return GetWindowLongPtr32(hWnd, nIndex)
        End If
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Dim notepad As Process = Process.GetProcessesByName("notepad")(0)
        'Dim hWnd As IntPtr = notepad.MainWindowHandle
        Dim GWL_EXSTYLE As Integer = -20
        Dim WS_EX_TOPMOST As IntPtr = 8
        'Dim Style As IntPtr = GetWindowLongPtr(hWnd, GWL_EXSTYLE)
        Dim notepad As Process = Process.GetProcessesByName("notepad")(0)
        Dim hWnd As IntPtr = notepad.MainWindowHandle
        Dim notepad2 As New HandleRef(0, hWnd)
        Dim Style As IntPtr = GetWindowLongPtr(notepad2, -20)
        MsgBox(Style.ToString)
    End Sub
End Class
 
Share this answer
 
Check WS_EX_TOPMOST[^].

You can use EnumChildWindows [^] or GetWindow [^] to get the window you want to check.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Oct-14 10:31am    
That is correct, but OP, who could not find this constant, would be as likely unaware how to check it up, too. Two links on a second line are pretty much irrelevant. It could be any HWND; and we don't know what's on input.

Please see Solution 2.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900