标题:[求助]如何鼠标拖动获得程序窗口标题和类名?
只看楼主
渴望知识
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-4-24
 问题点数:0 回复次数:3 
[求助]如何鼠标拖动获得程序窗口标题和类名?

我编一个程序,要用到其他程序窗口的标题和类名,其他程序的标题和类名不固定,什么方法能得到呢?就像“窗口类名查看器(spy)”那样,把一个图标拖动到其他指定窗口就取得该窗口标题和类名等信息。这个应该怎么实现呢?有具体例子最好,我初学者,谢谢!....

搜索更多相关主题的帖子: 程序窗口 拖动 类名 鼠标 
2007-06-21 19:59
猪肉
Rank: 1
等 级:新手上路
帖 子:96
专家分:1
注 册:2007-3-8
得分:0 
你回家自己慢慢研究吧,一百年后你一定会研究成功的。
2007-06-21 20:04
渴望知识
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-4-24
得分:0 
2007-06-21 22:13
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 

'在窗体上添加两个CommandButton控件,一个List控件,控件名都使用默认的名称。将下面的代码复制到窗体中就可以了。点击“开始”就可以获取鼠标所指的窗体的句柄、类型名和Caption属性

Option Explicit

Private Type POINTAPI
X As Long
Y As Long
End Type


Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const Swp_Nosize = &H1
Private Const Swp_Nomove = &H2
Private Const Swp_NoActivate = &H10
Private Const Swp_ShowWindow = &H40
Private Const Hwnd_TopMost = -1
Private Const Hwnd_NoTopMost = -2
Private Const wFlags = Swp_Nomove Or Swp_Nosize Or Swp_ShowWindow Or Swp_NoActivate

Private ExitGetInfo As Boolean '全局变量,用于设置是否中止GetInfo函数
Private GetInfoBusy As Boolean '全局变量,用于指示当前是否正在运行GetInfo函数

Private Sub Command1_Click()
SetWindowPos Me.hwnd, Hwnd_TopMost, 0, 0, 0, 0, wFlags '窗体置顶,将自身窗体设置为总是在最前面
Command2.Enabled = True
Command1.Enabled = False
GetInfo '开始获取窗体的信息
End Sub

Private Sub Command2_Click()
ExitGetInfo = True
SetWindowPos Me.hwnd, Hwnd_NoTopMost, 0, 0, 0, 0, wFlags '取消窗体置顶
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub GetInfo()
Dim Points As POINTAPI
Dim lhWnd As Long, LasthWnd As Long, I As Long
Dim Capt(5) As String, InfoS() As String, TempSTR As String * 255

GetInfoBusy = True '设置全局变量,指示GetInfo正在运行

Capt(0) = "当前Handle: "
Capt(1) = "当前Class: "
Capt(2) = "当前Caption:"
Capt(3) = "父Handle: "
Capt(4) = "父Class: "
Capt(5) = "父Caption: "

Do
GetCursorPos Points '获取鼠标坐标
lhWnd = WindowFromPoint(Points.X, Points.Y) '获取鼠标所在处窗体句柄
If lhWnd <> 0 And lhWnd <> LasthWnd Then '如果鼠标处有窗体存在(lhWnd<>0)
LasthWnd = lhWnd
ReDim InfoS(5)
InfoS(0) = "&H" & Hex(lhWnd) & "(" & lhWnd & ")"
I = GetClassName(lhWnd, TempSTR, 255)
If I Then InfoS(1) = Left(TempSTR, I)
I = GetWindowText(lhWnd, TempSTR, 255)
If I Then InfoS(2) = Left(TempSTR, I) Else Debug.Print I
I = GetParent(lhWnd)
InfoS(3) = "&H" & Hex(I) & "(" & IIf(I, I, "桌面") & ")"
If I Then
lhWnd = I
I = GetClassName(lhWnd, TempSTR, 255)
If I Then InfoS(4) = Left(TempSTR, I)
I = GetWindowText(lhWnd, TempSTR, 255)
If I Then InfoS(5) = Left(TempSTR, I)
End If
List1.Clear
For I = 0 To 5
If Len(InfoS(I)) Then List1.AddItem Capt(I) & InfoS(I)
Next
End If
DoEvents
Loop Until ExitGetInfo
ExitGetInfo = False
GetInfoBusy = False '设置全局变量,指示GetInfo已经结束
End Sub

Private Sub Form_Load()
Command1.Caption = "开始(&S)"
Command2.Caption = "结束(&C)"
Command1.Default = True
Command2.Cancel = True
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Command2_Click '如果正在获取窗体信息,要点两次关闭按钮才能正常退出。
Cancel = GetInfoBusy
End Sub

Private Sub Form_Resize()
On Error Resume Next
If Me.WindowState <> vbMinimized Then
List1.Width = Me.Width - 465
List1.Height = Me.Height - 1300
End If
End Sub

'如果一时不能理解代码请自行查看API函数说明。程序是临时写的,只加了一点注释,不过应该很清楚了。


VB QQ群:47715789
2007-06-23 01:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-149334-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.141394 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved