|
4#
楼主 |
发表于 2016-3-15 12:31:40
|
只看该作者
已经在网上找到代码了,谢谢老师!
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 'FindWindowAPI函数
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 'PostMessageAPI函数
Public Const WM_CLOSE = &H10 '常用变量定义
Private Sub Command2_Click()
Dim winHwnd As Long '定义一个长整形变量winHwnd
Dim RetVal As Long '定义一个长整形变量RetVal
winHwnd = FindWindow(vbNullString, "主窗体") 'API函数查找"主窗体"这个窗口
Debug.Print winHwnd '显示这个窗口句柄
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) '如果返回信息为0.表示失败,未发送成功
If winHwnd = 0 Then '如为0,表示没有找到窗口,那么就打开窗口
'打开窗体
End If
If winHwnd <> 0 Then '如不为0,表示找到窗口,那么就先关闭窗口,再重新打开窗体
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) '关闭窗体
'打开窗体
End If
End Sub
|
|