DesignerOptionService 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用于获取和设置设计器选项值的基类。
public ref class DesignerOptionService abstract : System::ComponentModel::Design::IDesignerOptionService
public abstract class DesignerOptionService : System.ComponentModel.Design.IDesignerOptionService
type DesignerOptionService = class
interface IDesignerOptionService
Public MustInherit Class DesignerOptionService
Implements IDesignerOptionService
- 继承
-
DesignerOptionService
- 派生
- 实现
示例
下面的代码示例演示如何访问 DesignerOptionService 以显示标准选项的当前值。
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace DesignerOptionServiceExample
{
// This control demonstrates retrieving the standard
// designer option service values in design mode.
public class DesignerOptionServiceControl : System.Windows.Forms.UserControl
{
private DesignerOptionService designerOptionSvc;
public DesignerOptionServiceControl()
{
this.BackColor = Color.Beige;
this.Size = new Size(404, 135);
}
public override System.ComponentModel.ISite Site
{
get
{
return base.Site;
}
set
{
base.Site = value;
// If siting component, attempt to obtain an DesignerOptionService.
if( base.Site != null )
designerOptionSvc = (DesignerOptionService)this.GetService(typeof(DesignerOptionService));
}
}
// Displays control information and current DesignerOptionService
// values, if available.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString("DesignerOptionServiceControl",
new Font("Arial", 9),
new SolidBrush(Color.Blue), 4, 4);
if( this.DesignMode )
e.Graphics.DrawString("Currently in design mode",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, 18);
else
e.Graphics.DrawString("Not in design mode. Cannot access DesignerOptionService.",
new Font("Arial", 8),
new SolidBrush(Color.Red), 4, 18);
if( base.Site != null && designerOptionSvc != null )
{
e.Graphics.DrawString("DesignerOptionService provides access to the table of option values listed when",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, 38);
e.Graphics.DrawString("the Windows Forms Designer\\General tab of the Tools\\Options menu is selected.",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, 50);
e.Graphics.DrawString("Table of standard value names and current values",
new Font("Arial", 8),
new SolidBrush(Color.Red), 4, 76);
// Displays a table of the standard value names and current values.
int ypos = 90;
// Obtains and shows the size of the standard design-mode grid square.
PropertyDescriptor pd;
pd = designerOptionSvc.Options.Properties["GridSize"];
e.Graphics.DrawString("GridSize",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos += 12;
// Uncomment the following code to demonstrate that this
// alternate syntax works the same as the previous syntax.
//pd = designerOptionSvc.Options["WindowsFormsDesigner"].Properties["GridSize"];
//e.Graphics.DrawString("GridSize",
// new Font("Arial", 8),
// new SolidBrush(Color.Black), 4, ypos);
//e.Graphics.DrawString(pd.GetValue(null).ToString(),
// new Font("Arial", 8),
// new SolidBrush(Color.Black), 200, ypos);
//ypos += 12;
//pd = designerOptionSvc.Options["WindowsFormsDesigner"]["General"].Properties["GridSize"];
//e.Graphics.DrawString("GridSize",
// new Font("Arial", 8),
// new SolidBrush(Color.Black), 4, ypos);
//e.Graphics.DrawString(pd.GetValue(null).ToString(),
// new Font("Arial", 8),
// new SolidBrush(Color.Black), 200, ypos);
//ypos += 12;
// Obtains and shows whether the design mode surface grid is enabled.
pd = designerOptionSvc.Options.Properties["ShowGrid"];
e.Graphics.DrawString("ShowGrid",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos+=12;
// Obtains and shows whether components should be aligned with the surface grid.
pd = designerOptionSvc.Options.Properties["SnapToGrid"];
e.Graphics.DrawString("SnapToGrid",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos += 12;
// Obtains and shows which layout mode is selected.
pd = designerOptionSvc.Options.Properties["LayoutMode"];
e.Graphics.DrawString("LayoutMode",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos += 12;
// Obtains and shows whether the Toolbox is automatoically
// populated with custom controls and components.
pd = designerOptionSvc.Options.Properties["AutoToolboxPopulate"];
e.Graphics.DrawString("AutoToolboxPopulate",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos += 12;
// Obtains and shows whether the component cache is used.
pd = designerOptionSvc.Options.Properties["UseOptimizedCodeGeneration"];
e.Graphics.DrawString("Optimized Code Generation",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
ypos += 12;
// Obtains and shows whether designer actions are automatically opened.
pd = designerOptionSvc.Options.Properties["ObjectBoundSmartTagAutoShow"];
e.Graphics.DrawString("Automatically Open Designer Actions",
new Font("Arial", 8),
new SolidBrush(Color.Black), 4, ypos);
e.Graphics.DrawString(pd.GetValue(null).ToString(),
new Font("Arial", 8),
new SolidBrush(Color.Black), 200, ypos);
}
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
' This control demonstrates retrieving the standard
' designer option service values in design mode.
Public Class DesignerOptionServiceControl
Inherits System.Windows.Forms.UserControl
Private designerOptionSvc As DesignerOptionService
Public Sub New()
Me.BackColor = Color.Beige
Me.Size = New Size(404, 135)
End Sub
Public Overrides Property Site() As System.ComponentModel.ISite
Get
Return MyBase.Site
End Get
Set
MyBase.Site = value
' If siting component, attempt to obtain an DesignerOptionService.
If (MyBase.Site IsNot Nothing) Then
designerOptionSvc = CType(Me.GetService(GetType(DesignerOptionService)), DesignerOptionService)
End If
End Set
End Property
' Displays control information and current DesignerOptionService
' values, if available.
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString("DesignerOptionServiceControl", _
New Font("Arial", 9), _
New SolidBrush(Color.Blue), 4, 4)
If Me.DesignMode Then
e.Graphics.DrawString("Currently in design mode", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, 18)
Else
e.Graphics.DrawString("Not in design mode. Cannot access DesignerOptionService.", _
New Font("Arial", 8), _
New SolidBrush(Color.Red), 4, 18)
End If
If (MyBase.Site IsNot Nothing) AndAlso (designerOptionSvc IsNot Nothing) Then
e.Graphics.DrawString("DesignerOptionService provides access to the table of option values listed when", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, 38)
e.Graphics.DrawString("the Windows Forms Designer\General tab of the Tools\Options menu is selected.", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, 50)
e.Graphics.DrawString("Table of standard value names and current values", _
New Font("Arial", 8), _
New SolidBrush(Color.Red), 4, 76)
' Displays a table of the standard value names and current values.
Dim ypos As Integer = 90
' Obtains and shows the size of the standard design-mode grid square.
Dim pd As PropertyDescriptor
pd = designerOptionSvc.Options.Properties("GridSize")
e.Graphics.DrawString("GridSize", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Uncomment the following code to demonstrate that this
' alternate syntax works the same as the previous syntax.
'pd = designerOptionSvc.Options["WindowsFormsDesigner"].Properties["GridSize"];
'e.Graphics.DrawString("GridSize",
' new Font("Arial", 8),
' new SolidBrush(Color.Black), 4, ypos);
'e.Graphics.DrawString(pd.GetValue(null).ToString(),
' new Font("Arial", 8),
' new SolidBrush(Color.Black), 200, ypos);
'ypos += 12;
'pd = designerOptionSvc.Options["WindowsFormsDesigner"]["General"].Properties["GridSize"];
'e.Graphics.DrawString("GridSize",
' new Font("Arial", 8),
' new SolidBrush(Color.Black), 4, ypos);
'e.Graphics.DrawString(pd.GetValue(null).ToString(),
' new Font("Arial", 8),
' new SolidBrush(Color.Black), 200, ypos);
'ypos += 12;
' Obtains and shows whether the design mode surface grid is enabled.
pd = designerOptionSvc.Options.Properties("ShowGrid")
e.Graphics.DrawString("ShowGrid", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Obtains and shows whether components should be aligned with the surface grid.
pd = designerOptionSvc.Options.Properties("SnapToGrid")
e.Graphics.DrawString("SnapToGrid", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Obtains and shows which layout mode is selected.
pd = designerOptionSvc.Options.Properties("LayoutMode")
e.Graphics.DrawString("LayoutMode", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Obtains and shows whether the Toolbox is automatoically
' populated with custom controls and components.
pd = designerOptionSvc.Options.Properties("AutoToolboxPopulate")
e.Graphics.DrawString("AutoToolboxPopulate", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Obtains and shows whether the component cache is used.
pd = designerOptionSvc.Options.Properties("UseOptimizedCodeGeneration")
e.Graphics.DrawString("Optimized Code Generation", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
ypos += 12
' Obtains and shows whether designer actions are automatically opened.
pd = designerOptionSvc.Options.Properties("ObjectBoundSmartTagAutoShow")
e.Graphics.DrawString("Automatically Open Designer Actions", _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 4, ypos)
e.Graphics.DrawString(pd.GetValue(Nothing).ToString(), _
New Font("Arial", 8), _
New SolidBrush(Color.Black), 200, ypos)
End If
End Sub
End Class
注解
类 DesignerOptionService 提供选项的集合。 其中每个选项集合都有一个索引器,可用于进一步筛选它。 每个选项集合都包含其自己的一组选项,以及其所有子选项的汇总。 如果属性之间发生命名冲突,则最外部的选项对象优先。 以下 工具 |选项 用户界面 (UI) 结构显示最外层选项对象的重要性:
WindowsFormsDesigner |一般
SnapToGrid
ShowGrid
GridSize
给定一个名为 IDesignerOptionServiceservice
的 ,若要获取 属性的值 GridSize ,需要进行以下调用:
// Obtains and shows the size of the standard design-mode grid square.
System::Drawing::Size size = *dynamic_cast<System::Drawing::Size^>(designerOptionService->GetOptionValue( "WindowsFormsDesigner\\General", "GridSize" ));
// Obtains and shows the size of the standard design-mode grid square.
Size size = (Size)designerOptionService.GetOptionValue("WindowsFormsDesigner\\General", "GridSize");
' Obtains and shows the size of the standard design-mode grid square.
Dim size As Size = CType(designerOptionService.GetOptionValue("WindowsFormsDesigner\General", "GridSize"), Size)
这有效,直到你想要移动到 GridSize 另一个页面。 此外, IDesignerOptionService 不提供发现机制。 如果不知道要传入的字符串,则服务找不到属性值。
类 DesignerOptionService 可解决这些问题。 可以查询集合,并且对象上 DesignerOptionService.DesignerOptionCollection 定义了一个将集合标记为可展开的类型转换器。 使用此类型转换器,可以将整个设计器选项服务传递到属性窗口,并直观地检查服务。
构造函数
DesignerOptionService() |
初始化 DesignerOptionService 类的新实例。 |
属性
Options |
获取此服务的选项集合。 |
方法
CreateOptionCollection(DesignerOptionService+DesignerOptionCollection, String, Object) |
使用给定名称创建新的 DesignerOptionService.DesignerOptionCollection 并将其添加到给定父级。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
PopulateOptionCollection(DesignerOptionService+DesignerOptionCollection) | |
ShowDialog(DesignerOptionService+DesignerOptionCollection, Object) |
显示给定对象的选项对话框。 |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
IDesignerOptionService.GetOptionValue(String, String) |
获取在此包中定义的选项的值。 |
IDesignerOptionService.SetOptionValue(String, String, Object) |
设置在此包中定义的选项的值。 |