【转  http://blog.csdn.net/ronotian/article/details/3165816

 

所谓控件的选择,就是在设计器上某个控件被选中或者控件获得焦点(通过Tab调整控件焦点)的时候,在控件的四周显示出调整手柄。

如下图:

 

 

如上,通过控件的调整手柄,我们可以调整控件的宽度和高度。而实现这个调整手柄的关键点其实得益于vs2005控件的灵活性。因为这8个正方形的调整手柄其实就是8个控件。

 

所以我们本文的重点如下:

1、开发自定义的调整手柄控件,也就是这正方形控件;

2、组合这8个调整手柄控件,目标是使设计器上的控件与调整手柄完全解耦,调整手柄能够不做任何的修改就可以应用于所有的控件;

3、设计器上的控件与调整手柄的对应方法,也就是选择控件的时候,能够在控件周围显示调整手柄。

 

下面我们针对这3个重点,在前一篇《在容器上拖动鼠标增加控件》的基础上,实现控件的选择功能。

 

1、开发自定义的调整手柄控件:

这其实属于自定义控件开发的问题,你首先增加“用户控件”,把控件名命名为UISizeDot,把下面的代码直接拷贝进去就可以了,所以我不做过多的描述,这里直接把代码贴出来好了。

  1. [ToolboxItem(false)]
  2. public partial class UISizeDot : Control
  3. {
  4.     private bool _movable;
  5.     private Pen pen = new Pen(Color.Black);
  6.     public UISizeDot()
  7.     {
  8.         InitializeComponent();
  9.         SetStyle(ControlStyles.UserPaint, true);
  10.         SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  11.         SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  12.         this.TabStop = false;
  13.         this._movable = true;
  14.     }
  15.     /// <summary>
  16.     /// UISizeDot的边框颜色
  17.     /// </summary>
  18.     public Color BorderColor
  19.     {
  20.         get { return pen.Color; }
  21.         set
  22.         {
  23.             this.pen = new Pen(value);
  24.             this.Refresh();
  25.         }
  26.     }
  27.     protected override void OnPaint(PaintEventArgs pe)
  28.     {
  29.         // TODO: 在此处添加自定义绘制代码
  30.         //this.BackColor = Color.White;
  31.         pe.Graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
  32.         // 调用基类 OnPaint
  33.         base.OnPaint(pe);
  34.     }
  35.     public bool Movable
  36.     {
  37.         get { return this._movable; }
  38.         set { this._movable = value; }
  39.     }
  40. }

2、组合这8个自定义调整手柄控件:

