我用ShellExecute调用了Windows的一个应用程序,请问如何获得(捕捉)此窗口中某一个控件的句柄,并将焦点聚集在上边呢(关键是这一点)?

yunok 2003-06-26 02:29:02
我用ShellExecute调用了Windows的一个应用程序,请问如何获得(捕捉)此窗口中某一个控件的句柄,并将焦点聚集在上边呢(关键是这一点)?

大侠们能不能给写一个例子?我知道可以根据窗体的标题来取得窗体的HWND,可是如何做呢?

比如:用ShellExecute调去了“我的电脑”的窗口,其中“地址栏”的句柄是656520(每次句柄都是不同的,我是用工具查出来的),然后把让这个Combo获得焦点。

我搜索了以前的帖子,并试验了一些。可是没有成功。郁闷。:)

呵呵。感谢大侠的帮助!!
...全文
108 点赞 收藏 26
写回复
26 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
snssh 2003-07-04
shell的基础知识,盼望讲解!!!!!!!!!!!
回复
cbxmir 2003-06-26
我也在试~
可以告诉我怎样关闭这个程序吗?
用postMessages
回复
goodname008 2003-06-26
大家多以后多交流,每个人都有自己会的和不会的,你赶上我会的了,呵呵呵呵........
回复
yunok 2003-06-26
呵呵。
30分:20分=goodname008(卢培培,想学好VB) :cnhgj(黄桂佳 → 吃软不吃硬)

这样好了。:)
回复
goodname008 2003-06-26
我的QQ:49743105
回复
cnhgj 2003-06-26
我不要。。我的办法不是那么好,把分给goodname008(卢培培,想学好VB)
回复
yunok 2003-06-26
解决了~揭帖!!!再次感谢!
cnhgj(黄桂佳 → 吃软不吃硬) ,goodname008(卢培培,想学好VB) 两位仁兄!!!

只是每人25分有些少。 因为我确实没分了。十分对不住阿!呵呵
回复
Alicky 2003-06-26
没办法呀。
顶一下。
回复
yunok 2003-06-26
晕~解决了~呵呵。
我是标准的傻X!我太弱智了,我怎么没想出来? cnhgj(黄桂佳 → 吃软不吃硬) ,goodname008(卢培培,想学好VB) 两位仁兄(仁妹),能不能告诉我你们的QQ?我的是 87266872 。

还是CSDN高人多的。

向VB高手学习!
回复
cnhgj 2003-06-26
学习!
回复
goodname008 2003-06-26
我也在线,快试试吧,呵呵
回复
yunok 2003-06-26
OK!!goodname008(卢培培,想学好VB) 朋友:我正在看您的代码并且试验。 :)

非常感谢你!!!
回复
goodname008 2003-06-26
'我刚在XP下调出来的,我这没问题,你试试吧
'改进了一下,那两个ShowWindow换了一下

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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 Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const WM_SETFOCUS = &H7

Private Sub Command1_Click()
Dim Handle As Long, topHandle As Long
Handle = FindWindow("CabinetWClass", "我的电脑") ' 只能是我的电脑,资源管理器不行
topHandle = Handle
Handle = FindWindowEx(Handle, 0&, "WorkerW", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ReBarWindow32", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ComboBoxEx32", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ComboBox", vbNullString)
Debug.Print Handle ' 这个Handle就是我的电脑的Combo的句柄了
SetForegroundWindow topHandle
SendMessage Handle, WM_SETFOCUS, 0, 0 ' 让那个Combo获得焦点
End Sub
回复
yunok 2003-06-26
呵呵。cnhgj(黄桂佳 → 吃软不吃硬) 朋友:您的方法也很好,不过有点投机取巧~呵呵,本来我是想取得子控件的句柄,然后用Api的Setfocus操作的。呵呵。

要是不太好做得话,我就用cnhgj您的方法,非常感谢你!!!
回复
goodname008 2003-06-26
'我刚在XP下调出来的,我这没问题,你试试吧
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
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 Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const WM_SETFOCUS = &H7

Private Sub Command1_Click()
Dim Handle As Long, topHandle As Long
Handle = FindWindow("CabinetWClass", "我的电脑") ' 只能是我的电脑,资源管理器不行
topHandle = Handle
Handle = FindWindowEx(Handle, 0&, "WorkerW", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ReBarWindow32", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ComboBoxEx32", vbNullString)
Handle = FindWindowEx(Handle, 0&, "ComboBox", vbNullString)
Debug.Print Handle ' 这个Handle就是我的电脑的Combo的句柄了
' 下面两句可能能用一个SendMessage代替,你查查吧,其功能是让我的电脑窗口为当前窗口
ShowWindow topHandle, 0
ShowWindow topHandle, 5

SendMessage Handle, WM_SETFOCUS, 0, 0 ' 让那个Combo获得焦点
End Sub
回复
cnhgj 2003-06-26
哦。。还要获得蕉点啊。。没用过。。不过可以试试用

SetForegroundWindow这个API,将焦点放在指定的窗体,然后用sendkeys "{tab}"


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long


Private Sub Command1_Click()
Dim hw As Long
hw = FindWindow(vbNullString, "我的电脑")
SetForegroundWindow (hw)
SendKeys "{tab}"
SendKeys "{tab}"
SendKeys "{tab}"
SendKeys "{tab}"
End Sub

先打开我的电脑,然后运行程序,然后你会看到焦点定在地址栏那里,我在win2000,vb6.0下通过
回复
cxwall 2003-06-26
关注!
回复
yunok 2003-06-26
恩!!谢谢cnhgj(黄桂佳 → 吃软不吃硬)!!然后呢?取得句柄后呢?
我想获得其下的Combo的句柄,并且把焦点给它~如何做?
回复
cnhgj 2003-06-26
取得窗体句柄

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
Dim hw As Long
hw = FindWindow(vbNullString, "我的电脑")
MsgBox hw
End Sub
回复
yunok 2003-06-26
spark_li(晕忽忽(恶人谷:通讯连长) 朋友: 不只是一个控件,必须取得句柄来操作。您好像没有看懂我的意思。:)
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告