
我们可以采取如下办法,来获取正在系统运行的窗口标题。
在WindowsAPI中有一个函数:
Get Windows (ByVal hWnd As Long ,ByVal wCmd As Long) As Long
其中,hWnd是当前窗口句柄,wCmd是与hWnd有联系的常量,其含义如下:
wCmd值
含义
GW-GHILD
第一个子窗口
GW-HWNDFIRST
子窗口的第一兄弟窗口,其第一个顶层窗口
GW-HWNDLAST
子窗口的最后一个兄弟窗口,或最后一个顶层窗口
GW-HWNDNEXT
后继窗口
GW-HWNDPRCV
先前窗口
GW-OWNER
窗口拥有者
此函数返回值是wCmd所指的窗口的句柄。
我们利用此句柄 ,再用函数Get WindowsText (ByVal hWnd As Long ,ByVal Ipstring As String, ByVal cch As Long) As Long,将句柄hWnd指定的窗口标题放入一个字符串变量Ipstring中,cch是指放入Ipstring中的最大字符数。此函数成功时返回字符串长度,如果窗口无标题则返回零。
在使用Get WindowsText函数前,还要先用函数Get WindowsTextLength (ByVal hWnd As Long )As Long得到hWnd指定窗口标题的长度,放入cch中。
自编一个过程FindTitle()查找系统中正在运行的所有标题,首先获得第一个顶层窗口句柄currwnd,而后采用While…Wend循环结构,当currwnd不为零而且标题文本长度不为零时,将获得的标题存入列表框Combo1,再找后继窗口的句柄,当句柄currwnd=0时表示已没有了后继窗口,退出循环。这样就将系统中所有的窗口句柄及标题找出来了。
然而调试中发现用此方法找到的窗口标题非常多,这说明Windows系统运行时有许多隐含的窗口,而这些窗口是我们所不需要的,而且用AppActivate激活时也出现错误。
因此我们再自编一个过程Sift ()查找可激活的窗口。其方法是用AppActivate逐个激活所有窗口,出现错误的丢掉,保留可激活的窗口标题,放入列表框Combo2。
首先新建窗体Form1,Caption=“获取窗口标题”,在窗体Form1上建立两个标签,Label1,Caption=“所有窗口标题”,Label2.Caption=“可激活的窗口标题”;建立两个下拉列表框,Combo1存放系统中的所有标题名称,Combo2存放可激活的标题名称;再建立两个命令按钮,Command1.Caption=“激活窗体”,可对所列窗体进行测试,命令按钮Command2.Caption=“刷新”,点击它可重新查找所有在系统中的窗体名称,当本程序运行以后又运行了新程序时使用此按钮。
从VB系统菜单上选取Project中的Add Module,将下述API函数及一些常量录入。
’Module 模块
Declare Function GetWindow Lib "user32"(ByVal hwnd As Long, ByVal wCmd As Long)As Long
Declare Function GetWindowText Lib "user32" Aias"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String,ByVal cch As Long)As Long
Declare Function GetWindowText Length Lib "user32" Aias"GetWindowTextLengthA" (ByVal hwnd As Long)As Long
Public Const GW-HWNDFIRST=0
Public Const GW-HWNDLAST=1
Public Const GW-HWNDNEXT=2
Public Const GW-HWNDPREV=3
Public Const GW-OWNER=4
建立两个子程序:
Sub FindTitle ()
查找桌面上的所有窗口标题
Dim currwnd As Integer
Combo1.Clear
Currwnd=GetWindow (hwnd, GW-HWNDFIRST)
While currwnd<>0
Length= GetWindow TextLength (currwnd)
listitem$=Space $(length +1)
length= GetWindow Text (currwnd,listitem$,length+1)
if length>0 Then
Combo1.Addltem listitem $
End if
currwnd= GetWindow (currwnd, GW-HWNDNEXT)
if Combl1.ListCount>0 Then
Combo1.Text=Combol.List (0)
Combo1.Listindex =0
Else
MsgBox“没有发现可活动的窗口”,16,“活动”
End if
Wend
End Sub
Sub Sift ()
’测试窗口能否活动
i=0
Combo2.Clear
Do
On Local Error Resume Next
AppActivate Combo1.List (i)
If Err =0 Then
Combo2.Additem Combo1.List(i)
End if
i=i+1
Loop Unti 1 i=Combo1.ListCount-1
AppActivate Form1.Caption
If Combo2.ListCount>0 then
Combo2.Text =Combo2.List (0)
Combo2.Listindex =0
Else
MsgBox “没有发现可活动窗口”,16,“活动”
End if
End Sub
Private Sub Form-Load ()
Form1.Show’首先将本窗体显示出来,否则查找出的窗体标题没有本身
MsgBox "开始查找窗口标题"
Call FindTitle
Call Sift
End Sub
Private Sub Command1-Click ()
F $=Combo2.Text
On Local Error Resume Next
AppActivate F $
End Sub
Private Sub Command2-Click ()
Call FindTitle
Call Sift
End Sub
findwindow 的两个参数是输入参数,所以不能解决问题。如果你在自己程序中获取标题,你可以用 MFC
CWnd::GetWindowText
void GetWindowText( CString&rString ) const
或API
int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount )
hWnd 用 NULL 或 this 试试。
程序以外参考下面方法列出所有窗口的szClass,和 标题,
对比 你要得程序名,若等于 szClass,则选中标题。
// 编译时,别忘了链接User32.lib
#include<iostream>
#include <Windows.h>
#include <iomanip>
using namespace std
int iCount=0
BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)
{
TCHAR szClass[256]={0}
TCHAR szWindow[256]={0}
::GetWindowText(hWnd, szWindow, 255)
::GetClassName(hWnd, szClass, 255)
if ( ::IsWindow(hWnd) &&
::IsWindowVisible(hWnd) &&
(::GetWindowLong(hWnd, GWL_EXSTYLE)&WS_EX_TOOLWINDOW)!
=WS_EX_TOOLWINDOW &&
::GetWindowLong(hWnd, GWL_HWNDPARENT)==0 )
{
int i=strlen(szClass)
cout <<"["<<szClass <<setw(15-strlen(szClass))<<"] "<<szWindow <<endl
iCount++
}
return TRUE
}
/* main User32.lib.*/
int main(int argc, char* argv[])
{
EnumWindows(EnumWindowProc, NULL)
cout<<"Find "<<iCount <<" windows."<<endl
return 0
}
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 LongPrivate Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Dim WithEvents Tim As Timer
Private Sub Form_Load()
Set Tim = Controls.Add("vb.timer", "tim")
Tim.Interval = 500: Tim.Enabled = True
SetWindowPos Me.hWnd, -1, 0, 0, 0, 0, 3 '使窗体置于最顶层,便于查看
End Sub
Private Sub Tim_Timer()
Dim hWnd As Long, st As String * 100
hWnd = GetForegroundWindow '得到活动窗口的句柄
GetWindowText hWnd, st, 100 '得到活动窗口的标题
Text1 = st
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)