玩酷之家

 找回密码
 立即注册
查看: 46|回复: 0

VB 使用GetForegroundWindow获得当前活动窗口的标题

[复制链接]
发表于 2021-1-8 16:32:52 | 显示全部楼层 |阅读模式
  1. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  2. Private Declare Function GetWindowText Lib "user32" _
  3.     Alias "GetWindowTextA" (ByVal hwnd As Long, _
  4.        ByVal lpString As String, ByVal cch As Long) As Long

  5. Private Sub Form_Load()
  6.     Timer1.Interval = 100 '设置间隔时间
  7. End Sub
  8. Private Sub Timer1_Timer()
  9.     Static CurrentHwnd As Long
  10.     Dim ForegroundWindowHwnd As Long
  11.     Dim sText As String * 255
  12.     ForegroundWindowHwnd = GetForegroundWindow
  13.     If ForegroundWindowHwnd = CurrentHwnd Then Exit Sub
  14.     CurrentHwnd = ForegroundWindowHwnd
  15.     If CurrentHwnd <> hwnd Then
  16.         Caption = "ActiveWidow's Caption: " & Left$(sText, GetWindowText(CurrentHwnd, sText, 255))
  17.     Else
  18.         Caption = "ActiveWindow's Caption: Form1"
  19.     End If
  20. End Sub
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号 )

GMT+8, 2021-1-22 05:17 , Processed in 1.092002 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表