MessageBox.Show()与所有者参数

我想在VB.NET中使用MessageBox.Show()和owner参数,如下所示:

MessageBox.Show(owner As IWin32Window,...) 

这是我的理解,如果这个代码是在Windows窗体中,您只需传递“我”(VB.NET)或“this”(C#)作为所有者参数。 但是,我的代码是Excel COM加载项的一部分,owner参数必须以某种方式绑定到特定的Excel窗口。

那么,我怎样才能把一个Excel窗口对象,并将其转换为IWin32Window,我可以传递给VB.NET中的MessageBox.Show()? 我会解决C#代码,如果我可以轻松地将其转换为VB.NET。

我已经指出你的C#版本 。 只需编写它的VB.NET版本:

 Public Class WindowWrapper Implements IWin32Window Private hwnd As IntPtr Public Sub New(handle As IntPtr) hwnd = handle End Sub Public Sub New(handle As Integer) hwnd = New IntPtr(handle) End Sub Public ReadOnly Property Handle As IntPtr Implements IWin32Window.Handle Get Return hwnd End Get End Property End Class 

并使用像:

 MessageBox.Show(New WindowWrapper(app.Hwnd), _ '' other arguments... _ ) 

其中app是存储Microsoft.Office.Interop.Excel.Application接口的variables。