ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2022-07-02 23:33:41  阅读:156  来源: 互联网

标签:strfmt Invalidate 为例 C# SimpleClock System timer using new


在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;然后开始运行调试程序,出现的如下画面中所有数字是静止动的;

 

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

 

标签:strfmt,Invalidate,为例,C#,SimpleClock,System,timer,using,new
来源: https://www.cnblogs.com/chenlight/p/16438920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有