http://msdn2.microsoft.com/zh-cn/library/ms171830(VS.80).aspx

此示例演示如何向组件和自定义控件添加智能标记支持。

有关此代码示例的完整说明,请参见演练:向 Windows 窗体组件添加智能标记。

示例

Visual Basic
'///'Pull model smart tag example.'Need references to System.dll, System.Windows.Forms.dll, ' System.Design.dll, and System.Drawing.dll.'///Imports SystemImports System.DrawingImports System.CollectionsImports System.ComponentModelImports System.ComponentModel.DesignImports System.Windows.FormsImports System.TextImports System.Reflection

Namespace SmartTags

Public Class Form1Inherits System.Windows.Forms.Form

Private colorLabel2 As ColorLabel

Public Sub New()            InitializeComponent()End Sub

'VS Forms Designer generated methodPrivate Sub InitializeComponent()Me.colorLabel2 = New ColorLabelMe.SuspendLayout()''colorLabel2'Me.colorLabel2.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(165, Byte), Integer), CType(CType(0, Byte), Integer))Me.colorLabel2.ColorLocked = FalseMe.colorLabel2.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))Me.colorLabel2.Location = New System.Drawing.Point(41, 42)Me.colorLabel2.Name = "colorLabel2"Me.colorLabel2.Size = New System.Drawing.Size(117, 25)Me.colorLabel2.TabIndex = 0Me.colorLabel2.Text = "ColorLabel"''Form1'Me.ClientSize = New System.Drawing.Size(292, 273)Me.Controls.Add(Me.colorLabel2)Me.Name = "Form1"Me.ResumeLayout(False)

End Sub

        <STAThread()> _Shared Sub Main()Dim f1 As New Form1()            f1.ShowDialog()End SubEnd Class

'///'ColorLabel is a simple extension of the standard Label control,' with color property locking added.'///    <Designer(GetType(ColorLabelDesigner))> _Public Class ColorLabelInherits System.Windows.Forms.Label

Private colorLockedValue As Boolean = False

Public Property ColorLocked() As BooleanGetReturn colorLockedValueEnd GetSet(ByVal value As Boolean)                colorLockedValue = valueEnd SetEnd Property

Public Overrides Property BackColor() As ColorGetReturn MyBase.BackColorEnd GetSet(ByVal value As Color)If ColorLocked ThenReturnElseMyBase.BackColor = valueEnd IfEnd SetEnd Property

Public Overrides Property ForeColor() As ColorGetReturn MyBase.ForeColorEnd GetSet(ByVal value As Color)If ColorLocked ThenReturnElseMyBase.ForeColor = valueEnd IfEnd SetEnd PropertyEnd Class

'///'Designer for the ColorLabel control with support for a smart ' tag panel.'///'Must add reference to System.Design.dll    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _Public Class ColorLabelDesignerInherits System.Windows.Forms.Design.ControlDesigner

Private lists As DesignerActionListCollection

'Use pull model to populate smart tag menu.Public Overrides ReadOnly Property ActionLists() _As DesignerActionListCollectionGetIf lists Is Nothing Then                    lists = New DesignerActionListCollection()                    lists.Add( _New ColorLabelActionList(Me.Component))End IfReturn listsEnd GetEnd PropertyEnd Class

'///'DesignerActionList-derived class defines smart tag entries and' resultant actions.'///Public Class ColorLabelActionListInherits System.ComponentModel.Design.DesignerActionList

Private colLabel As ColorLabel

Private designerActionUISvc As DesignerActionUIService = Nothing

'The constructor associates the control 'with the smart tag list.Public Sub New(ByVal component As IComponent)

MyBase.New(component)Me.colLabel = component

' Cache a reference to DesignerActionUIService, so the' DesigneractionList can be refreshed.Me.designerActionUISvc = _            CType(GetService(GetType(DesignerActionUIService)), _            DesignerActionUIService)

End Sub

'Helper method to retrieve control properties. Use of ' GetProperties enables undo and menu updates to work properly.Private Function GetPropertyByName(ByVal propName As String) _As PropertyDescriptorDim prop As PropertyDescriptor            prop = TypeDescriptor.GetProperties(colLabel)(propName)If prop Is Nothing ThenThrow New ArgumentException( _"Matching ColorLabel property not found!", propName)ElseReturn propEnd IfEnd Function

'Properties that are targets of DesignerActionPropertyItem entries.Public Property BackColor() As ColorGetReturn colLabel.BackColorEnd GetSet(ByVal value As Color)                GetPropertyByName("BackColor").SetValue(colLabel, value)End SetEnd Property

Public Property ForeColor() As ColorGetReturn colLabel.ForeColorEnd GetSet(ByVal value As Color)                GetPropertyByName("ForeColor").SetValue(colLabel, value)End SetEnd Property

