你好,我在用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
,谁能告诉我为什么?如何让它工作,隐藏窗口,然后再展示它呢?
发布于 2011-05-01 09:02:47
因为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。
进程只有在进程具有图形界面的情况下才具有与其关联的主窗口。如果关联进程没有主窗口,则MainWindowHandle值为零。对于已隐藏的进程,即在任务栏中不可见的进程,值也为零。如果进程在任务栏的最右侧显示为通知区域中的图标,则可以使用。
您应该做的是通过p(0).MainWindowHandle
一次获得窗口句柄,并将返回的句柄保存在某个地方,以便显示/隐藏窗口。当然,当窗口被目标进程破坏时,您应该确保将保存的句柄调零。在记事本的情况下,您可以使用Process::Exited
event。
发布于 2011-05-01 09:03:09
听起来像是“精心设计”。不要将"false“的返回值视为错误。
根据MSDN:
If the window was previously visible, the return value is nonzero.
If the window was previously hidden, the return value is zero.
http://msdn.microsoft.com/en-us/library/ms633549%28VS.85%29.aspx
您可以通过将函数的互操作导入声明为32位整数类型而不是布尔值类型来阻止异常发生。
Private Shared Function ShowWindowAsync(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
发布于 2011-05-15 17:16:03
当我藏起来的时候:
WindowHandle = Proc(0).MainWindowHandle
ShowWindowAsync(Proc(0).MainWindowHandle, ShowWindowConstants.SW_HIDE)
然后在显示我使用WindowHandle
变量时:
ShowWindowAsync(WindowHandle, ShowWindowConstants.SW_SHOW)
https://stackoverflow.com/questions/5847481
复制相似问题