VSTO側邊面板CustomTaskPanes


由於集團填報預算的Excel插件使用的是側邊自定義面板,感覺這種形式恰好比較適合手頭的項目,所以把自己的插件改成側邊面板形式。

Excel側邊面板可以直接添加“用戶控件(windows窗體)”格式,類為:System.Windows.Forms.UserControl,也可以引入WPF的控件。

我創建的窗體名稱為:RightPanel

默認的側邊面板是綁定工作簿的窗體的,不會自動切換,為了解決這個問題,創建了窗口句柄字典。最終實現效果如下

具體代碼如下


private CustomTaskPane RightPane { get; set; } //側邊面板

/// <summary>
/// 側邊面板開關
/// </summary>
private void PanelOnOff_Click(object sender, RibbonControlEventArgs e)
{
    ExcelApp = Globals.ThisAddIn.Application;
    int TempInt = Globals.ThisAddIn.Application.Hwnd;
    RefreshRightPane(TempInt);

    //設置面板可見性
    RightPane.Visible = PanelOnOff.Checked;
}
/// <summary>
/// 重新綁定右側面板
/// </summary>
/// <param name="HwndInt">當前窗體的句柄</param>
private void RefreshRightPane(int HwndInt)
{
    if (HwndPaneDic.ContainsKey(HwndInt))
    {
        RightPane = HwndPaneDic[HwndInt];
    }
    else
    {
        //創建控件
        UserControl rightPanel = new RightPanel();
        //添加控件
        RightPane = Globals.ThisAddIn.CustomTaskPanes.Add(rightPanel, "這里寫窗體名稱");
        //設置在右側顯示
        RightPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
        //禁止用戶修改位置
        RightPane.DockPositionRestrict = Microsoft.Office.Core.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
        //事件
        RightPane.VisibleChanged += new EventHandler(CustomPane_VisibleChanged);
        //添加到字典
        HwndPaneDic.Add(HwndInt, RightPane);
    }

}

/// <summary>
/// 側邊面板事件,用於保持按鈕與面板狀態一致
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CustomPane_VisibleChanged(object sender, System.EventArgs e)
{
    int TempInt = Globals.ThisAddIn.Application.Hwnd;
    PanelOnOff.Checked = RightPane.Visible;
    if (!PanelOnOff.Checked)
    {
        PanelOnOff.Label = "打開面板";
        PanelOnOff.ScreenTip = "點擊打開側邊面板";
        ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate);
        ExcelApp.WindowDeactivate -= new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate);
    }
    else
    {
        PanelOnOff.Label = "關閉面板";
        PanelOnOff.ScreenTip = "點擊關閉側邊面板";
        ExcelApp.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate);
        ExcelApp.WindowDeactivate += new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate);
    }
}

/// <summary>
/// 窗體激活事件
/// </summary>
/// <param name="WBK"></param>
/// <param name="WD"></param>
private void CustomPane_WindowActivate(Excel.Workbook WBK,Excel.Window WD)
{
    ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate);
    ExcelApp = Globals.ThisAddIn.Application;
    string WBKName = WBK.Name;
    
    int TempHwnd = WD.Hwnd;
    RefreshRightPane(TempHwnd);
    //設置面板可見性
    RightPane.Visible = true;

}

/// <summary>
/// 窗體取消激活事件
/// </summary>
/// <param name="WBK"></param>
/// <param name="WD"></param>
private void CustomPane_WindowDeactivate(Excel.Workbook WBK,Excel.Window WD)
{
    int TempHwnd = WD.Hwnd;
    if (HwndPaneDic.ContainsKey(TempHwnd))
    {
        HwndPaneDic[TempHwnd].Visible = false;
        ExcelApp.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate);
    }
}

//窗體句柄字典
private Dictionary<int, CustomTaskPane> HwndPaneDic = new Dictionary<int, CustomTaskPane> { };



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM