一步一步的教你做C#的自定义控件

翻译|其它|编辑:郝浩|2004-01-16 10:05:00.000|阅读 3080 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


程序说明:

    原作者Alexandr Khilov,代码大小:8k
    环境:C#, .NET, controls, custom, c# controls

下面是翻译的作者的说明。

简介

这篇文章介绍了怎样创建一个.NET的自定义控件。

这里我将先制作一个自定义控件,然后在一个WIndows程序中使用它。我为我的控件实现了一些自定义的属性,你
可以学习怎样在C#中制作。

创建控件

1.打开Visual Studio创建一个新的工程。你的工程必须是基于Windows Control Librar模板的。我们把它起名为ctlCuteButton并点击OK。

2.这样你的工程就打开了,从工程中删除UserControl。

3.现在,在Project菜单中:Project->Add User Control...选择Custom Control模板。你可以把它命名为cuteButton。单击OK。一个新的Custom control就被加入到你的工程中了。

4.改变cuteButton的基类:
把   public class cuteButton : System.Windows.Forms.Control
改为 public class cuteButton : System.Windows.Forms.Button
现在你的控件就基于System.Windows.Forms.Button类了。

5.现在我们为控件创建属性。在cuteButton类中加入下面的代码:
private Color m_color1 = Color.LightGreen; //first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

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(); }
}


6.编译我们的控件之前,重载Paint事件:
// 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();


7.现在我们按<Ctrl>+<Shift>+<B>开始编译。

下面是完整的cuteButton.cs文件的内容:

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; // first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

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();
}
}
}


测试控件

1.打开一个新的VS .NET实例。创建一个新的工程,选择Windows Application模板。

2.在这个新的Windows Forms工程中,我们把我们编译好的自定义控件加入到toolbox。方法是鼠标右键单击toolbox,选择Customize Toolbox,从.NET Framework Components属性页中,单击Browse并加载Control Library DLL # (这里是ctlCuteButton\bin\debug\cuteButton.dll)。这个cuteButton组件就显示在Toolbox中了。

你可以做一些的属性的改动(cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2)。

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP