【发布时间】:2018-07-21 00:15:21
【问题描述】:
我们有一个在某些情况下可能会启动 Outlook 的应用程序。它获取一个 Outlook.Explorer 对象 (oWindow As Outlook.Explorer) 并调用其 .Activate() 过程。
到目前为止,一切都是 hunky dory,但是我想把 Explorer 对象带到前台,我们称之为这个过程:
Public Shared Sub BringToFore(ByVal oWindow As Object)
Dim oFoo As IOleWindow
Dim hWnd As IntPtr
oFoo = TryCast(oWindow, IOleWindow)
If Not (oFoo Is Nothing) Then
Try
oFoo.GetWindow(hWnd)
Catch ex As Exception
End Try
End If
Try
If hWnd.ToInt32 <> IntPtr.Zero.ToInt32 Then
Try
If IsIconic(hWnd) Then
ShowWindow(hWnd, SW_RESTORE)
End If
SetForegroundWindow(hWnd)
Catch ex As System.Exception
End Try
Else
End If
Catch ex As Exception
End Try
End Sub
IOleWindow 定义如下:
<ComImport(), Guid("00000114-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Private Interface IOleWindow
''' <summary>
''' Returns the window handle to one of the windows participating in in-place activation
''' (frame, document, parent, or in-place object window).
''' </summary>
''' <param name="phwnd">Pointer to where to return the window handle.</param>
Sub GetWindow(<System.Runtime.InteropServices.Out()> ByRef phwnd As IntPtr)
''' <summary>
''' Determines whether context-sensitive help mode should be entered during an
''' in-place activation session.
''' </summary>
''' <param name="fEnterMode"><c>true</c> if help mode should be entered;
''' <c>false</c> if it should be exited.</param>
Sub ContextSensitiveHelp(<[In](), MarshalAs(UnmanagedType.Bool)> ByVal fEnterMode As Boolean)
End Interface
以及通常的声明
Private Const SW_RESTORE As Integer = 9
Private Declare Auto Function IsIconic Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Auto Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
Private Declare Auto Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As IntPtr
BringToFore 过程在大多数情况下都能正常工作。有时调用应用程序 - WPF 应用程序 - 只是挂起。事件查看器记录 AppHangB1 并且应用程序崩溃。
我可以在 BringToFore 程序中做些什么来防止这种情况发生吗?知道其中哪些是导致问题的原因吗? TryCast(oWindow, IOleWindow), oFoo.GetWindow(hWnd), IsIconic(hWnd), ShowWindow(hWnd, SW_RESTORE) 还是 SetForegroundWindow(hWnd)? (老实说,我个人怀疑它可能是 SetForegroundWindow)。如果是这样,我可以在代码中做些什么吗?有什么条件要检查?如果 this 条件为真,那么不要尝试做 that ..... 之类的事情?我宁愿不要放弃将 Explorer 设置为前台的想法。在这种情况下,Outlook 可能会出现在我的应用程序“后面”,而一些用户可能只是没有意识到发生了一些事情......如果你发现我的想法。
谢谢
皮诺
【问题讨论】:
标签: vb.net visual-studio outlook