首页 技术 正文
技术 2022年11月8日
0 收藏 374 点赞 1,867 浏览 2269 个字

1 自定义控件与用户控件区别

WinForm中,

用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Controls控件

自定义控件(Custom Control):继承自 Control,主要用于开发windows控件的最基本的类,比如 Text,Button 控件

2 要开发自己的控件的几种方法[1]

复合控件(Composite Controls):将现有的各种控件组合起来,形成一个新的控件,来满足用户的需求。

扩展控件(Extended Controls):就是在现有的控件基础上,派生出一个新的控件,增加新的功能,或者修改原有功能,来满足用户需求。

自定义控件(Custom Controls):就是直接从System.Windows.Forms.Control类派生,也就是说完全由自己来设计、实现一个全新的控件,这是最灵活、最强大的方法,但是,对开发者的要求也是最高的。要实现一个自定义控件,必须为Control类的的OnPaint事件编写代码,在OnPaint事件中实现自定义控件的绘制工作。同时,还可以重写Control类的WndProc方法,来处理底层的Windows消息。所以说,要实现一个自定义控件,对开发者的要求较高,要求开发者必须了解GDI+和Windows API的知识。

3 示例:Clock User Control[1]

源代码

Steps:

1. 新建一个Windows控件库项目(从UserControl派生)

2. 添加一个Timer控件,并设置属性(Enable=True, Interval=1000)和事件 (Ticker=Time1_Tick)

1         private void timer1_Tick(object sender, EventArgs e)
2 {
3 this.Time = DateTime.Now;
4 Refresh();
5 }

3. 重写OnPaint事件,绘制用户界面

C# 自定义控件VS用户控件

图1 重写OnPaint事件,绘制用户界面

 1         #region draw clock
2 private void UserClock_Paint(object sender, PaintEventArgs e)
3 {
4 Graphics dc = e.Graphics;
5 Pen pn = new Pen(ForeColor);
6 SolidBrush br = new SolidBrush(ForeColor);
7 initCoordinates(dc);
8 DrawDots(dc, br);
9 DrawHourHand(dc, pn);
10 DrawSecondHand(dc, pn);
11 DrawMinuteHand(dc, pn);
12 }
13
14 public void initCoordinates(Graphics dc)
15 {
16 if (this.Width == 0 || this.Height == 0) return;
17 dc.TranslateTransform(this.Width / 2, this.Height / 2);
18 dc.ScaleTransform(this.Height / 250F, this.Width / 250F);
19 }
20 public void DrawDots(Graphics dc, Brush brush)
21 {
22 int iSize;
23 for (int i = 0; i <= 59; i++)
24 {
25 if (i % 5 == 0)
26 {
27 iSize = 15;
28 }
29 else
30 {
31 iSize = 5;
32 }
33 dc.FillEllipse(brush, -iSize / 2, -100 - iSize / 2, iSize, iSize);
34 dc.RotateTransform(6);
35 }
36 }
37 public virtual void DrawHourHand(Graphics grfx, Pen pn)
38 {
39 GraphicsState gs = grfx.Save();
40 grfx.RotateTransform(360.0F * Time.Hour / 12 + 30.0F * Time.Minute / 60);
41 grfx.DrawLine(pn, 0, 0, 0, -50);
42 grfx.Restore(gs);
43 }
44 public virtual void DrawMinuteHand(Graphics grfx, Pen pn)
45 {
46 GraphicsState gs = grfx.Save();
47 grfx.RotateTransform(360.0F * Time.Minute / 60 + 6.0F * Time.Second / 60);
48 grfx.DrawLine(pn, 0, 0, 0, -70);
49 grfx.Restore(gs);
50 }
51 public virtual void DrawSecondHand(Graphics grfx, Pen pn)
52 {
53 GraphicsState gs = grfx.Save();
54 grfx.RotateTransform(360.0F * Time.Second / 60);
55 grfx.DrawLine(pn, 0, 0, 0, -100);
56 grfx.Restore(gs);
57 }
58 #endregion

4. 生成用户控件

5. 测试用户控件

创建WinForm应用程序,在Toolbox添加Tab “User Control”,再往其中拖入第4步中生成的自定义控件的dll文件。再把Toolbox中的用户控件“UserControlClock”拖到界面“Form1”中,如下图所示。

C# 自定义控件VS用户控件

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295