如何判断窗口是否为指定窗口(已知窗口标题)

kd911 2009-09-29 12:08:38
如何判断窗口是否为指定窗口(已知窗口标题)

已知窗口标题 Text1.Text = getCaption(hWnd1) ,指定的标题格式有四种:中1A:与 文2B 对话系统错误登录非以上三种

程序需要判断不同窗口执行不同命令,并且在第一种情况中,要把“中1A:”和“文2B”单独拿出来作为一个变量,供以后使用。

小弟很菜,请大虾们回答是尽量详细。

谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢~!

以下是获取窗口标题的代码:

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Dim hWnd1 As Long

Private Sub Form_Load()
Label1.AutoSize = True
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
Label1.Caption = getCaption(hWnd1)
Text1.Text = getCaption(hWnd1)
End Sub
'根据窗口句柄得到该窗口的标题
Function getCaption(hWnd As Long)
Dim hWndlength As Long, hWndTitle As String, A As Long
hWndlength = GetWindowTextLength(hWnd)
hWndTitle = String$(hWndlength, 0)
A = GetWindowText(hWnd, hWndTitle, (hWndlength + 1))
getCaption = hWndTitle
End Function
Private Sub Command1_Click()
End
End Sub

...全文
150 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2009-09-29
  • 打赏
  • 举报
回复
Option Explicit

Dim hWnd1 As Long
Dim eCategory As Long
Dim sSpicalCaption As String

Private Sub Timer1_Timer()
Dim a() As String

hWnd1 = GetForegroundWindow()
Label1.Caption = getCaption(hWnd1)
Text1.Text = Label1.Caption

If Text1.Text Like "*与*对话" Then
a = Split(Left$(Text1.Text, Len(Text1.Text) - 2), "与")
sSpicalCaption = Trim$(a(0)) & Trim$(a(1))
eCategory = 1
Else
sSpicalCaption = vbNullString
Select Case Text1.Text
Case "系统错误"
eCategory = 2
Case "登录"
eCategory = 3
Case Else
eCategory = 4
End Select
End If
End Sub
zouyingbin 2009-09-29
  • 打赏
  • 举报
回复
http://bbs.yjxsoft.net/?u=627
king06 2009-09-29
  • 打赏
  • 举报
回复
'需要的俩变量
Dim strOne As String
Dim strAnother As String

Private Sub Timer1_Timer()
hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
Label1.Caption = getCaption(hWnd1)
Text1.Text = getCaption(hWnd1)
If Label1.Caption Like "*与*对话" Then
strOne = Trim(Left(Label1.Caption, InStr(Label1.Caption, "与") - 1))
strAnother = Trim(Mid(Label1.Caption, InStr(Label1.Caption, "与") + 1, InStr(Label1.Caption, "对话") - InStr(Label1.Caption, "与") - 1))
ElseIf Label1.Caption = "系统错误" Then

ElseIf Label1.Caption = "登陆" Then

Else '非以上三种

End If
End Sub
孤独剑_LPZ 2009-09-29
  • 打赏
  • 举报
回复
Private Sub Timer1_Timer()
Static i As Long
hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
Label1.Caption = getCaption(hWnd1)
text1.Text = getCaption(hWnd1)
'下面增加的代码
Select Case text1.Text
Case "中1A:与 文2B 对话"
s(i) = text1.Text '保存到全局变量中,要先定义
i = i + 1
.... '添加执行命令
Case "系统错误"
.... '添加执行命令
Case "登录"
.... '添加执行命令
Case Else
.... '添加执行命令
End Select
End Sub
神马都能聊 2009-09-29
  • 打赏
  • 举报
回复
    If Text1.Text Like "*与*对话" Then
a = Split(Left$(Text1.Text, Len(Text1.Text) - 2), "与")
sSpicalCaption = Trim$(a(0)) & Trim$(a(1))
eCategory = 1
Else

字符串支持like?好玩,一会去玩下
kd911 2009-09-29
  • 打赏
  • 举报
回复
已经可以了。


谢了·



Tiger_Zhao 2009-09-29
  • 打赏
  • 举报
回复
* 是通配符。
你运行一下程序,自己看看 sSpicalCaption 的值!
kd911 2009-09-29
  • 打赏
  • 举报
回复
刚才仔细看了一下几位前辈的代码。

在“*与*对话”这个中的“*”是变化的。如果用If Text1.Text Like "*与*对话" Then

不就成了固定的了??
kd911 2009-09-29
  • 打赏
  • 举报
回复
我看错了。····
kd911 2009-09-29
  • 打赏
  • 举报
回复
谢谢大家的回答·~


可是好像没有解决“*与* 对话”标题中,取得两个的“*”的变量
threenewbee 2009-09-29
  • 打赏
  • 举报
回复
strCaption = getCaption(hWnd)
If strCaption Like "*与*对话" Then
...
End If

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