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

标签 windows vb.net

你好 我正在使用 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/

相关文章:

c++ - n>0 的 Sleep(n) 是否会将 CPU 时间让给其他线程

linux - Fork() 在 Cygwin(在 Windows 上)和 Linux 中是不同的

vb.net - LINQ 选择新建

vb.net - 使用多个搜索参数进行搜索

c# - 如何为 web api 返回对象格式的数据?

.net - 有哪些免费或廉价的工具可以搜索/索引文件系统(使用 .Net)?

java - 在 Java 中获取我的文档路径

c# - Lockbits 跨越 1bpp 索引图像字节边界

c# - 从背后的 ASP.NET 代码与 iFrame 对话

.net - 在 VB.Net 中使用 Enter 键的 Tab 键功能