如何为Excel-DNA开发的Excel插件创建引导式产品导览
Hey there! Let’s walk through how you can build that polished guided tour experience for your Excel-DNA plugin—just like the one AbleBits uses. I’ve broken down the best tools and steps to get you started:
Since you’re working with Excel-DNA (a .NET-based framework), these libraries are perfect for embedding interactive tours:
- TourGuide.NET: A dedicated .NET library for guided tours, supporting WinForms/WPF. It handles semi-transparent overlays, element highlighting, tooltip-style descriptions, and navigation buttons out of the box. It’s designed to work seamlessly with desktop apps like Excel.
- Intro.js.NET: A lightweight .NET wrapper for the popular Intro.js library. It’s easy to configure, supports custom overlay styles, and can target specific UI elements (like your Ribbon buttons) with minimal code.
- Custom WinForms/WPF Build: If you want full control over every detail, you can roll your own solution using WinForms forms for overlays and Windows API calls to target UI elements. This is great if you need highly customized behavior that off-the-shelf libraries don’t offer.
The key to replicating AbleBits’ tour is layering a semi-transparent mask over Excel, with "holes" for the features you want to highlight, plus navigation controls and text prompts. Here’s the core workflow:
- Detect First Run: Check if the user is launching the plugin for the first time (use the registry, a config file, or Excel’s built-in settings to track this preference).
- Capture Excel’s Main Window: Use
ExcelDnaUtil.Applicationto get the Excel instance, then grab its HWND (window handle) to anchor your overlay correctly. - Create the Overlay Mask: Add a full-screen, semi-transparent form on top of Excel. Use a
Regionobject to cut out the area of your target feature (e.g., a Ribbon button) so it stays fully visible against the dark mask. - Add Tour Content: Place text labels, icons, and navigation buttons ("Next", "Back", "Finish") on the overlay. Position them relative to the highlighted feature for maximum clarity.
- Handle Navigation: Wire up the buttons to switch between tour steps, updating the overlay’s highlighted region and content each time the user navigates.
- Mark Tour as Completed: Once the user finishes or skips the tour, save that preference so it doesn’t show again on future launches.
Using TourGuide.NET (Simplest Approach)
First, install the TourGuide.NET NuGet package in your Excel-DNA project. Then add this basic setup to your add-in:
using ExcelDna.Integration; using TourGuide; using System.Drawing; using System.Windows.Forms; public class MyExcelAddIn : IExcelAddIn { private TourManager _tourManager; public void AutoOpen() { if (IsFirstRun()) { // Get Excel's window handle var excelApp = ExcelDnaUtil.Application as Microsoft.Office.Interop.Excel.Application; IntPtr excelHwnd = new IntPtr(excelApp.Hwnd); // Initialize tour manager _tourManager = new TourManager(excelHwnd); // Add first tour step (highlight a Ribbon button) var featureButtonRect = GetRibbonButtonScreenRect("MyPluginTab", "MyCoreFeatureBtn"); _tourManager.AddStep(new TourStep { HighlightRect = featureButtonRect, Title = "Welcome to My Plugin!", Description = "Click this button to access our core data analysis tools.", Position = TourStepPosition.Bottom }); // Add more steps as needed... // Start the tour _tourManager.StartTour(); // Mark tour as completed SetTourCompleted(); } } public void AutoClose() => _tourManager?.Dispose(); // Helper: Check if user is running the plugin for the first time private bool IsFirstRun() { var registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\MyExcelPlugin"); return registryKey.GetValue("TourCompleted") == null; } // Helper: Save tour completion status private void SetTourCompleted() { var registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\MyExcelPlugin"); registryKey.SetValue("TourCompleted", true); } // Helper: Get screen coordinates of a Ribbon button private Rectangle GetRibbonButtonScreenRect(string tabId, string buttonId) { // Use Windows API or Office Ribbon callbacks to find the button's position // Example placeholder (replace with actual logic): return new Rectangle(250, 120, 180, 60); } }
Custom WinForms Overlay (Full Control)
If you want to build it from scratch, here’s a snippet for the overlay mask:
using ExcelDna.Integration; using System.Drawing; using System.Windows.Forms; private void LaunchCustomTour() { var excelApp = ExcelDnaUtil.Application as Microsoft.Office.Interop.Excel.Application; IntPtr excelHwnd = new IntPtr(excelApp.Hwnd); var excelWindow = Control.FromHandle(excelHwnd); // Create semi-transparent overlay var overlay = new Form { FormBorderStyle = FormBorderStyle.None, WindowState = FormWindowState.Maximized, Opacity = 0.75, BackColor = Color.Black, TopMost = true, Owner = excelWindow }; // Cut out a hole for the highlighted feature var highlightArea = GetRibbonButtonScreenRect("MyPluginTab", "MyCoreFeatureBtn"); var overlayRegion = new Region(new Rectangle(0, 0, overlay.Width, overlay.Height)); overlayRegion.Exclude(highlightArea); overlay.Region = overlayRegion; // Add tour description label var descriptionLabel = new Label { Text = "This button lets you batch-process spreadsheets in one click!", ForeColor = Color.White, Font = new Font("Segoe UI", 12), AutoSize = true, Location = new Point(highlightArea.Right + 30, highlightArea.Top) }; overlay.Controls.Add(descriptionLabel); // Add "Next" button var nextBtn = new Button { Text = "Next", Size = new Size(80, 35), Location = new Point(overlay.Width - 100, overlay.Height - 60), ForeColor = Color.White, BackColor = Color.DodgerBlue }; nextBtn.Click += (s, e) => { // Update overlay for next step here overlay.Close(); // Replace with step transition logic }; overlay.Controls.Add(nextBtn); overlay.ShowDialog(); }
- Excel Version Compatibility: Ribbon layouts can vary between Excel versions (2016, 2019, 365). Test your tour across versions to ensure highlight positions are accurate.
- Thread Safety: Excel runs on a single UI thread—make sure all overlay and tour logic runs on that thread (use
ExcelDnaUtil.InvokeIfRequiredif you’re running code in a background context). - User Control: Always add a "Skip Tour" button so users can bypass the guide if they prefer to dive straight into the plugin.
- Performance: Keep overlay elements lightweight to avoid slowing down Excel. Avoid heavy animations or complex UI on the mask layer.
内容的提问来源于stack exchange,提问作者user171943




