以《C# Windows程序设计》P427为例,再次学习Invalidate();和ResizeRedraw=true;两者的用法

在simpleclock.cs这个实例中,对于invalidate()有了进一步的学习与理解;invalidate()是用来使工作区无效,即让上一秒的工作区域失效,这样我们就可以看到秒表在一秒一秒地走动;具体代码如下:

 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 SimpleClock_chen
 9 {
10     class SimpleClock:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new SimpleClock());
15         }
16         public SimpleClock()
17         {
18             Text = "Simple Clock";
19             BackColor = SystemColors.Window;
20             ForeColor = SystemColors.WindowText;
21 
22             Timer timer = new Timer();
23             timer.Tick += new EventHandler(TimerOnTick);
24             timer.Interval = 1000;
25             timer.Start();
26         }
27         private void TimerOnTick(object obj, EventArgs e)
28         {
29             Invalidate();
30             //ResizeRedraw=true;
31         }
32         protected override void OnPaint(PaintEventArgs e)
33         {
34             StringFormat strfmt = new StringFormat();
35             strfmt.Alignment = StringAlignment.Center;
36             strfmt.LineAlignment = StringAlignment.Center;
37 
38             e.Graphics.DrawString(DateTime.Now.ToString("F"),
39                                   Font,new SolidBrush(ForeColor),
40                                   ClientRectangle,strfmt);
41         }
42     }
43 }

 

 开始运行调试后,这个画面在一秒一秒地走秒;

如果注释掉//Invalidate();会发现画面是静止的,所有数字保持不动,也不走秒。拖动右下角放大或缩小,所有数字也是保持静止不动的状态。

 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 SimpleClock_chen
 9 {
10     class SimpleClock:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new SimpleClock());
15         }
16         public SimpleClock()
17         {
18             Text = "Simple Clock";
19             BackColor = SystemColors.Window;
20             ForeColor = SystemColors.WindowText;
21 
22             Timer timer = new Timer();
23             timer.Tick += new EventHandler(TimerOnTick);
24             timer.Interval = 1000;
25             timer.Start();
26         }
27         private void TimerOnTick(object obj, EventArgs e)
28         {
29             //Invalidate();
30             ResizeRedraw=true;
31         }
32         protected override void OnPaint(PaintEventArgs e)
33         {
34             StringFormat strfmt = new StringFormat();
35             strfmt.Alignment = StringAlignment.Center;
36             strfmt.LineAlignment = StringAlignment.Center;
37 
38             e.Graphics.DrawString(DateTime.Now.ToString("F"),
39                                   Font,new SolidBrush(ForeColor),
40                                   ClientRectangle,strfmt);
41         }
42     }
43 }

这个实例代码中,注释掉Invalidate();更改为ResizeRedraw=true;然后开始运行调试程序,出现的如下画面中所有数字是静止动的;

 

 但是,如果我们拖到右下角将框体放大或缩小,会发现数字会随着拖动而变动;一旦拖动停止,则所有数字也保持停止。

 

posted @ 2022-07-02 23:25  chenlight  阅读(94)  评论(0编辑  收藏  举报