C#自定义控件

C#中自定义控件VS用户控件大比拼

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

2

3

4

5

6

7

8

private void timer1_Tick(object sender, EventArgs e)

   {

 

 

      this.Time = DateTime.Now;   

 

      Refresh();     

   }

 

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

#region draw clock

    private void UserClock_Paint(object sender, PaintEventArgs e)

    {

      Graphics dc = e.Graphics;

      Pen pn = new Pen(ForeColor);

      SolidBrush br = new SolidBrush(ForeColor);

      initCoordinates(dc);

      DrawDots(dc, br);

      DrawHourHand(dc, pn);

      DrawSecondHand(dc, pn);

      DrawMinuteHand(dc, pn);

    }

     

    public void initCoordinates(Graphics dc)

    {

      if (this.Width == 0 || this.Height == 0) return;

      dc.TranslateTransform(this.Width / 2, this.Height / 2);

      dc.ScaleTransform(this.Height / 250F, this.Width / 250F);

    }

    public void DrawDots(Graphics dc, Brush brush)

    {

      int iSize;

      for (int i = 0; i <= 59; i++)

      {

        if (i % 5 == 0)

        {

          iSize = 15;

        }

        else

        {

          iSize = 5;

        }

        dc.FillEllipse(brush, -iSize / 2, -100 - iSize / 2, iSize, iSize);

        dc.RotateTransform(6);

      }

    }

    public virtual void DrawHourHand(Graphics grfx, Pen pn)

    {

      GraphicsState gs = grfx.Save();

      grfx.RotateTransform(360.0F * Time.Hour / 12 + 30.0F * Time.Minute / 60);

      grfx.DrawLine(pn, 0, 0, 0, -50);

      grfx.Restore(gs);

    }

    public virtual void DrawMinuteHand(Graphics grfx, Pen pn)

    {

      GraphicsState gs = grfx.Save();

      grfx.RotateTransform(360.0F * Time.Minute / 60 + 6.0F * Time.Second / 60);

      grfx.DrawLine(pn, 0, 0, 0, -70);

      grfx.Restore(gs);

    }

    public virtual void DrawSecondHand(Graphics grfx, Pen pn)

    {

      GraphicsState gs = grfx.Save();

      grfx.RotateTransform(360.0F * Time.Second / 60);

      grfx.DrawLine(pn, 0, 0, 0, -100);

      grfx.Restore(gs);

    }

    #endregion

4. 生成用户控件

5. 测试用户控件

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值