API getwindowrect 的使用问题
我做了一个小程序,但不能正确使用getwindowrect函数,我是新手,万望各位高手们指教,谢谢主程序:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aaa As New task
Dim bbb As taskbar
Dim ccc As Collection
ccc = aaa.gainedlist
ListBox1.Items.Clear()
For Each bbb In ccc
ListBox1.Items.Add(bbb.hwn.ToString & "-" & bbb.name)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem.ToString.Split("-")(0)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ListBox1.SelectedIndex = -1 Then Exit Sub
Dim aaa As New chuangkou
aaa.hwn = ListBox1.SelectedItem.ToString.Split("-")(0)
aaa.huoqu()
Label1.Text = aaa.chuzhuo.Bottom
aaa = Nothing
End Sub
End Class
类型 chuangkou
Public Class chuangkou
'用于对窗口进行操作的类型
Public hwn As IntPtr
Public chuzhuo As Rectangle
Private Declare Function GetWindowRect Lib "user32" (ByRef hwnd As IntPtr, ByRef lpRect As Rectangle) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Function gainzhuo() As Boolean
If GetWindowRect(hwn, chuzhuo) <> 0 Then '此处出现问题,获取的坐座总是0,不论是left/top/bottom or right ,请各位指教!!!!
MsgBox("窗口信息获取完毕")
Else
MsgBox("获取窗口座标失败!!!")
End If
Return True
End Function
Public Function huoqu() As Boolean
gainzhuo()
Return True
End Function
End Class
类型TASK
Public Class task
Private Declare Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As IntPtr
Private Declare Function GetParent Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Int32) As IntPtr
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Int16
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
Public Const GW_OWNER = 4
Public Const GWL_EXSTYLE = (-20)
Public Const GW_CHILD = 5
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_EX_APPWINDOW = &H40000
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2
Private taskli As Collection
Public Function gainedlist() As Collection
Return taskli
End Function
Public Sub New()
'类初始化
Dim aaa As IntPtr
Dim bbb As IntPtr
Dim ccc As taskbar
Dim eee As Long
Dim ddd As String
Dim fff As Integer
fff = 0
taskli = New Collection
aaa = GetDesktopWindow
bbb = GetWindow(aaa, GW_CHILD)
bbb = GetWindow(bbb, GW_HWNDFIRST)
Do
If bbb = 0 Then Exit Do
If IsTaskbarApp(bbb) Then
fff = fff + 1
eee = GetWindowTextLength(bbb)
ddd = Space$(eee + 1)
eee = GetWindowText(bbb, ddd, eee + 1)
ccc = New taskbar
ccc.hwn = bbb
ccc.name = ddd
taskli.Add(ccc, ccc.name & ccc.hwn.ToString)
End If
bbb = GetWindow(bbb, GW_HWNDNEXT)
Loop
End Sub
'判断窗口是否为任务应用函数,参数为窗口句柄
Public Function IsTaskbarApp(ByVal hwnd As IntPtr) As Boolean
Dim ExtendStyle As Long
Dim hParent As IntPtr
Dim BoolIsTaskBarApp As Boolean
ExtendStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE)
hParent = GetParent(hwnd)
BoolIsTaskBarApp = CBool((IsWindowVisible(hwnd) <> 0) And _
(GetWindow(hwnd, GW_OWNER) = 0) And _
(hParent = 0 Or hParent = GetDesktopWindow()))
If (ExtendStyle And WS_EX_TOOLWINDOW) Then BoolIsTaskBarApp = False
If (ExtendStyle And WS_EX_APPWINDOW) Then BoolIsTaskBarApp = True
IsTaskbarApp = BoolIsTaskBarApp
End Function
End Class