以Microsoft C# Windows程序设计为例,理解PageUnit和PageScale的用法

Microsoft windows 程序设计,第250页中,使用不同的枚举值,来求得“一英寸长的直线”,达到相同的目标。

对于以上代码,我编写了实例进行实践,代码如下:

 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 PageUnitDemo  9 { 10     class PageUnitDemo:Form 11     { 12         static void Main(string[] args) 13         { 14             Application.Run(new PageUnitDemo()); 15         } 16         public PageUnitDemo() 17         { 18             Text = Page Unit 试验!; 19         } 20         protected override void OnPaint(PaintEventArgs e) 21         { 22             Graphics grfx = e.Graphics; 23  24             grfx.PageUnit = GraphicsUnit.Inch; 25             grfx.PageScale = 0.01f; 26  27             grfx.DrawLine((new Pen(ForeColor)),0,10,100,10); 28  29             grfx.PageUnit = GraphicsUnit.Document; 30             grfx.PageScale = 3; 31  32             grfx.DrawLine(new Pen(ForeColor),0,20,100,20); 33  34             grfx.PageUnit = GraphicsUnit.Pixel; 35             grfx.PageScale = 1 ; 36  37             grfx.DrawLine(new Pen(ForeColor), 0, 30, 96, 30); 38  39             grfx.PageUnit = GraphicsUnit.Millimeter; 40             grfx.PageScale = 0.254f; 41  42             grfx.DrawLine(new Pen(ForeColor), 0, 40, 100, 40); 43  44             grfx.PageUnit = GraphicsUnit.Point; 45             grfx.PageScale = 0.72f; 46  47             grfx.DrawLine(new Pen(ForeColor), 0, 50, 100, 50); 48         } 49     } 50 }

 

 从以上结果可以看到,绘制的直线长度都是一样的;

其中,pixel是像素单位,pixel与inch之间的换算关系为:inch = pixel / 96

综合上述五种换算关系,总结为:DrawLine函数中的线长度*PageScale = 一个单位的PageUnit.枚举值;