VSTO側邊面板CustomTaskPanes

因爲集團填報預算的Excel插件使用的是側邊自定義面板,感受這種形式剛好比較適合手頭的項目,因此把本身的插件改爲側邊面板形式。windows

Excel側邊面板能夠直接添加「用戶控件(windows窗體)」格式,類爲:System.Windows.Forms.UserControl,也能夠引入WPF的控件。插件

我建立的窗體名稱爲:RightPanelcode

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

具體代碼以下blog

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> { };
相關文章
相關標籤/搜索