由于集團填報預算的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> { };
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/17059.html
標籤:C#
上一篇:c#泛型是什么
