再一次体会ResizeRedraw = true;的作用

先放入正常的代码,如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace RadioButtons
 9 {
10     class RadioButtons:Form
11     {
12         bool bFillEllipse;
13         Color colorEllipse;
14         int n = 0;
15 
16         static void Main(string[] args)
17         {
18             Application.Run(new RadioButtons());
19         }
20         public RadioButtons()
21         {
22             Text = "Radio Buttons Demo";
23             ResizeRedraw = true;
24 
25             string[] astrColor = { "Black","Blue","Green","Cyan",
26                                    "Red","Magenta","Yellow","White"};
27             GroupBox grpbox = new GroupBox();
28             grpbox.Parent = this;
29             grpbox.Text = "Color";
30             grpbox.Location = new Point(Font.Height,Font.Height);
31             grpbox.Size = new Size(9 * Font.Height,
32                         (3 * astrColor.Length + 4) * Font.Height/2);
33             for (int i = 0; i < astrColor.Length; i++)
34             {
35                 RadioButton radiobtn = new RadioButton();
36                 radiobtn.Parent = grpbox;
37                 radiobtn.Text = astrColor[i];
38                 radiobtn.Location = new Point(Font.Height,
39                                     3 * (i + 1) * Font.Height/2);
40                 radiobtn.Size = new Size(7 * Font.Height,
41                                          3 * Font.Height/2);
42                 //单选按钮事件
43                 radiobtn.CheckedChanged += new EventHandler(radiobtn_CheckedChanged);
44 
45                 if (i == 0)
46                 {
47                     radiobtn.Checked = true;
48                 }
49             }
50             CheckBox chkbox = new CheckBox();
51             chkbox.Parent = this;
52             chkbox.Text = "Fill Ellipse";
53             chkbox.Location = new Point(Font.Height,
54                               3 * (astrColor.Length + 2) * Font.Height/2);
55             chkbox.Size = new Size(Font.Height * 7,3 * Font.Height/2);
56             chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
57         }
58 
59         void chkbox_CheckedChanged(object sender, EventArgs e)
60         {
61             bFillEllipse = ((CheckBox)sender).Checked;
62             Invalidate(false);
63             
64         }
65         //单选按钮事件的具体实现
66         void radiobtn_CheckedChanged(object sender, EventArgs e)
67         {
68              RadioButton radiobtn = (RadioButton)sender;
69              if (radiobtn.Checked)
70              {
71                  colorEllipse = Color.FromName(radiobtn.Text);
72                  Invalidate(false);
73                  
74              }
75         }
76         protected override void OnPaint(PaintEventArgs e)
77         {
78              Graphics grfx = CreateGraphics();
79 
80            //测试
81              grfx.DrawString("这是第一行",Font,new SolidBrush(Color.Red),0,n * Font.Height);
82              n++;
83              Console.WriteLine(n);
84 
85              Rectangle rect = new Rectangle(10 * Font.Height,0,
86                                 ClientSize.Width-10*Font.Height-1,
87                                 ClientSize.Height-1);
88              if (bFillEllipse)
89              {
90                  grfx.FillEllipse(new SolidBrush(colorEllipse), rect);
91              }
92              else
93                  grfx.DrawEllipse(new Pen(colorEllipse),rect);
94         }
95     }
96 }

 

 按住界面右下角,间断拖动,可以看到红色字体“这是第一行”和右侧的“椭圆”是随着每次的拖动在不停变化的;

下面尝试将//ResizeRedraw = true;注释掉,看看会发生什么情况 ?代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace RadioButtons
 9 {
10     class RadioButtons:Form
11     {
12         bool bFillEllipse;
13         Color colorEllipse;
14         int n = 0;
15 
16         static void Main(string[] args)
17         {
18             Application.Run(new RadioButtons());
19         }
20         public RadioButtons()
21         {
22             Text = "Radio Buttons Demo";
23             //ResizeRedraw = true;
24 
25             string[] astrColor = { "Black","Blue","Green","Cyan",
26                                    "Red","Magenta","Yellow","White"};
27             GroupBox grpbox = new GroupBox();
28             grpbox.Parent = this;
29             grpbox.Text = "Color";
30             grpbox.Location = new Point(Font.Height,Font.Height);
31             grpbox.Size = new Size(9 * Font.Height,
32                         (3 * astrColor.Length + 4) * Font.Height/2);
33             for (int i = 0; i < astrColor.Length; i++)
34             {
35                 RadioButton radiobtn = new RadioButton();
36                 radiobtn.Parent = grpbox;
37                 radiobtn.Text = astrColor[i];
38                 radiobtn.Location = new Point(Font.Height,
39                                     3 * (i + 1) * Font.Height/2);
40                 radiobtn.Size = new Size(7 * Font.Height,
41                                          3 * Font.Height/2);
42                 //单选按钮事件
43                 radiobtn.CheckedChanged += new EventHandler(radiobtn_CheckedChanged);
44 
45                 if (i == 0)
46                 {
47                     radiobtn.Checked = true;
48                 }
49             }
50             CheckBox chkbox = new CheckBox();
51             chkbox.Parent = this;
52             chkbox.Text = "Fill Ellipse";
53             chkbox.Location = new Point(Font.Height,
54                               3 * (astrColor.Length + 2) * Font.Height/2);
55             chkbox.Size = new Size(Font.Height * 7,3 * Font.Height/2);
56             chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
57         }
58 
59         void chkbox_CheckedChanged(object sender, EventArgs e)
60         {
61             bFillEllipse = ((CheckBox)sender).Checked;
62             Invalidate(false);
63             
64         }
65         //单选按钮事件的具体实现
66         void radiobtn_CheckedChanged(object sender, EventArgs e)
67         {
68              RadioButton radiobtn = (RadioButton)sender;
69              if (radiobtn.Checked)
70              {
71                  colorEllipse = Color.FromName(radiobtn.Text);
72                  Invalidate(false);
73                  
74              }
75         }
76         protected override void OnPaint(PaintEventArgs e)
77         {
78              Graphics grfx = CreateGraphics();
79 
80            //测试
81              grfx.DrawString("这是第一行",Font,new SolidBrush(Color.Red),0,n * Font.Height);
82              n++;
83              Console.WriteLine(n);
84 
85              Rectangle rect = new Rectangle(10 * Font.Height,0,
86                                 ClientSize.Width-10*Font.Height-1,
87                                 ClientSize.Height-1);
88              if (bFillEllipse)
89              {
90                  grfx.FillEllipse(new SolidBrush(colorEllipse), rect);
91              }
92              else
93                  grfx.DrawEllipse(new Pen(colorEllipse),rect);
94         }
95     }
96 }

拖动界面右下角,不断放大,可以看到,“文字”和“椭圆”没有自动刷新,产生了多次重叠。

由此可见,ResizeRedraw = true;的作用就很明显了!

 

 

 

posted @ 2022-10-16 09:58  chenlight  阅读(32)  评论(0)    收藏  举报