【问题标题】:MessageBox.Show() with owner argument带有所有者参数的 MessageBox.Show()
【发布时间】:2015-06-27 15:52:49
【问题描述】:

我想在 VB.NET 中使用 MessageBox.Show() 与 owner 参数如下:

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

据我了解,如果此代码采用 Windows 形式,您只需将“Me”(VB.NET)或“this”(C#)作为所有者参数传递。但是,我的代码是 Excel COM 加载项的一部分,并且所有者参数必须以某种方式绑定到特定的 Excel 窗口。

那么,如何获取 Excel 窗口对象并将其转换为可以传递给 VB.NET 中的 MessageBox.Show() 的 IWin32Window?如果我可以轻松地将其转换为 VB.NET,我将接受 C# 代码。

【问题讨论】:

标签: c# vb.net excel


【解决方案1】:

我已经将您指向C# version。只需编写它的 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 接口的变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2011-06-08
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多