由於集團填報預算的Excel插件使用的是側邊自定義面板,感覺這種形式恰好比較適合手頭的項目,所以把自己的插件改成側邊面板形式。 Excel側邊面板可以直接添加“用戶控制項(windows窗體)”格式,類為:System.Windows.Forms.UserControl,也可以引入WPF的控制項。 我創 ...
由於集團填報預算的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> { };