VSTO侧边面板CustomTaskPanes

由于集团填报预算的Excel插件使用的是侧边自定义面板,感觉这种形式恰好比较适合手头的项目,所以把自己的插件改成侧边面板形式。

Excel侧边面板可以直接添加“用户控件(windows窗体)”格式,类为:System.Windows.Forms.UserControl,也可以引入WPF的控件。

我创建的窗体名称为:RightPanel

默认的侧边面板是绑定工作簿的窗体的,不会自动切换,为了解决这个问题,创建了窗口句柄字典。最终实现效果如下

VSTO侧边面板CustomTaskPanes_第1张图片

具体代码如下


private CustomTaskPane RightPane { get; set; } //侧边面板

/// 
/// 侧边面板开关
/// 
private void PanelOnOff_Click(object sender, RibbonControlEventArgs e)
{
    ExcelApp = Globals.ThisAddIn.Application;
    int TempInt = Globals.ThisAddIn.Application.Hwnd;
    RefreshRightPane(TempInt);

    //设置面板可见性
    RightPane.Visible = PanelOnOff.Checked;
}
/// 
/// 重新绑定右侧面板
/// 
/// 当前窗体的句柄
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);
    }

}

/// 
/// 侧边面板事件,用于保持按钮与面板状态一致
/// 
/// 
/// 
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);
    }
}

/// 
/// 窗体激活事件
/// 
/// 
/// 
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;

}

/// 
/// 窗体取消激活事件
/// 
/// 
/// 
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 HwndPaneDic = new Dictionary { };


你可能感兴趣的:(VSTO侧边面板CustomTaskPanes)