'Boolean properties are automatically displayed with binary ' UI (such as a checkbox).Public Property LockColors() As BooleanGetReturn colLabel.ColorLockedEnd GetSet(ByVal value As Boolean)                GetPropertyByName("ColorLocked").SetValue(colLabel, value)

' Refresh the list.Me.designerActionUISvc.Refresh(Me.Component)End SetEnd Property

Public Property [Text]() As StringGetReturn colLabel.TextEnd GetSet(ByVal value As String)                GetPropertyByName("Text").SetValue(colLabel, value)End SetEnd Property

'Method that is target of a DesignerActionMethodItemPublic Sub InvertColors()Dim currentBackColor As Color = colLabel.BackColor            BackColor = Color.FromArgb( _            255 - currentBackColor.R, _            255 - currentBackColor.G, _            255 - currentBackColor.B)

Dim currentForeColor As Color = colLabel.ForeColor            ForeColor = Color.FromArgb( _            255 - currentForeColor.R, _            255 - currentForeColor.G, _            255 - currentForeColor.B)End Sub

'Implementation of this virtual method creates smart tag  ' items, associates their targets, and collects into list.Public Overrides Function GetSortedActionItems() _As DesignerActionItemCollectionDim items As New DesignerActionItemCollection()

'Define static section header entries.            items.Add(New DesignerActionHeaderItem("Appearance"))            items.Add(New DesignerActionHeaderItem("Information"))

'Boolean property for locking color selections.            items.Add(New DesignerActionPropertyItem( _"LockColors", _"Lock Colors", _"Appearance", _"Locks the color properties."))

If Not LockColors Then                items.Add( _New DesignerActionPropertyItem( _"BackColor", _"Back Color", _"Appearance", _"Selects the background color."))

                items.Add( _New DesignerActionPropertyItem( _"ForeColor", _"Fore Color", _"Appearance", _"Selects the foreground color."))

'This next method item is also added to the context menu ' (as a designer verb).                items.Add( _New DesignerActionMethodItem( _Me, _"InvertColors", _"Invert Colors", _"Appearance", _"Inverts the fore and background colors.", _True))End If            items.Add( _New DesignerActionPropertyItem( _"Text", _"Text String", _"Appearance", _"Sets the display text."))

'Create entries for static Information section.Dim location As New StringBuilder("Location: ")            location.Append(colLabel.Location)Dim size As New StringBuilder("Size: ")            size.Append(colLabel.Size)

            items.Add( _New DesignerActionTextItem( _            location.ToString(), _"Information"))

            items.Add( _New DesignerActionTextItem( _            size.ToString(), _"Information"))

Return itemsEnd Function

End ClassEnd Namespace

C#
/// Pull model smart tag example.// Need references to System.dll, System.Windows.Forms.dll, // System.Design.dll, and System.Drawing.dll./

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.ComponentModel.Design;using System.Windows.Forms;using System.Text;using System.Reflection;

namespace SmartTags{

public class Form1 : System.Windows.Forms.Form{private ColorLabel colorLabel2;

public Form1()    {        InitializeComponent();    }

// VS Forms Designer generated methodprivate void InitializeComponent()    {this.colorLabel2 = new SmartTags.ColorLabel();this.SuspendLayout();// // colorLabel2// this.colorLabel2.BackColor = System.Drawing.Color.Gold;this.colorLabel2.ColorLocked = false;this.colorLabel2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));this.colorLabel2.Location = new System.Drawing.Point(41, 42);this.colorLabel2.Name = "colorLabel2";this.colorLabel2.Size = new System.Drawing.Size(117, 25);this.colorLabel2.TabIndex = 0;this.colorLabel2.Text = "colorLabel2";// // Form1// this.ClientSize = new System.Drawing.Size(292, 273);this.Controls.Add(this.colorLabel2);this.Name = "Form1";this.ResumeLayout(false);

    }

    [STAThread]static void Main()    {        Form1 f1 = new Form1();        f1.ShowDialog();    }

}

/// ColorLabel is a simple extension of the standard Label control,// with color property locking added./[Designer(typeof(ColorLabelDesigner))]public class ColorLabel : System.Windows.Forms.Label{private bool colorLockedValue = false;

public bool ColorLocked    {get        {return colorLockedValue;        }set        {            colorLockedValue = value;        }    }

public override Color BackColor    {get        {return base.BackColor;        }set        {if (ColorLocked)return;elsebase.BackColor = value;        }    }

public override Color ForeColor    {get        {return base.ForeColor;        }set        {if (ColorLocked)return;elsebase.ForeColor = value;        }    }}

/// Designer for the ColorLabel control with support for a smart // tag panel.// Must add reference to System.Design.dll/[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] public class ColorLabelDesigner :          System.Windows.Forms.Design.ControlDesigner{private DesignerActionListCollection actionLists;

