VSTO侧边⾯板CustomTaskPanes
由于集团填报预算的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)
{pane
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_Win
dowActivate);
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> { };
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论