这篇文章简明扼要的介绍了在 .Net Framework中如何来产生自己的定制控件.我们讲述如果产生一个定制控件,然后在一个应用程序中使用它. 我在这个控件中加入了自己的一些属性,我们可以看一下在C#中是如果实现的. 生成控件在Visual Studio .net 中, 用Windows控件库来作为模板,生成一个新的项目.项目的名称为 ctlCuteButton.
首先么做的就是要修改cuteButton的父类
修改下面这行:public class cuteButton : System.Windows.Forms.Control改为:public class cuteButton : System.Windows.Forms.Button现在我们的控件就是从System.Windows.Forms.Button 类来继承了.现在让我们给这个控件加一些属性,在cuteButton类中加入下面的代码就可以完成.
private Color m_color1 = Color.LightGreen; //第一种颜色private Color m_color2 = Color.DarkBlue; // 第二种颜色private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 { get { return m_color1; } set { m_color1 = value; Invalidate(); }} public Color cuteColor2{ get { return m_color2; } set { m_color2 = value; Invalidate(); }} public int cuteTransparent1{ get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); }} public int cuteTransparent2{ get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); }}上面的 Invalidate() 方法是用来在设计和使用时用来刷新控件的内部.重写父类的Paint 事件
// Calling the base class OnPaintbase.OnPaint(pe);// Create two semi-transparent colorsColor c1 = Color.FromArgb(m_color1Transparent , m_color1);Color c2 = Color.FromArgb(m_color2Transparent , m_color2);Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);pe.Graphics.FillRectangle (b, ClientRectangle);b.Dispose();现在我们可以编译这控件了,可以使用快捷键<Ctrl>+<Shift>+<B>.
下面是全部的代码:using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms; namespace ctlCuteButton{ /// <summary> /// Summary description for cuteButton. /// </summary> public class cuteButton : System.Windows.Forms.Button { private Color m_color1 = Color.LightGreen; //第一种颜色 private Color m_color2 = Color.DarkBlue; // 第二种颜色 private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 public Color cuteColor1 { get { return m_color1; } set { m_color1 = value; Invalidate(); } } public Color cuteColor2 { get { return m_color2; } set { m_color2 = value; Invalidate(); } } public int cuteTransparent1 { get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); } } public int cuteTransparent2 { get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); } } public cuteButton() { } protected override void OnPaint(PaintEventArgs pe) { // Calling the base class OnPaint base.OnPaint(pe); // Create two semi-transparent colors Color c1 = Color.FromArgb (m_color1Transparent , m_color1); Color c2 = Color.FromArgb (m_color2Transparent , m_color2); Brush b = new System.Drawing.Drawing2D.LinearGradientBrush (ClientRectangle, c1, c2, 10); pe.Graphics.FillRectangle (b, ClientRectangle); b.Dispose(); }}}如何来使用你的控件打开VS.Net,用Winows应用程序模板来产生一个新的项目.
在新的项目中,你可以把上面编译过的控件添加到工具箱中. 在工具箱中,右键,定制工具箱,然后添加.Net控件,然后浏览,选中控件的dll文件(在我们的例子中,这个文件是ctlCuteButton\bin\debug\cuteButton.dll).这是cuteButton控件就会出现在工具箱中.
你可以尝试改变控件的一些属性,比如 cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2.现在,你就完全知道如果开发和使用自定义控件了吧.祝你好运.
首先么做的就是要修改cuteButton的父类
修改下面这行:public class cuteButton : System.Windows.Forms.Control改为:public class cuteButton : System.Windows.Forms.Button现在我们的控件就是从System.Windows.Forms.Button 类来继承了.现在让我们给这个控件加一些属性,在cuteButton类中加入下面的代码就可以完成.
private Color m_color1 = Color.LightGreen; //第一种颜色private Color m_color2 = Color.DarkBlue; // 第二种颜色private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 { get { return m_color1; } set { m_color1 = value; Invalidate(); }} public Color cuteColor2{ get { return m_color2; } set { m_color2 = value; Invalidate(); }} public int cuteTransparent1{ get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); }} public int cuteTransparent2{ get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); }}上面的 Invalidate() 方法是用来在设计和使用时用来刷新控件的内部.重写父类的Paint 事件
// Calling the base class OnPaintbase.OnPaint(pe);// Create two semi-transparent colorsColor c1 = Color.FromArgb(m_color1Transparent , m_color1);Color c2 = Color.FromArgb(m_color2Transparent , m_color2);Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);pe.Graphics.FillRectangle (b, ClientRectangle);b.Dispose();现在我们可以编译这控件了,可以使用快捷键<Ctrl>+<Shift>+<B>.
下面是全部的代码:using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms; namespace ctlCuteButton{ /// <summary> /// Summary description for cuteButton. /// </summary> public class cuteButton : System.Windows.Forms.Button { private Color m_color1 = Color.LightGreen; //第一种颜色 private Color m_color2 = Color.DarkBlue; // 第二种颜色 private int m_color1Transparent = 64; // 第一种颜色的透明度 private int m_color2Transparent = 64; // 第二种颜色的透明度 public Color cuteColor1 { get { return m_color1; } set { m_color1 = value; Invalidate(); } } public Color cuteColor2 { get { return m_color2; } set { m_color2 = value; Invalidate(); } } public int cuteTransparent1 { get { return m_color1Transparent; } set { m_color1Transparent = value; Invalidate(); } } public int cuteTransparent2 { get { return m_color2Transparent; } set { m_color2Transparent = value; Invalidate(); } } public cuteButton() { } protected override void OnPaint(PaintEventArgs pe) { // Calling the base class OnPaint base.OnPaint(pe); // Create two semi-transparent colors Color c1 = Color.FromArgb (m_color1Transparent , m_color1); Color c2 = Color.FromArgb (m_color2Transparent , m_color2); Brush b = new System.Drawing.Drawing2D.LinearGradientBrush (ClientRectangle, c1, c2, 10); pe.Graphics.FillRectangle (b, ClientRectangle); b.Dispose(); }}}如何来使用你的控件打开VS.Net,用Winows应用程序模板来产生一个新的项目.
在新的项目中,你可以把上面编译过的控件添加到工具箱中. 在工具箱中,右键,定制工具箱,然后添加.Net控件,然后浏览,选中控件的dll文件(在我们的例子中,这个文件是ctlCuteButton\bin\debug\cuteButton.dll).这是cuteButton控件就会出现在工具箱中.
你可以尝试改变控件的一些属性,比如 cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2.现在,你就完全知道如果开发和使用自定义控件了吧.祝你好运.
搂主的意思是否要生成Win32的dll? 那是不行的,只能生成.net类库dll