我的WPF元素在屏幕上可见还是被任何应用程序的另一个窗口隐藏
如何知道WPF应用程序中的某个元素是否被任何应用程序的另一个窗口隐藏 下面是我如何在屏幕上获取有关打开窗口的信息的示例。例如:我的应用程序在顶部 (我学习的基本代码)我的WPF元素在屏幕上可见还是被任何应用程序的另一个窗口隐藏,wpf,winapi,Wpf,Winapi,如何知道WPF应用程序中的某个元素是否被任何应用程序的另一个窗口隐藏 下面是我如何在屏幕上获取有关打开窗口的信息的示例。例如:我的应用程序在顶部 (我学习的基本代码) 将函数GetTopWindow Lib“user32”别名“GetTopWindow”(ByVal hwnd作为整数)声明为整数 将函数GetNextWindow Lib“user32”别名“GetWindow”(ByVal hwnd为整数,ByVal wFlag为整数)声明为整数 将函数IsWindowVisible Lib“u
将函数GetTopWindow Lib“user32”别名“GetTopWindow”(ByVal hwnd作为整数)声明为整数
将函数GetNextWindow Lib“user32”别名“GetWindow”(ByVal hwnd为整数,ByVal wFlag为整数)声明为整数
将函数IsWindowVisible Lib“user32”别名“IsWindowVisible”(ByVal hwnd作为整数)声明为布尔值
将函数GetWindowText Lib“user32”别名“GetWindowTextA”(ByVal hwnd作为整数,ByVal lpString作为字符串,ByVal cch作为整数)声明为整数
将函数GetWindowRect Lib“user32”别名“GetWindowRect”(ByVal hwnd作为IntPtr,ByRef pwi作为Rect)声明为布尔值
函数IsOnTop(ByVal hwnd作为整数)作为布尔值
Dim i作为整数=GetTopWindow(0)
尺寸x为整数=1
像线一样变暗
做
i=GetNextWindow(i,2)'按Z顺序查找下一个窗口
如果i=hwnd,则
退出Do
其他的
如果i=0,则“从未找到与输入句柄匹配的窗口”
返回错误
如果结束
如果结束
如果IsWindowVisible(i)=真,则
s=空间(256)
如果GetWindowText(i,s,255)为0,则
“防止气球提示和上下文提示混淆非常重要
x+=1
如果结束
如果结束
环
'x是Z顺序号
如果x=1,则
返回真值
其他的
返回错误
如果结束
端函数
公共函数GetWindowText(ByVal hWnd作为IntPtr)作为字符串
尺寸s=空间(256)
GetWindowText(hWnd、s、255)
返回s.ToString()
端函数
函数GetRectWindow(hwnd为整数)为Rect
Dim rc As Rect
GetWindowRect(hwnd,rc)
返回rc
端函数
我需要知道其他窗户的宽度和高度。如果没有这些数据,我仍然无法知道某个元素是否对用户隐藏。例如,在我的应用程序中,我有一个包含两个DataGrid的窗口,其中一个可能被其他应用程序隐藏
问题是,尽管GetRectWindow方法返回此数据,但例如,它给出了宽度属性=4.09332988076806E-311,并且应该是350。据我所知,它使用Twips单元。我将其转换为像素单位,但得到的结果是一个无穷大的数字-0。这里有一种方法:
- 计算视觉相对于根视觉的界限(visual.TransformToAncestor)
- 计算相对于屏幕的视觉边界(本机GetWindowRect和GetClientRect)
- 枚举所有顶级窗口(本机枚举窗口)
- 检查每个窗口是否可见,以及是否与可视窗口的边界重叠(本机GetWindowRect)
Declare Function GetTopWindow Lib "user32" Alias "GetTopWindow" (ByVal hwnd As Integer) As Integer
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Integer, ByVal wFlag As Integer) As Integer
Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Integer) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As IntPtr, ByRef pwi As Rect) As Boolean
Function IsOnTop(ByVal hwnd As Integer) As Boolean
Dim i As Integer = GetTopWindow(0)
Dim x As Integer = 1
Dim s As String
Do
i = GetNextWindow(i, 2) ' Find next window in Z-order
If i = hwnd Then
Exit Do
Else
If i = 0 Then ' Never find any window match the input handle
Return False
End If
End If
If IsWindowVisible(i) = True Then
s = Space(256)
If GetWindowText(i, s, 255) <> 0 Then
' Very important to prevent confusing of BalloonTips and ContextMenuStrips
x += 1
End If
End If
Loop
' x is Z-order number
If x = 1 Then
Return True
Else
Return False
End If
End Function
Public Function GetWindowText(ByVal hWnd As IntPtr) As String
Dim s = Space(256)
GetWindowText(hWnd, s, 255)
Return s.ToString()
End Function
Function GetRectWindow(hwnd As Integer) As Rect
Dim rc As Rect
GetWindowRect(hwnd, rc)
Return rc
End Function