这个功能的目的就是,就是将这8个调整手柄控件组合起来,为了描述以及开发上的方便,我们使用一个控件数组UISizeDot[]_UISizeDot = new UISizeDot(8),我们约定从左上角按照顺时针方向到又下角分别_UISizeDot[0]~ _UISizeDot[7],其它的不用说了,看代码吧:

  1. public enum ENUM_UISizeMode
  2. {
  3.     FixNone = 0,       //不固定
  4.     FixLocation = 1,   //固定左上角,这时只能改变两边
  5.     FixHeight = 2,     //固定高
  6.     FixWidth = 3,      //固定宽
  7.     FixBoth = 4        //长宽都固定
  8. }
  9. public class UISizeKnob
  10. {
  11.     private const int DOT_WIDTH = 7;   //UISizeDot宽度
  12.     private const int DOT_HEIGHT = 7;  //UISizeDot高度
  13.     private const int DOT_SPACE = 0;   //UISizeDot与_Owner的距离
  14.     private const int DOT_COUNT = 8;   //要显示的UISizeDot数
  15.     private System.Windows.Forms.Control _Owner;
  16.     private UISizeDot[] _UISizeDot;
  17.     private int _OldTop;
  18.     private int _OldLeft;
  19.     private int _NewTop;
  20.     private int _NewLeft;
  21.     private int _OldWidth;
  22.     private int _OldHeight;
  23.     private int _ClickAtX;
  24.     private int _ClickAtY;
  25.     private ENUM_UISizeMode _UISizeMode;
  26.     private bool _BeginDrag;
  27.     private Rectangle _OldRect;
  28.     private Color _DotColor = Color.White;        //UISizeDot默认颜色为白色
  29.     private Color _DotBorderColor = Color.Black;  //UISizeDot默认边框颜色为黑色
  30.     public event System.Windows.Forms.MouseEventHandler MouseDown = null;
  31.     public event System.Windows.Forms.MouseEventHandler MouseMove = null;
  32.     public event System.Windows.Forms.MouseEventHandler MouseUp = null;
  33.     private int j=0;
  34.     private bool _IsShow = false;
  35.     public UISizeKnob(System.Windows.Forms.Control owner)
  36.     {
  37.         this._Owner = owner;
  38.         this._NewTop = owner.Top;
  39.         this._NewLeft = owner.Left;
  40.         this._OldWidth = owner.Width;
  41.         this._OldHeight = owner.Height;
  42.         InitUISizeDots();
  43.     }
  44.     public bool IsShow
  45.     {
  46.         get { return this._IsShow; }
  47.     }
  48.     public Color DotColor
  49.     {
  50.         get { return this._DotColor; }
  51.         set
  52.         {
  53.             this._DotColor = value;
  54.             this._DotBorderColor = Color.FromArgb(Math.Abs(Convert.ToInt32(value.R) - 255), Math.Abs(Convert.ToInt32(value.G) - 255), Math.Abs(Convert.ToInt32(value.B) - 255));
  55.         }
  56.     }
  57.     /// <summary>
  58.     /// 注销
  59.     /// </summary>
  60.     public void Dispose()
  61.     {
  62.         for (int i = 0; i < this._UISizeDot.Length; i++)
  63.         {
  64.             this._UISizeDot[i].Dispose();
  65.         }
  66.     }
  67.     /// <summary>
  68.     /// this._Owner的大小改变模式
  69.     /// </summary>
  70.     public ENUM_UISizeMode UISizeMode
  71.     {
  72.         get { return this._UISizeMode; }
  73.         set { this._UISizeMode = value; }
  74.     }
  75.     private void InitUISizeDots()
  76.     {
  77.         this._UISizeDot = new UISizeDot[DOT_COUNT];
  78.         for (int i = 0; i < DOT_COUNT; i++)
  79.         {
  80.             this._UISizeDot[i] = new UISizeDot();
  81.             this._UISizeDot[i].Width = DOT_WIDTH;
  82.             this._UISizeDot[i].Height = DOT_HEIGHT;
  83.             this._UISizeDot[i].Visible = false;
  84.             this._Owner.Parent.Controls.Add(this._UISizeDot[i]);
  85.             this._UISizeDot[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseDown);
  86.             this._UISizeDot[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseMove);
  87.             this._UISizeDot[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseUp);
  88.         }
  89.         this._UISizeDot[0].Cursor = System.Windows.Forms.Cursors.SizeNWSE;
  90.         this._UISizeDot[1].Cursor = System.Windows.Forms.Cursors.SizeNS;
  91.         this._UISizeDot[2].Cursor = System.Windows.Forms.Cursors.SizeNESW;
  92.         this._UISizeDot[3].Cursor = System.Windows.Forms.Cursors.SizeWE;
  93.         this._UISizeDot[4].Cursor = System.Windows.Forms.Cursors.SizeNWSE;
  94.         this._UISizeDot[5].Cursor = System.Windows.Forms.Cursors.SizeNS;
  95.         this._UISizeDot[6].Cursor = System.Windows.Forms.Cursors.SizeNESW;
  96.         this._UISizeDot[7].Cursor = System.Windows.Forms.Cursors.SizeWE;
  97.         SetUISizeDotsPosition();
  98.     }
  99.     public void ShowUISizeDots(bool show)
  100.     {
  101.         this._IsShow = show;
  102.         //2006-10-05:将此函数中所有的this._UISizeDot.Length全部替换成8
  103.         if (show)
  104.         {
  105.             SetUISizeDotsPositionByMove(false);
  106.         }
  107.         else
  108.         {
  109.             this._Owner.Parent.SuspendLayout();
  110.             for (int i = 0; i < DOT_COUNT; i++)
  111.             {
  112.                 this._UISizeDot[i].Visible = show;
  113.             }
  114.             this._Owner.Parent.ResumeLayout();
  115.             return;
  116.         }
  117.         if (this._UISizeMode == ENUM_UISizeMode.FixNone)
  118.         {
  119.             for (int i = 0; i < DOT_COUNT; i++)
  120.             {
  121.                 this._UISizeDot[i].BorderColor = this._DotBorderColor;
  122.                 this._UISizeDot[i].BackColor = this._DotColor;
  123.                 this._UISizeDot[i].Visible = show;
  124.             }
  125.         }
  126.         else if (this._UISizeMode == ENUM_UISizeMode.FixLocation)
  127.         {
  128.             for (int i = 0; i < DOT_COUNT; i++)
  129.             {
  130.                 this._UISizeDot[i].BorderColor = this._DotBorderColor;
  131.                 this._UISizeDot[i].BackColor = this._DotColor;
  132.                 this._UISizeDot[i].Visible = show;
  133.             }
  134.             this._UISizeDot[0].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  135.             this._UISizeDot[0].Movable = false;
  136.             this._UISizeDot[1].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  137.             this._UISizeDot[1].Movable = false;
  138.             this._UISizeDot[2].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  139.             this._UISizeDot[2].Movable = false;
  140.             this._UISizeDot[6].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  141.             this._UISizeDot[6].Movable = false;
  142.             this._UISizeDot[7].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  143.             this._UISizeDot[7].Movable = false;
  144.         }
  145.         else if (this._UISizeMode == ENUM_UISizeMode.FixHeight)
  146.         {
  147.             this._UISizeDot[0].Visible = false;
  148.             this._UISizeDot[1].Visible = false;
  149.             this._UISizeDot[2].Visible = false;
  150.             this._UISizeDot[3].BorderColor = this._DotBorderColor;
  151.             this._UISizeDot[3].BackColor = this._DotColor;
  152.             this._UISizeDot[3].Refresh();
  153.             this._UISizeDot[3].Visible = show;
  154.             this._UISizeDot[4].Visible = false;
  155.             this._UISizeDot[5].Visible = false;
  156.             this._UISizeDot[6].Visible = false;
  157.             this._UISizeDot[7].BorderColor = this._DotBorderColor;
  158.             this._UISizeDot[7].BackColor = this._DotColor;
  159.             this._UISizeDot[7].Refresh();
  160.             this._UISizeDot[7].Visible = show;
  161.         }
  162.         else if (this._UISizeMode == ENUM_UISizeMode.FixWidth)
  163.         {
  164.             this._UISizeDot[0].Visible = false;
  165.             this._UISizeDot[1].BorderColor = this._DotBorderColor;
  166.             this._UISizeDot[1].BackColor = this._DotColor;
  167.             this._UISizeDot[1].Visible = show;
  168.             this._UISizeDot[1].Refresh();
  169.             this._UISizeDot[2].Visible = false;
  170.             this._UISizeDot[3].Visible = false;
  171.             this._UISizeDot[4].Visible = false;
  172.             this._UISizeDot[5].BorderColor = this._DotBorderColor;
  173.             this._UISizeDot[5].BackColor = this._DotColor;
  174.             this._UISizeDot[5].Visible = show;
  175.             this._UISizeDot[5].Refresh();
  176.             this._UISizeDot[6].Visible = false;
  177.             this._UISizeDot[7].Visible = false;
  178.         }
  179.         else if (this._UISizeMode == ENUM_UISizeMode.FixBoth)
  180.         {
  181.             for (int i = 0; i < DOT_COUNT; i++)
  182.             {
  183.                 this._UISizeDot[i].BorderColor = this._DotBorderColor;
  184.                 this._UISizeDot[i].BackColor = System.Drawing.Color.FromArgb(9, 55, 119);
  185.                 this._UISizeDot[i].Movable = false;
  186.                 this._UISizeDot[i].Visible = show;
  187.             }
  188.         }
  189.     }
  190.     private void SetUISizeDotsPosition()
  191.     {
  192.         int left, width, height, top;
  193.         left = this._Owner.Left;
  194.         top = this._Owner.Top;
  195.         width = this._Owner.Width;
  196.         height = this._Owner.Height;
  197.         this._UISizeDot[0].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
  198.         this._UISizeDot[1].Location = new Point(left + width / 2 - DOT_WIDTH / 2, top - DOT_HEIGHT - DOT_SPACE);
  199.         this._UISizeDot[2].Location = new Point(left + width + DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
  200.         this._UISizeDot[3].Location = new Point(left + width + DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
  201.         this._UISizeDot[4].Location = new Point(left + width + DOT_SPACE, top + height + DOT_SPACE);
  202.         this._UISizeDot[5].Location = new Point(left + width / 2 - DOT_WIDTH / 2, top + height + DOT_SPACE);
  203.         this._UISizeDot[6].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height + DOT_SPACE);
  204.         this._UISizeDot[7].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
  205.     }
  206.     private void SetUISizeDotsPositionByMove(bool Show)
  207.     {
  208.         int left, width, height, top;
  209.         left = this._Owner.Left;
  210.         top = this._Owner.Top;
  211.         width = this._Owner.Width;
  212.         height = this._Owner.Height;
  213.         this._UISizeDot[0].Visible = Show;
  214.         this._UISizeDot[1].Visible = Show;
  215.         this._UISizeDot[2].Visible = Show;
  216.         this._UISizeDot[3].Visible = Show;
  217.         this._UISizeDot[4].Visible = Show;
  218.         this._UISizeDot[5].Visible = Show;
  219.         this._UISizeDot[6].Visible = Show;
  220.         this._UISizeDot[7].Visible = Show;
  221.         this._UISizeDot[0].BringToFront();
  222.         this._UISizeDot[1].BringToFront();
  223.         this._UISizeDot[2].BringToFront();
  224.         this._UISizeDot[3].BringToFront();
  225.         this._UISizeDot[4].BringToFront();
  226.         this._UISizeDot[5].BringToFront();
  227.         this._UISizeDot[6].BringToFront();
  228.         this._UISizeDot[7].BringToFront();
  229.         this._UISizeDot[0].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
  230.         this._UISizeDot[1].Location = new Point(left + width / 2 - DOT_WIDTH / 2, top - DOT_HEIGHT - DOT_SPACE);
  231.         this._UISizeDot[2].Location = new Point(left + width + DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
  232.         this._UISizeDot[3].Location = new Point(left + width + DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
  233.         this._UISizeDot[4].Location = new Point(left + width + DOT_SPACE, top + height + DOT_SPACE);
  234.         this._UISizeDot[5].Location = new Point(left + width / 2 - DOT_WIDTH / 2, top + height + DOT_SPACE);
  235.         this._UISizeDot[6].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height + DOT_SPACE);
  236.         this._UISizeDot[7].Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
  237.     }
  238.     private void UISizeDot_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  239.     {
  240.         if (!((UISizeDot)sender).Movable)
  241.         {
  242.             return;
  243.         }
  244.         j++;
  245.         this.ShowUISizeDots(false);
  246.         this._BeginDrag = true;
  247.         this._ClickAtX = e.X;
  248.         this._ClickAtY = e.Y;
  249.         this._OldTop = this._Owner.Top;
  250.         this._OldLeft = this._Owner.Left;
  251.         this._NewTop = this._Owner.Top;
  252.         this._NewLeft = this._Owner.Left;
  253.         this._OldHeight = this._Owner.Height;
  254.         this._OldWidth = this._Owner.Width;
  255.         Rectangle rect = new Rectangle(this._NewLeft - 1, this._NewTop - 1, this._OldWidth + 2, this._OldHeight + 2);
  256.         //this._Owner.Parent.CreateGraphics().DrawRectangle(new Pen(Color.Black,2),rect);
  257.         this._OldRect = rect;
  258.         if (this.MouseDown != null)
  259.             this.MouseDown(sender, e);
  260.     }
  261.     private void UISizeDot_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  262.     {
  263.         if (!((UISizeDot)sender).Movable)
  264.         {
  265.             return;
  266.         }
  267.         if (this._BeginDrag)
  268.         {
  269.             int eX = e.X - this._ClickAtX;
  270.             int eY = e.Y - this._ClickAtY;
  271.             if (this._UISizeDot[0] == sender)
  272.             {
  273.                 this._Owner.Location = new System.Drawing.Point(this._NewLeft + eX, this._NewTop + eY);
  274.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width - eX, this._Owner.Height - eY);
  275.             }
  276.             else if (this._UISizeDot[1] == sender)
  277.             {
  278.                 this._Owner.Location = new System.Drawing.Point(this._NewLeft, this._NewTop + eY);
  279.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width, this._Owner.Height - eY);
  280.             }
  281.             else if (this._UISizeDot[2] == sender)
  282.             {
  283.                 this._Owner.Location = new System.Drawing.Point(this._NewLeft, this._NewTop + eY);
  284.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width + eX, this._Owner.Height - eY);
  285.             }
  286.             else if (this._UISizeDot[3] == sender)
  287.             {
  288.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width + eX, this._Owner.Height);
  289.             }
  290.             else if (this._UISizeDot[4] == sender)
  291.             {
  292.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width + eX, this._Owner.Height + eY);
  293.             }
  294.             else if (this._UISizeDot[5] == sender)
  295.             {
  296.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Width, this._Owner.Height + eY);
  297.             }
  298.             else if (this._UISizeDot[6] == sender)
  299.             {
  300.                 this._Owner.Location = new System.Drawing.Point(this._NewLeft + eX, this._NewTop);
  301.                 this._Owner.Size = new System.Drawing.Size(this._Owner.Size.Width - eX, this._Owner.Height + eY);
  302.             }
  303.             else if (this._UISizeDot[7] == sender)
  304.             {
  305.                 this._Owner.Location = new System.Drawing.Point(this._NewLeft + eX, this._NewTop);
  306.                 this._Owner.Size = new System.Drawing.Size(_Owner.Width - eX, this._Owner.Height);
  307.             }
  308.             this._NewTop = this._Owner.Top;
  309.             this._NewLeft = this._Owner.Left;
  310.             //this._OldHeight = this._Owner.Height;
  311.             //this._OldWidth = this._Owner.Width;
  312.             SetUISizeDotsPosition();
  313.             this._Owner.Refresh();
  314.             this._Owner.Parent.Refresh();
  315.             //this._Owner.Parent.CreateGraphics().DrawRectangle(new Pen(this._Owner.BackColor,2), this._OldRect);
  316.             Rectangle rect = new Rectangle(this._NewLeft - 1, this._NewTop - 1, this._Owner.Width + 2, this._Owner.Height + 2);
  317.             //this._Owner.Parent.CreateGraphics().DrawRectangle(new Pen(Color.Black,2), rect);
  318.             this._OldRect = rect;
  319.         }
  320.         if (this.MouseMove != null)
  321.             this.MouseMove(sender, e);
  322.     }
  323.     private void UISizeDot_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  324.     {
  325.         if (!((UISizeDot)sender).Movable)
  326.         {
  327.             return;
  328.         }
  329.         Hashtable OldWidth;
  330.         Hashtable OldHeight;
  331.         Hashtable NewWidth;
  332.         Hashtable NewHeight;
  333.         //Test.UIResizeCommand ResizeCommand;
  334.        
  335.         //this._Owner.Parent.CreateGraphics().DrawRectangle(new Pen(this._Owner.BackColor,2),this._OldRect);
  336.         this.ShowUISizeDots(true);
  337.         //使用UIResizeCommand,这里主要是保存现场,这样可以方便实现Undo和Redo
  338.         if (this._OldHeight != this._Owner.Height || this._OldWidth != this._Owner.Width)
  339.         {
  340.             OldWidth = new Hashtable();
  341.             OldHeight = new Hashtable();
  342.             NewWidth = new Hashtable();
  343.             NewHeight = new Hashtable();
  344.             OldWidth.Add(this._Owner, this._OldWidth);
  345.             OldHeight.Add(this._Owner, this._OldHeight);
  346.             NewWidth.Add(this._Owner, this._Owner.Width);
  347.             NewHeight.Add(this._Owner, this._Owner.Height);
  348.         }
  349.         this._BeginDrag = false;
  350.         if (this.MouseUp != null)
  351.             this.MouseUp(sender, e);
  352.     }
  353. }

