gpt4 book ai didi

windows - ShowWindowAsync() 不显示隐藏窗口 (SW_SHOW)

转载 作者:可可西里 更新时间:2023-11-01 11:14:29 26 4
gpt4 key购买 nike

你好我正在使用 Visual Basic 2008

这是我的部分代码:

    Private Const SW_HIDE As Integer = 0
Private Const SW_SHOW As Integer = 5

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function ShowWindowAsync(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
End Function

button1 代码:

            Try
Dim p() As Process = Process.GetProcessesByName("notepad")
Dim mwh = p(0).MainWindowHandle
ShowWindowAsync(mwh, SW_HIDE)
Catch ex As Exception
MsgBox("error")
End Try

button2 代码:

            Try         
Dim p() As Process = Process.GetProcessesByName("notepad")
Dim mwh = p(0).MainWindowHandle
ShowWindowAsync(mwh, SW_SHOW)
Catch ex As Exception
MsgBox("error")
End Try

当我点击button 1(隐藏窗口)时效果很好,但是当我想要显示窗口回来时(通过点击button 2)函数返回FALSE,谁能告诉我为什么?以及如何让它工作,隐藏窗口然后再次显示它?

最佳答案

因为 ShowWindowAsync() returns zero when the window was previously hidden according to the MSDN documentation , 零被解释为 FALSE。返回值不表示函数是否成功。

因此,当您第一次在可见窗口上调用 ShowWindowAsync(mwh, SW_HIDE) 时,函数会返回 TRUE,因为 ShowWindowAsync() 会返回一个非零值表示窗口(现在/将被隐藏)曾经是可见的。

然后在隐藏窗口上第二次调用 ShowWindowAsync(mwh, SW_SHOW) 返回 FALSE 因为 ShowWindowAsync() 返回一个零值表明窗口(现在/将可见)过去是隐藏的。

换句话说,这是设计使然,该功能应该按预期工作(除非 mwh 窗口不响应消息或无效)。

但这里真正的问题是一旦你隐藏了一个窗口,Process::MainWindowHandle property doesn't have the handle anymore .

A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.

你应该做的是通过p(0).MainWindowHandle 获取一次窗口句柄,并将返回的句柄保存在某个地方,这样你就可以显示/隐藏窗口。当然,您应该确保在窗口被目标进程销毁时将保存的句柄归零。在记事本的情况下,您可以使用 Process::Exited event .

关于windows - ShowWindowAsync() 不显示隐藏窗口 (SW_SHOW),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5847481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com