使用擴充性介面自訂 UI 功能Customize UI features by using extensibility interfaces
當您使用 Visual Studio 中的 Office 開發工具,在 VSTO 增益集中建立自訂工作窗格、功能區自訂和 Outlook 表單區域時,這些工具提供可處理許多實作詳細資料的類別和設計工具。The Office development tools in Visual Studio provide classes and designers that handle many implementation details when you use them to create custom task panes, ribbon customizations, and Outlook form regions in a VSTO Add-in. 不過,如果您有特殊需求,也可以針對每項功能自行實作 「擴充性介面」 (Extensibility Interface)。However, you can also implement the extensibility interface for each feature yourself if you have special requirements.
適用于: 本主題中的資訊適用于專案中的 VSTO [加入] - 。Applies to: The information in this topic applies to VSTO Add-in projects. 如需詳細資訊,請參閱 依 Office 應用程式和專案類型提供的功能。For more information, see Features available by Office application and project type.
擴充性介面的總覽Overview of extensibility interfaces
Microsoft Office 定義一組 COM VSTO 增益集可實作以自訂特定功能 (例如功能區) 的擴充性介面。Microsoft Office defines a set of extensibility interfaces that COM VSTO Add-ins can implement to customize certain features, such as the ribbon. 這些介面提供可存取功能的完整控制權。These interfaces provide full control over the features they provide access to. 然而,您需要對 Managed 程式碼中的 COM 互通性有些了解,才能實作這些介面。However, implementing these interfaces requires some knowledge of COM interoperability in managed code. 在某些情況下,這些介面的程式撰寫模型對熟悉 .NET Framework 的開發人員而言,也不是那麼直接易懂。In some cases, the programming model of these interfaces is also not intuitive for developers who are accustomed to the .NET Framework.
當您使用 Visual Studio 中的 Office 專案範本建立 VSTO 增益集時,您不需要實作擴充性介面來自訂功能區等功能。When you create a VSTO Add-in by using the Office project templates in Visual Studio, you do not have to implement the extensibility interfaces to customize features like the ribbon. Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 會為您實作這些介面。The Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime implements these interfaces for you. 相反地,您可以使用 Visual Studio 所提供之更直覺的類別和設計工具。Instead, you can use more intuitive classes and designers provided by Visual Studio. 不過,如果需要,您仍可直接在 VSTO 增益集中實作擴充性介面。However, you can still implement the extensibility interfaces directly in your VSTO Add-in if you want to.
如需 Visual Studio 針對這些功能所提供之類別和設計工具的詳細資訊,請參閱 自訂工作窗格、 功能區設計工具和 建立 Outlook 表單區域。For more information about the classes and designers that Visual Studio provides for these features, see Custom task panes, Ribbon designer, and Create Outlook form regions.
您可以在 VSTO 增益集中執行的擴充性介面Extensibility interfaces you can implement in a VSTO Add-in
下表列出您可以實作的擴充性介面,以及支援這些介面的應用程式。The following table lists the extensibility interfaces you can implement and the applications that support them.
介面Interface | 描述Description | 應用程式Applications |
---|---|---|
IRibbonExtensibility | 實作這個介面可自訂功能區 UI。Implement this interface to customize the ribbon UI. 注意: 您可以將 功能區 (XML) 專案加入至專案,以在 VSTO 增益集中產生預設的 IRibbonExtensibility 實值。Note: You can add a Ribbon (XML) item to a project to generate a default IRibbonExtensibility implementation in your VSTO Add-in. 如需詳細資訊,請參閱 Ribbon XML。For more information, see Ribbon XML. | ExcelExcel InfoPath 2013InfoPath 2013 InfoPath 2010InfoPath 2010 OutlookOutlook PowerPointPowerPoint ProjectProject VisioVisio WordWord |
ICustomTaskPaneConsumer | 實作這個介面可建立自訂工作窗格。Implement this interface to create a custom task pane. | ExcelExcel OutlookOutlook PowerPointPowerPoint WordWord |
FormRegionStartup | 實作這個介面可建立 Outlook 表單區域。Implement this interface to create an Outlook form region. | OutlookOutlook |
Microsoft Office 還定義其他幾個擴充性介面,例如 IBlogExtensibility、 EncryptionProvider和 SignatureProvider。There are several other extensibility interfaces that are defined by Microsoft Office, such as IBlogExtensibility, EncryptionProvider, and SignatureProvider. Visual Studio 不支援在使用 Office 專案範本建立的 VSTO 增益集中實作這些介面。Visual Studio does not support implementing these interfaces in a VSTO Add-in created by using the Office project templates.
使用擴充性介面Use extensibility interfaces
若要使用擴充性介面自訂 UI 功能,請在您的 VSTO 增益集專案中實作適當的介面。To customize a UI feature by using an extensibility interface, implement the appropriate interface in your VSTO Add-in project. 然後,覆寫 RequestService 方法以傳回實作介面的類別執行個體。Then, override the RequestService method to return an instance of the class that implements the interface.
如需示範如何 IRibbonExtensibility 在 OUTLOOK VSTO 增益集中執行、和介面的範例應用程式 ICustomTaskPaneConsumer FormRegionStartup ,請參閱 Office 程式開發範例中的 UI 管理員範例。For a sample application that demonstrates how to implement the IRibbonExtensibility, ICustomTaskPaneConsumer, and FormRegionStartup interfaces in a VSTO Add-in for Outlook, see the UI Manager Sample in Office development samples.
執行擴充性介面的範例Example of implementing an extensibility interface
下列程式碼範例示範 ICustomTaskPaneConsumer 介面的簡單實作,以建立自訂工作窗格。The following code example demonstrates a simple implementation of the ICustomTaskPaneConsumer interface to create a custom task pane. 這個範例會定義兩個類別:This example defines two classes:
TaskPaneHelper
類別實作 ICustomTaskPaneConsumer ,以建立及顯示自訂工作窗格。TheTaskPaneHelper
class implements ICustomTaskPaneConsumer to create and display a custom task pane.TaskPaneUI
類別提供工作窗格的 UI。TheTaskPaneUI
class provides the UI of the task pane.TaskPaneUI
類別的屬性讓 COM 可以看見該類別,進而讓 Microsoft Office 應用程式可以探索該類別。The attributes for theTaskPaneUI
class make the class visible to COM, which enables Microsoft Office applications to discover the class. 在這個範例中,UI 是空的 UserControl,但是您可以透過修改程式碼來加入控制項。In this example, the UI is an empty UserControl, but you can add controls by modifying the code.注意
若要將
TaskPaneUI
類別公開至 COM,您也必須設定專案的 [註冊 COM Interop] 屬性。To expose theTaskPaneUI
class to COM, you must also set the Register for COM Interop property for the project.Public Class TaskPaneHelper Implements Office.ICustomTaskPaneConsumer Friend taskPane As Office.CustomTaskPane Public Sub CTPFactoryAvailable(ByVal CTPFactoryInst As Office.ICTPFactory) _ Implements Office.ICustomTaskPaneConsumer.CTPFactoryAvailable If CTPFactoryInst IsNot Nothing Then ' Create a new task pane. taskPane = CTPFactoryInst.CreateCTP( _ "Microsoft.Samples.Vsto.VB.TaskPaneUI", "Contoso") taskPane.Visible = True End If End Sub End Class <System.Runtime.InteropServices.ComVisible(True)> _ <System.Runtime.InteropServices.ProgId("Microsoft.Samples.Vsto.VB.TaskPaneUI")> _ <System.Runtime.InteropServices.Guid("FFA0920E-F7A5-453d-8AB2-249F4C25B4B2")> _ Public Class TaskPaneUI Inherits UserControl End Class
public class TaskPaneHelper : Office.ICustomTaskPaneConsumer { internal Office.CustomTaskPane taskPane; public void CTPFactoryAvailable(Office.ICTPFactory CTPFactoryInst) { if (CTPFactoryInst != null) { // Create a new task pane. taskPane = CTPFactoryInst.CreateCTP( "Microsoft.Samples.Vsto.CS.TaskPaneUI", "Contoso"); taskPane.Visible = true; } } } [System.Runtime.InteropServices.ComVisible(true)] [System.Runtime.InteropServices.ProgId("Microsoft.Samples.Vsto.CS.TaskPaneUI")] [System.Runtime.InteropServices.Guid("FFA0920E-F7A5-453d-8AB2-249F4C25B4B2")] public class TaskPaneUI : UserControl { }
如需有關如何執行的詳細資訊 ICustomTaskPaneConsumer ,請參閱 Microsoft Office 檔中的在 2007 Office 系統中建立自訂工作窗格 。For more information about implementing ICustomTaskPaneConsumer, see Create custom task panes in the 2007 Office system in the Microsoft Office documentation.
覆寫 RequestService 方法的範例Example of overriding the RequestService method
下列程式碼範例示範如何覆寫 RequestService 方法,以從先前的程式碼範例傳回 TaskPaneHelper
類別的執行個體。The following code example demonstrates how to override the RequestService method to return an instance of the TaskPaneHelper
class from the previous code example. 它會檢查 serviceGuid 參數的值以判斷所要求的介面,然後傳回實作該介面的物件。It checks the value of the serviceGuid parameter to determine which interface is being requested, and then returns an object that implements that interface.
Friend taskPaneHelper1 As TaskPaneHelper
Protected Overrides Function RequestService( _
ByVal serviceGuid As Guid) As Object
If (serviceGuid = GetType(Office.ICustomTaskPaneConsumer).GUID) Then
If (taskPaneHelper1 Is Nothing) Then
taskPaneHelper1 = New TaskPaneHelper()
End If
Return taskPaneHelper1
End If
Return MyBase.RequestService(serviceGuid)
End Function
internal TaskPaneHelper taskPaneHelper1;
protected override object RequestService(Guid serviceGuid)
{
if (serviceGuid == typeof(Office.ICustomTaskPaneConsumer).GUID)
{
if (taskPaneHelper1 == null)
{
taskPaneHelper1 = new TaskPaneHelper();
}
return taskPaneHelper1;
}
return base.RequestService(serviceGuid);
}
另請參閱See also
- Office 開發範例和逐步解說Office development samples and walkthroughs
- 程式 VSTO 增益集Program VSTO Add-ins
- 開發 Office 方案Develop Office solutions
- 從其他 Office 方案呼叫 VSTO 增益集的程式碼Call code in VSTO Add-ins from other Office solutions
- 如何:在 Visual Studio 中建立 Office 專案How to: Create Office projects in Visual Studio
- VSTO 增益集的架構Architecture of VSTO Add-ins