3、设计器上控件与调整手柄的对应方法:

在我们的UISizeKnob中,有一个函数ShowUISizeDots(bool show)。这里的show参数就是表示,显示或者隐藏控件的调整手柄。

 

所以为了方便,我在前面的Form中增加这样的变量和函数:

变量private Hashtable _HashUISizeKnob负责缓存每个新增加的控件对应的UISizeKnob对象,因为一个Control只能对应一个UISizeKnob对象。

另外,我们怎么知道新增了控件呢,这里需要实现事件Control.ControlAdded,在这里是实现Form的ControlAdded事件。

 

 

Form的代码修改后如下:

 

  1. //在Form中增加几个Button,分别命名为cmdArrow,cmdLabel,cmdTextBox,cmdComboBox,cmdGroupBox
  2. public partial class Form1 : Form
  3. {
  4.     private MouseHook _MouseHook;
  5.     //我们将所有的已经与具体控件关联了的UISizeKnob缓存在这个HashTable中
  6.     private Hashtable _HashUISizeKnob;
  7.     public Form1()
  8.     {
  9.         InitializeComponent();
  10.         this._MouseHook = new MouseHook(this);
  11.         this._HashUISizeKnob = new Hashtable();
  12.         //为了简洁明了,我们在ControlAdded中来设置具体控件和UISizeKnob的关联
  13.         this.ControlAdded += new ControlEventHandler(Form1_ControlAdded);
  14.     }
  15.     void Form1_ControlAdded(object sender, ControlEventArgs e)
  16.     {
  17.         if (!(e.Control is UISizeDot))
  18.         {
  19.             this._HashUISizeKnob.Add(e.Control, new UISizeKnob(e.Control));
  20.             //点击控件的时候,显示控件的选择
  21.             e.Control.Click += new EventHandler(Control_Click);
  22.         }
  23.     }
  24.     void Control_Click(object sender, EventArgs e)
  25.     {
  26.         //寿险清除已经选择的控件
  27.         foreach (UISizeKnob knob in this._HashUISizeKnob.Values)
  28.         {
  29.             knob.ShowUISizeDots(false);
  30.         }
  31.         try
  32.         {
  33.             ((UISizeKnob)this._HashUISizeKnob[sender]).ShowUISizeDots(true);
  34.         }
  35.         catch { }
  36.     }
  37.    
  38.     private void cmdArrow_Click(object sender, EventArgs e)
  39.     {
  40.         SettingService.Instance.SelectedToolBoxControl = null;
  41.     }
  42.     private void cmdLabel_Click(object sender, EventArgs e)
  43.     {
  44.         SettingService.Instance.SelectedToolBoxControl = new Label();
  45.     }
  46.     private void cmdTextBox_Click(object sender, EventArgs e)
  47.     {
  48.         SettingService.Instance.SelectedToolBoxControl = new TextBox();
  49.     }
  50.     private void cmdComboBox_Click(object sender, EventArgs e)
  51.     {
  52.         SettingService.Instance.SelectedToolBoxControl = new ComboBox();
  53.     }
  54.     private void cmdGroupBox_Click(object sender, EventArgs e)
  55.     {
  56.         SettingService.Instance.SelectedToolBoxControl = new GroupBox();
  57.     }
  58. }

以上就是实现控件选择的关键部分,各位结合上面一篇,应该是很容易理解的,代码都是可运行的,自己拷贝过去就可以了。