// Use pull model to populate smart tag menu.public override DesignerActionListCollection ActionLists    {get        {if (null == actionLists)            {                actionLists = new DesignerActionListCollection();                actionLists.Add(new ColorLabelActionList(this.Component));            }return actionLists;        }    }}

/// DesignerActionList-derived class defines smart tag entries and// resultant actions./public class ColorLabelActionList :          System.ComponentModel.Design.DesignerActionList{private ColorLabel colLabel;

private DesignerActionUIService designerActionUISvc = null;

//The constructor associates the control //with the smart tag list.public ColorLabelActionList( IComponent component ) : base(component)     {this.colLabel = component as ColorLabel;

// Cache a reference to DesignerActionUIService, so the// DesigneractionList can be refreshed.this.designerActionUISvc =            GetService(typeof(DesignerActionUIService))            as DesignerActionUIService;    }

// Helper method to retrieve control properties. Use of // GetProperties enables undo and menu updates to work properly.private PropertyDescriptor GetPropertyByName(String propName)    {        PropertyDescriptor prop;        prop = TypeDescriptor.GetProperties(colLabel)[propName];if (null == prop)             throw new ArgumentException(                  "Matching ColorLabel property not found!",                   propName);elsereturn prop;    }

// Properties that are targets of DesignerActionPropertyItem entries.public Color BackColor    {get        {return colLabel.BackColor;        }set        {            GetPropertyByName("BackColor").SetValue(colLabel, value);        }    }

public Color ForeColor    {get        {return colLabel.ForeColor;        }set        {            GetPropertyByName("ForeColor").SetValue(colLabel, value);        }    }

// Boolean properties are automatically displayed with binary // UI (such as a checkbox).public bool LockColors    {get        {return colLabel.ColorLocked;        }set        {            GetPropertyByName("ColorLocked").SetValue(colLabel, value);

// Refresh the list.this.designerActionUISvc.Refresh(this.Component);        }    }

public String Text    {get        {return colLabel.Text;        }set        {            GetPropertyByName("Text").SetValue(colLabel, value);        }    }

// Method that is target of a DesignerActionMethodItempublic void InvertColors()    {        Color currentBackColor = colLabel.BackColor;        BackColor = Color.FromArgb(            255 - currentBackColor.R,             255 - currentBackColor.G,             255 - currentBackColor.B);

        Color currentForeColor = colLabel.ForeColor;        ForeColor = Color.FromArgb(            255 - currentForeColor.R,             255 - currentForeColor.G,             255 - currentForeColor.B);    }

// Implementation of this abstract method creates smart tag  // items, associates their targets, and collects into list.public override DesignerActionItemCollection GetSortedActionItems()    {        DesignerActionItemCollection items = new DesignerActionItemCollection();

//Define static section header entries.        items.Add(new DesignerActionHeaderItem("Appearance"));        items.Add(new DesignerActionHeaderItem("Information"));

//Boolean property for locking color selections.        items.Add(new DesignerActionPropertyItem("LockColors",                         "Lock Colors", "Appearance",                         "Locks the color properties."));if (!LockColors)        {            items.Add(new DesignerActionPropertyItem("BackColor",                             "Back Color", "Appearance",                             "Selects the background color."));            items.Add(new DesignerActionPropertyItem("ForeColor",                             "Fore Color", "Appearance",                             "Selects the foreground color."));

//This next method item is also added to the context menu // (as a designer verb).            items.Add(new DesignerActionMethodItem(this,                             "InvertColors", "Invert Colors",                             "Appearance",                             "Inverts the fore and background colors.",true));        }        items.Add(new DesignerActionPropertyItem("Text",                         "Text String", "Appearance",                         "Sets the display text."));

//Create entries for static Information section.        StringBuilder location = new StringBuilder("Location: ");        location.Append(colLabel.Location);        StringBuilder size = new StringBuilder("Size: ");        size.Append(colLabel.Size);        items.Add(new DesignerActionTextItem(location.ToString(),                         "Information"));        items.Add(new DesignerActionTextItem(size.ToString(),                         "Information"));

return items;    }}

}

编译代码

当您更改组件的设计时方面时,需要重新生成控件项目。此外,如果有另外一个 Windows 窗体项目当前处于打开状态并且使用此组件,您很可能将需要刷新该项目才能看到更改。一般情况下,需要关闭并重新打开包含组件的设计窗口。

请参见

参考

DesignerVerb
DesignerActionItem
DesignerActionList
ActionLists
DesignerActionService

概念

Windows 窗体的设计器命令和 DesignerAction 对象模型

其他资源

扩展设计时支持

