获取当前活动窗口句柄和标题

获得鼠标指针位置函数
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'设置窗体位置 函数
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 GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Const StringLen = 80

 


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_GETTEXT = &HD


Private Sub Form_Click() '这里是窗体点击置顶和取消
Dim CancelVal As Long
Static Choose As Integer
If Choose = 0 Then
CancelVal = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)
Me.Caption = "已经成为顶层窗体"
Choose = 1
Else
CancelVal = SetWindowPos(Me.hwnd, -2, 0, 0, 0, 0, 3)
Me.Caption = "已还原为一般窗口"
Choose = 0
End If

End Sub


Private Sub Form_Load()
Timer1.Interval = 100 '设置timer属性
Form1.Caption = "单击本窗体成为顶层窗体"
End Sub

 

Private Sub LABEL1_Click(Index As Integer)
Form_Click
End Sub


Private Sub Lb1_Click()

End Sub

Private Sub Timer1_Timer()
Dim PointXY As POINTAPI
Dim WinHwnd&, WinCancelValue As Long
Dim WinCaption As String * StringLen
Dim ClassName As String * StringLen
GetCursorPos PointXY
WinHwnd = WindowFromPoint(PointXY.x, PointXY.y)
'WinCancelValue = GetWindowText(WinHwnd, WinCaption, StringLen)
'下一句若为此句就会非发关闭:WinCancelValue = SendMessage(WinHwnd, WM_GETTEXT, StringLen,WinCaption) 缺少ByVal
WinCancelValue = SendMessage(WinHwnd, WM_GETTEXT, StringLen, ByVal WinCaption)
WinCancelValue = GetClassName(WinHwnd, ClassName, StringLen - 1)
Label1(0).Caption = "鼠标的X坐标:" & PointXY.x
Label1(1).Caption = "鼠标的Y坐标:" & PointXY.y
Label1(2).Caption = "鼠标所在窗口句柄为:" & WinHwnd
Label1(3).Caption = "窗口的标题为:" & Trim(WinCaption)
Label1(4).Caption = "窗口的类名为:" & ClassName
End Sub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值