PowerPoint + VSTO 动态更改上下文菜单
PowerPoint + VSTO dynamically changing context menu
我想向上下文菜单添加一些按钮(可能还有其他类型的控件 - 我现在不确定),但前提是满足某些条件。因此,有可用的 Application.WindowBeforeRightClick 事件,但它仅在我单击幻灯片时触发,而不是在单击某些形状时触发。
到目前为止,我添加了一些 xml 作为彩带:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<contextMenus>
<contextMenu idMso="ContextMenuShape">
<button id="button1" label="button1"/>
</contextMenu>
<contextMenu idMso="ContextMenuObjectsGroup">
<button id="button2" label="Align vertical axes" onAction="AlignXAxis" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button3" label="Align horizontal axes" onAction="AlignYAxis" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button4" label="Align scale" onAction="MaxScale" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button5" label="Add legend" onAction="Legend" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button6" label="Adjust chart width" onAction="AdjustWidth" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button7" label="Adjust chart height" onAction="AdjustHeight" getVisible="ChangeCMObjectsGroupVisibility"/>
</contextMenu>
</contextMenus>
</customUI>
和代码:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.WindowBeforeRightClick += new PPT.EApplication_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick);
ribbon= (Ribbon1)CreateRibbonExtensibilityObject();
}
void Application_WindowBeforeRightClick(PPT.Selection Sel, ref bool Cancel)
{
PPT.Selection selection= Globals.ThisAddIn.Application.ActiveWindow.Selection;
if (selection.Type==PPT.PpSelectionType.ppSelectionShapes)
{
if (selection.ShapeRange.HasChart==MsoTriState.msoTrue)
{
ribbon.TurnOnChangeCMObjectsGroupVisibility();
}
}
}
是否可以检查所选形状的类型、hide/show/add/remove 控件然后显示正确的上下文菜单?
Application_WindowSelectionChange
事件将在您右键单击形状时触发。您可以在该事件处理程序中检查形状类型(或任何其他标准)。
为了更新上下文菜单,您需要使功能区失效。这会调用您的 ChangeCMObjectsGroupVisibility
回调并根据条件更新上下文菜单项的可见性。您可能需要使用全局变量向 ChangeCMObjectsGroupVisibility
回调提供已满足的标准。
我想向上下文菜单添加一些按钮(可能还有其他类型的控件 - 我现在不确定),但前提是满足某些条件。因此,有可用的 Application.WindowBeforeRightClick 事件,但它仅在我单击幻灯片时触发,而不是在单击某些形状时触发。
到目前为止,我添加了一些 xml 作为彩带:
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<contextMenus>
<contextMenu idMso="ContextMenuShape">
<button id="button1" label="button1"/>
</contextMenu>
<contextMenu idMso="ContextMenuObjectsGroup">
<button id="button2" label="Align vertical axes" onAction="AlignXAxis" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button3" label="Align horizontal axes" onAction="AlignYAxis" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button4" label="Align scale" onAction="MaxScale" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button5" label="Add legend" onAction="Legend" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button6" label="Adjust chart width" onAction="AdjustWidth" getVisible="ChangeCMObjectsGroupVisibility"/>
<button id="button7" label="Adjust chart height" onAction="AdjustHeight" getVisible="ChangeCMObjectsGroupVisibility"/>
</contextMenu>
</contextMenus>
</customUI>
和代码:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.WindowBeforeRightClick += new PPT.EApplication_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick);
ribbon= (Ribbon1)CreateRibbonExtensibilityObject();
}
void Application_WindowBeforeRightClick(PPT.Selection Sel, ref bool Cancel)
{
PPT.Selection selection= Globals.ThisAddIn.Application.ActiveWindow.Selection;
if (selection.Type==PPT.PpSelectionType.ppSelectionShapes)
{
if (selection.ShapeRange.HasChart==MsoTriState.msoTrue)
{
ribbon.TurnOnChangeCMObjectsGroupVisibility();
}
}
}
是否可以检查所选形状的类型、hide/show/add/remove 控件然后显示正确的上下文菜单?
Application_WindowSelectionChange
事件将在您右键单击形状时触发。您可以在该事件处理程序中检查形状类型(或任何其他标准)。
为了更新上下文菜单,您需要使功能区失效。这会调用您的 ChangeCMObjectsGroupVisibility
回调并根据条件更新上下文菜单项的可见性。您可能需要使用全局变量向 ChangeCMObjectsGroupVisibility
回调提供已满足的标准。