【问题标题】:vba using win32 api, showwindow function closes windowvba使用win32 api,showwindow函数关闭窗口
【发布时间】:2022-01-21 14:45:43
【问题描述】:
#If VBA7 And Win64 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
#End If

Private Sub close_window()

Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
'Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, "somedocument.docx - Word")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)

'Show the classname
MsgBox "Classname: " + Left$(lpClassName, RetVal)
'Post a message to the window to close itself

'PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

上述代码的目的是关闭 Word 文档。但是,即使评论PostMessage WinWnd, WM_CLOSE, 0&, 0& 如上所示,单词 doc 确实关闭了,而取消注释 postmessage 命令时,窗口并没有关闭。我已经得出结论(通过逐行注释上面的代码)ShowWindow WinWnd,SW_SHOWNORMAL 函数关闭窗口(word 文档)。这正常吗?

【问题讨论】:

  • Word 公开了一个object model,因此您不必摆弄窗口句柄来与之交互。
  • FindWindow API 应该返回一个LongPtr。所以你的函数声明不正确。那么,你是想玩 API 函数,还是真的想以这种方式退出 Word?如果您不想尝试某些 API 调用,还有其他更合适的方法可以做到这一点。如果这是你真正想要的,我可以告诉你。
  • FaneDuru 我已经相应地更改了函数和变量的声明,现在它就像一个魅力!非常感谢大家!
  • 好的。很高兴我能帮上忙,但你没有回答我关于你想要什么的问题。使用 API 可以很好地学习,但这是你想要的吗?
  • FaneDuru 主要是为了学习!

标签: vba winapi


【解决方案1】:

这里按照 FaneDuru 的建议是成功完成任务的代码:

#If VBA7 And Win64 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long

#End If

Private Sub Form_Load()

Dim WinWnd As LongPtr, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
'Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
 WinWnd = FindWindow(vbNullString, "some_document.docx - Word")
 If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
 'Show the window
 ShowWindow WinWnd, SW_SHOWNORMAL
 'Create a buffer
 lpClassName = Space(256)
 'retrieve the class name
 'RetVal = GetClassName(WinWnd, lpClassName, 256)

 'Show the classname
 'MsgBox "Classname: " + Left$(lpClassName, RetVal)
 'Post a message to the window to close itself

 PostMessage WinWnd, WM_CLOSE, 0&, 0&
 End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多