Note: Advice in this article will only work for DotNetBrowser 1.
As a regular WinForms control, the BrowserView
implementation provided in DotNetBrowser is compatible with ExcelDNA library.
The following sample code demonstrates how to add DotNetBrowser in Excel Add-Ins using ExcelDNA:
C#
using System.Runtime.InteropServices; using System.Windows.Forms; using ExcelDna.Integration.CustomUI; using DotNetBrowser; using DotNetBrowser.WinForms; namespace DotNetBrowserTab { [ComVisible(true)] public class RibbonController : ExcelRibbon { public override string GetCustomUI(string RibbonID) { return @" <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'> <ribbon> <tabs> <tab id='dotnetbrowser_tab' label='DotNetBrowser Tab'> <group id='dotnetbrowser_group' label='DotNetBrowser Group'> <button id='dotnetbrowser_open' label='Open DotNetBrowser' onAction='OnShowCTP'/> <button id='dotnetbrowser_close' label='Close DotNetBrowser' onAction='OnDeleteCTP'/> </group > </tab> </tabs> </ribbon> </customUI>"; } public void OnShowCTP(IRibbonControl control) { CTPManager.ShowCTP(); } public void OnDeleteCTP(IRibbonControl control) { CTPManager.DeleteCTP(); } } /////////////// Define the UserControl to display on the CTP /////////////////////////// // Would need to be marked with [ComVisible(true)] if in a project // that is marked as [assembly:ComVisible(false)] which is the default for VS projects. [ComVisible(true)] public class DotNetBrowserControl : UserControl { public BrowserView browserView; public DotNetBrowserControl() { browserView = new WinFormsBrowserView(); browserView.Browser.LoadURL("www.teamdev.com/dotnetbrowser"); Controls.Add((Control)browserView); } //Ovverrided for focus protected override void WndProc(ref Message m) { if (m.Msg == 528 && !Focus()) { //Set this property only for heavyweight render mode browserView.Focused = true; Focus(); } base.WndProc(ref m); } } /////////////// Helper class to manage CTP /////////////////////////// internal static class CTPManager { static CustomTaskPane ctp; public static void ShowCTP() { if (ctp == null) { // Make a new one using ExcelDna.Integration.CustomUI.CustomTaskPaneFactory ctp = CustomTaskPaneFactory.CreateCustomTaskPane(typeof(DotNetBrowserControl), "DotNetBrowser"); ctp.Visible = true; ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight; } else { // Just show it again ctp.Visible = true; } } public static void DeleteCTP() { if (ctp != null) { // Could hide instead, by calling ctp.Visible = false; ctp.Delete(); ctp = null; } } } }
VB.NET
Imports System.Runtime.InteropServices Imports System.Windows.Forms Imports ExcelDna.Integration.CustomUI Imports DotNetBrowser Imports DotNetBrowser.WinForms Namespace DotNetBrowserTab <ComVisible(True)> Public Class RibbonController Inherits ExcelRibbon Public Overrides Function GetCustomUI(ByVal RibbonID As String) As String Return _ " <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui'> <ribbon> <tabs> <tab id='dotnetbrowser_tab' label='DotNetBrowser Tab'> <group id='dotnetbrowser_group' label='DotNetBrowser Group'> <button id='dotnetbrowser_open' label='Open DotNetBrowser' onAction='OnShowCTP'/> <button id='dotnetbrowser_close' label='Close DotNetBrowser' onAction='OnDeleteCTP'/> </group > </tab> </tabs> </ribbon> </customUI>" End Function Public Sub OnShowCTP(ByVal control As IRibbonControl) CTPManager.ShowCTP() End Sub Public Sub OnDeleteCTP(ByVal control As IRibbonControl) CTPManager.DeleteCTP() End Sub End Class <ComVisible(True)> Public Class DotNetBrowserControl Inherits UserControl Public browserView As BrowserView Public Sub New() browserView = New WinFormsBrowserView() browserView.Browser.LoadURL("www.teamdev.com/dotnetbrowser") Controls.Add(CType(browserView, Control)) End Sub Protected Overrides Sub WndProc(ByRef m As Message) If m.Msg = 528 AndAlso Not Focus() Then browserView.Focused = True Focus() End If MyBase.WndProc(m) End Sub End Class Friend Module CTPManager Shared ctp As CustomTaskPane Sub ShowCTP() If ctp Is Nothing Then ctp = CustomTaskPaneFactory.CreateCustomTaskPane(GetType(DotNetBrowserControl), "DotNetBrowser") ctp.Visible = True ctp.DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight Else ctp.Visible = True End If End Sub Sub DeleteCTP() If ctp IsNot Nothing Then ctp.Delete() ctp = Nothing End If End Sub End Module End Namespace