CSharp: Decorator Pattern

 

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
/// <summary>
  /// Summary description for Decorator.
  /// 装饰  Decorator Patterns
  /// 20220918
  /// geovindu,Geovin Du,涂聚文
  /// </summary>
  public interface Decorator
  {
 
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      void mouseMove(object sender, MouseEventArgs e);
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      void mouseEnter(object sender, EventArgs e);
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      void mouseLeave(object sender, EventArgs e);
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      void paint(object sender, PaintEventArgs e);
 
 
 
  }

  

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/// <summary>
   /// 装饰  Decorator Patterns
   /// 20220918
   /// geovindu,Geovin Du,涂聚文
   /// </summary>
   public partial class CoolDecorator : Panel, Decorator
   {
 
       protected Control contl;
       protected Pen bPen, wPen, gPen;
       private bool mouse_over;
       protected float x1, y1, x2, y2;
 
       /// <summary>
       ///
       /// </summary>
       public CoolDecorator()
       {
           InitializeComponent();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="container"></param>
       public CoolDecorator(IContainer container)
       {
           container.Add(this);
 
           InitializeComponent();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="c"></param>
       /// <param name="baseC"></param>
       public CoolDecorator(Control c, Control baseC)
       {
           //the first control is the one layed out
           //the base control is the one whose paint method we extend
           //this allows for nesting of decorators
           contl = c;
           this.Controls.AddRange(new Control[] { contl });
           c.Location = new Point(0, 0);
           this.Name = "deco" + contl.Name;
           this.Size = contl.Size;
           x1 = c.Location.X - 1;
           y1 = c.Location.Y - 1;
           x2 = c.Size.Width;
           y2 = c.Size.Height;
 
           //create the overwrite pens
           gPen = new Pen(c.BackColor, 2);
           bPen = new Pen(Color.Black, 1);
           wPen = new Pen(Color.White, 1);
 
           //mouse over, enter handler
           EventHandler evh = new EventHandler(mouseEnter);
           c.MouseHover += evh;
           c.MouseEnter += evh;
           c.MouseHover += evh;
           //mouse move handler
           c.MouseMove += new MouseEventHandler(mouseMove);
           c.MouseLeave += new EventHandler(mouseLeave);
           //paint handler catches button's paint
           baseC.Paint += new PaintEventHandler(paint);
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="p"></param>
       public void locate(Point p)
       {
           this.Location = p;
           contl.Location = new Point(0, 0);
           x1 = p.X;
           y1 = p.Y;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="c"></param>
       public virtual void locate(Control c)
       {
           this.Location = c.Location;
           c.Location = new Point(0, 0);
           x1 = c.Location.X - 1;
           y1 = c.Location.Y - 1;
           x2 = c.Size.Width;
           y2 = c.Size.Height;
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void mouseMove(object sender, MouseEventArgs e)
       {
           mouse_over = true;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void mouseEnter(object sender, EventArgs e)
       {
           mouse_over = true;
           this.Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void mouseLeave(object sender, EventArgs e)
       {
           mouse_over = false;
           this.Refresh();
       }
 
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public virtual void paint(object sender, PaintEventArgs e)
       {
           //draw over button to change its outline
           Graphics g = e.Graphics;
           const int d = 1;
           //draw over everything in gray first
           g.DrawRectangle(gPen, 0, 0, x2 - 1, y2 - 1);
           //draw black and white boundaries
           //if the mouse is over
           if (mouse_over)
           {
               g.DrawLine(bPen, 0, 0, x2 - d, 0);
               g.DrawLine(bPen, 0, 0, 0, y2 - 1);
               g.DrawLine(wPen, 0, y2 - d, x2 - d, y2 - d);
               g.DrawLine(wPen, x2 - d, 0, x2 - d, y2 - d);
           }
       }
 
   }

  

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
/// <summary>
 /// Summary description for SlashDeco.
 /// 装饰  Decorator Patterns
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class SlashDeco : CoolDecorator
 {
     private Pen rPen;
     /// <summary>
     ///
     /// </summary>
     /// <param name="c"></param>
     /// <param name="bc"></param>
     public SlashDeco(Control c, Control bc)
         : base(c, bc)
     {
         rPen = new Pen(Color.Red, 2);
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     public override void paint(object sender, PaintEventArgs e)
     {
         Graphics g = e.Graphics;
         x1 = 0; y1 = 0;
         x2 = this.Size.Width;
         y2 = this.Size.Height;
         g.DrawLine(rPen, x1, y1, x2, y2);
     }
 }

  

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/// <summary>
   /// 装饰  Decorator Patterns
   /// 20220918
   /// geovindu,Geovin Du,涂聚文
   /// </summary>
   public partial class DecoPanel : System.Windows.Forms.Panel
   {
 
       private Control c;
       private Pen bPen, wPen, gPen;
       private bool gotControl;
       private bool mouse_over;
       private float x1, y1, x2, y2;
       /// <summary>
       ///
       /// </summary>
       public DecoPanel()
       {
           InitializeComponent();
           init();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="container"></param>
       public DecoPanel(IContainer container)
       {
           container.Add(this);
 
           InitializeComponent();
       }
 
       /// <summary>
       ///
       /// </summary>
       private void init()
       {
           bPen = new Pen(System.Drawing.Color.Black, 1);
           wPen = new Pen(System.Drawing.Color.White, 1);//
           gotControl = false;      //no control
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void ctMouseEnter(object sender, EventArgs e)
       {
           mouse_over = true;
           Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void ctMouseLeave(object sender, EventArgs e)
       {
           mouse_over = false;
           this.Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void ctMouseMove(object sender, MouseEventArgs e)
       {
           mouse_over = true;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void ctPaint(object sender, PaintEventArgs e)
       {
           //draw over button to change its outline
           Graphics g = e.Graphics;
           const int d = 1;
           //draw over everything in gray first
           g.DrawRectangle(gPen, 0, 0, x2 - 1, y2 - 1);
           //draw black and white boundaries
           //if the mouse is over
           if (mouse_over)
           {
               g.DrawLine(bPen, 0, 0, x2 - d, 0);
               g.DrawLine(bPen, 0, 0, 0, y2 - 1);
               g.DrawLine(wPen, 0, y2 - d, x2 - d, y2 - d);
               g.DrawLine(wPen, x2 - d, 0, x2 - d, y2 - d);
           }
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="e"></param>
       protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
       {
           //This is where we find out about the control
           if (!gotControl)
           {       //once only
               //get the control
               c = this.Controls[0];
               //set the panel size
               //1 pixel bigger all around
               Size sz = new Size();
               sz.Width = c.Size.Width + 2;
               sz.Height = c.Size.Height + 2;
               this.Size = sz;
 
               x1 = c.Location.X - 1;
               y1 = c.Location.Y - 1;
               x2 = c.Size.Width;
               y2 = c.Size.Height;
               //create the overwrite pen
               gPen = new Pen(c.BackColor, 2);
               gotControl = true;        //only once
               //mouse over, enter handler
               EventHandler evh = new EventHandler(ctMouseEnter);
               c.MouseHover += evh;
               c.MouseEnter += evh;
               //mouse move handler
               c.MouseMove += new MouseEventHandler(ctMouseMove);
               c.MouseLeave += new EventHandler(ctMouseLeave);
               //paint handler catches button's paint
               c.Paint += new PaintEventHandler(ctPaint);
           }
       }
 
 
   
 
 
   }

  

窗体调用:

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
59
60
61
62
63
64
65
66
67
68
69
70
/// <summary>
    /// 装饰  Decorator Patterns
   /// 20220918
   /// geovindu,Geovin Du,涂聚文   
   /// </summary>
   public partial class DecoratorPatternsForm : Form
   {
 
       private SlashDeco slash;
       /// <summary>
       ///
       /// </summary>
       private CoolDecorator cdec;
 
       /// <summary>
       ///
       /// </summary>
       private void init()
       {
           //save the position of the button
           Point pt = btButtonA.Location;
           //create a cool decorator
           cdec = new CoolDecorator(btButtonA, btButtonA);
           //decorate the cool decorator with a slash
           slash = new SlashDeco(cdec, btButtonA);
           slash.locate(pt);
           //add outside decorator to the layout
           //and remove the button from the layout
           this.Controls.AddRange(new System.Windows.Forms.Control[] { slash });
           this.Controls.Remove(btButtonA);
 
           //this.Controls.AddRange(new System.Windows.Forms.Control[] {cdec});
 
       }
       /// <summary>
       /// 装饰Decorator Patterns
       /// </summary>
       public DecoratorPatternsForm()
       {
           InitializeComponent();
           init();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void DecoratorPatternsForm_Load(object sender, EventArgs e)
       {
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void btQuit_Click(object sender, EventArgs e)
       {
           this.Close();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void btButtonA_Click(object sender, EventArgs e)
       {
 
       }
   }

  

输出:

点击前:、

 

点击后:

 

posted @   ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示