如何:向 Windows 窗体组件附加智能标记相关推荐

  1. 【MSDN文摘】使用自定义验证组件库扩展 Windows 窗体: Form Scope

    使用自定义验证组件库扩展 Windows 窗体,第 2 部分(Windows 窗体探索) 发布日期: 5/28/2004 | 更新日期: 5/28/2004 Michael Weinhardt www ...

  2. 使用增强的Windows窗体为你的.Net程序打造丰富的用户界面

    本文内容基于微软 Visual Studio 2005 发布前的预览版,之前代号为"Whidbey".其中所有信息在正式版中都可能会有所改变. 本文所讨论内容: 关于Windows ...

  3. .NET FRAMEWORK 2.0...使用增强的Windows窗体为你的.Net程序打造丰富的用户界面

    作者:Michael Weinhardt,Chris Sells 翻译:宋文锋 原文出处:.NET Framework 2.0: Craft a Rich UI for Your .NET App w ...

  4. C# Windows 窗体编程入门详解

    C# Windows 窗体编程入门详解 基于Web的B/S架构应用程序近年来确实非常流行,B/S易于部署.易于维护的特点使Web应用程序开发得到了前所未有的发展.但是,Web应用程序的缺点是,它们有时 ...

  5. 创建可按比例调整的布局的 Windows 窗体

    能够正确调整大小的窗体可以提高您的用户界面的易用性. 此演练演示了如何创建当用户调整窗体大小时按比例调整的布局. 您将使用 TableLayoutPanel 控件实现一个接收联系人信息的数据输入窗体. ...

  6. C# Windows 窗体的.Net 框架绘图技术

    当编写一个典型的Windows 窗体程序时,窗体和控件的绘制.效果等操作是不需要特别加以考虑的.这是为什么呢?因为通过使用 .Net 框架,开发人员可以拖动一系列的控件到窗体上,并书写一些简单的与事件 ...

  7. Windows窗体学这一篇就够了(C#控件讲解)

    目录 一.Form窗体 1.1窗体的创建和删除 1.添加窗体 2.删除窗体 3.多窗体的使用 1.2.窗体属性 1.2.1更换窗体图标 1.2.2隐藏窗体的标题栏(FormBorderStyle属性) ...

  8. Windows窗体与控件

    学习下Window的窗体与控件,UI,我的IDE是VS2012,通过学习这些基本控件,如果以后要用到别的控件,就能够较快上手. Windows窗体 窗体是应用程序的基本单元,是非常重要的.它实质上是一 ...

  9. 10. Windows窗体

    Windows窗体 1 Form窗体 1.1 Form窗体的概念 1.2 添加或删除窗体 1.3 多窗体的使用 1.4 窗体的属性 1.5 窗体的显示与隐藏 1.6 窗体的事件 2 MDI窗体 2.1 ...

最新文章

  1. 超图iServer发布一个示例3D场景
  2. 区块链大热,和出版业如何发生关系?
  3. Java通过HighCharts导出图表
  4. hp-ux修改时区方法_UX研究人员可以倡导人类的6种方法
  5. linux mysql清除缓存_转载-清除Linux中MySQL的使用痕迹~/.mysql_history
  6. 哪个厂商搭载鸿蒙系统,神助攻!魅族官宣接入鸿蒙,导致概念股由绿翻红,3支直接涨停...
  7. 谁扰乱了中国的工资秩序?
  8. jmeter - 录制app接口
  9. InfoWorld 公布开源软件( 2019 年)
  10. C#编程(四十)----------运算符重载
  11. 关于拉格朗日对偶问题中对偶性的理解 (很有趣)
  12. Qualcomm MSM8937 dual DSI 笔记
  13. 尚学堂马士兵hibernate讲义
  14. 在matlab中开根号,请问,在matlab里面如果输入开方号(根号)?如9的开方怎么写?...
  15. 学单片机有什么用?单片机自学网有哪些?
  16. C#单位档案信息管理系统源码
  17. Netplus收发消息的基本流程
  18. 英语六级考前急救100词 10个List
  19. PGSQL 模糊查询不区分大小写
  20. 服务器进系统后键盘鼠标一卡一卡的,windows10系统鼠标卡顿的处理方法

热门文章

  1. C# string.Format格式化时间或货币
  2. 转:RSS阅读、社会化阅读与个性化阅读
  3. 怎么用命令开远程主机的telnet服务 2
  4. 什么是 “马太效应” ?
  5. 太赞了:《Spring Framework 4.x 参考文档》最新中文版开放下载!
  6. Hadoop框架:单服务下伪分布式集群搭建
  7. Socket编程实践(1) --TCP/IP简述
  8. Hadoop基础--HDFS/Yarn/MapReduce概述
  9. linux下查看硬盘信息、硬盘分区、格式化、挂载、及swap分区
  10. First C program