由于集團(tuán)填報(bào)預(yù)算的Excel插件使用的是側(cè)邊自定義面板,感覺這種形式恰好比較適合手頭的項(xiàng)目,所以把自己的插件改成側(cè)邊面板形式。
Excel側(cè)邊面板可以直接添加“用戶控件(windows窗體)”格式,類為:System.Windows.Forms.UserControl,也可以引入WPF的控件。
我創(chuàng)建的窗體名稱為:RightPanel
默認(rèn)的側(cè)邊面板是綁定工作簿的窗體的,不會(huì)自動(dòng)切換,為了解決這個(gè)問題,創(chuàng)建了窗口句柄字典。最終實(shí)現(xiàn)效果如下

具體代碼如下
private CustomTaskPane RightPane { get; set; } //側(cè)邊面板
/// <summary>
/// 側(cè)邊面板開關(guān)
/// </summary>
private void PanelOnOff_Click(object sender, RibbonControlEventArgs e)
{
ExcelApp = Globals.ThisAddIn.Application;
int TempInt = Globals.ThisAddIn.Application.Hwnd;
RefreshRightPane(TempInt);
//設(shè)置面板可見性
RightPane.Visible = PanelOnOff.Checked;
}
/// <summary>
/// 重新綁定右側(cè)面板
/// </summary>
/// <param name="HwndInt">當(dāng)前窗體的句柄</param>
private void RefreshRightPane(int HwndInt)
{
if (HwndPaneDic.ContainsKey(HwndInt))
{
RightPane = HwndPaneDic[HwndInt];
}
else
{
//創(chuàng)建控件
UserControl rightPanel = new RightPanel();
//添加控件
RightPane = Globals.ThisAddIn.CustomTaskPanes.Add(rightPanel, "這里寫窗體名稱");
//設(shè)置在右側(cè)顯示
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>
/// 側(cè)邊面板事件,用于保持按鈕與面板狀態(tài)一致
/// </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 = "點(diǎn)擊打開側(cè)邊面板";
ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate);
ExcelApp.WindowDeactivate -= new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate);
}
else
{
PanelOnOff.Label = "關(guān)閉面板";
PanelOnOff.ScreenTip = "點(diǎn)擊關(guān)閉側(cè)邊面板";
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);
//設(shè)置面板可見性
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> { };
|