博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VB用windows API激活子窗体
阅读量:5895 次
发布时间:2019-06-19

本文共 2885 字,大约阅读时间需要 9 分钟。

setforegroundwindow只能激活桌面级的父窗体,即使后面跟的hwnd是子窗体的hwnd也不行!

激活子窗体,可以先用setforegroundwindow把父窗体带到前台,然后用以下消息激活子窗体:

 'Private Const WM_MOUSEACTIVATE=&H21

SendMessage thwnd, WM_MOUSEACTIVATE, 0, 0  

'Private Const WM_LBUTTONDOWN = &H201

SendMessage tHwnd, WM_LBUTTONDOWN, 0, 0 

或者直接用SendMessage thwnd, 33, 0, 0来激活子窗体。

在form1下面新建3个command按钮,然后打开excel,新建book1,book2,把焦点切换到book1,然后测试以下代码,会把焦点切换到book2:

'form1的代码:

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 SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) 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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const WM_MOUSEACTIVATE = &H21
Private Const WM_LBUTTONDOWN = &H201

Private Sub Command1_Click()
' Call SetNumLock(False) '关闭NumLock键
' CALL MySendKey (vbKeyCapital)
' MsgBox GetAsyncKeyState(vbKeyCapital)
If GetKeyState(vbKeyCapital) = 1 Then '如果CAPSLOCK被按下
SendKeys "{CAPSLOCK}", False
' Call MySendKey(vbKeyCapital)
' ScanCode = MapVirtualKey(vbKeyCapital, 0)
' Call keybd_event(vbKeyCapital, ScanCode, 0, 0)
' Sleep 1000
' Call keybd_event(vbKeyCapital, ScanCode, KEYEVENTF_KEYUP, 0)
' Sleep 1000
End If
Dim dHwnd As Long
Dim tHwnd As Long
dHwnd = FindWindow("XLMAIN", vbNullString)
If dHwnd > 0 Then
tHwnd = FindWindowEx(dHwnd, ByVal 0&, "XLDESK", vbNullString)
If tHwnd > 0 Then
tHwnd = FindWindowEx(tHwnd, ByVal 0&, vbNullString, "Book2")
End If
End If
If tHwnd = 0 Then
MsgBox "请先新建两个空白excel,名字叫做book1,book2"
Exit Sub
End If
SetForegroundWindow dHwnd '激活父窗体
Sleep 1000 '等待窗口切换
'Private Const WM_MOUSEACTIVATE = &H21
' SendMessage tHwnd, WM_MOUSEACTIVATE, 0, 0
' Private Const WM_LBUTTONDOWN = &H201
SendMessage tHwnd, WM_LBUTTONDOWN, 0, 0
' Private Const WM_PARENTNOTIFY = &H210
' Private Const WM_ACTIVATE = &H6
' SendMessage hwnd4, WM_ACTIVATE, 0, 0
' Private Const WM_LBUTTONDOWN = &H201
' SendMessage hwnd4, WM_LBUTTONDOWN, 0, 0
Sleep 1000 '等待窗口切换
' SendKeys "{CAPSLOCK}", FALSE
Sleep 1000
SendKeys "{enter}"
SendKeys "AAA", False
SendKeys "bbb", False
SendKeys "CCC", False
SendKeys "ddd", False
SendKeys "EEE", False
End Sub

Private Sub Command2_Click()

End
End Sub

Private Sub Command3_Click()

dHwnd = FindWindow("Notepad", vbNullString)
If dHwnd > 0 Then
tHwnd = FindWindowEx(dHwnd, ByVal 0&, "Edit", vbNullString)
End If
SetForegroundWindow dHwnd '激活父窗体
MySendKey vbKeyA
End Sub

转载地址:http://ovxsx.baihongyu.com/

你可能感兴趣的文章
Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
查看>>
不同页面之间实现参数传递的几种方式讨论
查看>>
程序员进阶之路—如何独当一面
查看>>
SpringMVC中ModelAndView addObject()设置的值jsp取不到的问题
查看>>
Prometheus : 入门
查看>>
使用 PowerShell 创建和修改 ExpressRoute 线路
查看>>
PHP如何学习?
查看>>
谈教育与成长
查看>>
jni c++
查看>>
在C#中获取如PHP函数time()一样的时间戳
查看>>
Redis List数据类型
查看>>
大数据项目实践(四)——之Hive配置
查看>>
Thread类源码解读(1)——如何创建和启动线程
查看>>
Bootstrap清除浮动的实现原理
查看>>
初学vue2.0-组件-文档理解笔记v1.0
查看>>
NG-ZORRO-MOBILE 0.11.9 发布,基于 Angular 7 的 UI 组件
查看>>
我就是一个救火员(DBA救援)
查看>>
Centos7安装Gitlab10.0
查看>>
Windows Server 笔记(六):Active Directory域服务:域控制器安装
查看>>
discuz X3登录流程分析
查看>>