您的位置:首页 > 编程语言 > C#

C#语言 第四部分 图形界面编程(四) 尺寸、坐标、边界

2010-04-17 23:51 316 查看
这一课很简单,学习几个类即可。

1 尺寸:Size类和SizeF类

Size类是一个表示尺寸的值类型类,具有两个正常属性:Width和Height,表示宽度和高度;一个静态属性:Empty,表示0尺寸常量(Width属性和Height属性均为0)。

SizeF类和Size类似,区别主要是Size类型对象的属性为整数值,SizeF类型对象的属性为单精度浮点类型。

看一个简单的例子:

1 using System;
2 using System.Drawing;
3
4 namespace Edu.Study.Graphics.ToolClasses {
5 static class Program {
6 static void Main(string[] args) {
7 // 定义Size类型对象, 宽度200, 高度100
8 Size size = new Size(100, 200);
9 // 重新设置宽度为150
10 size.Width = 150;
11 Console.WriteLine(size);
12
13 // 将size变量表示的对象和一个宽度300, 高度200的Size对象相加
14 size = Size.Add(size, new Size(300, 200));
15 Console.WriteLine(size);
16
17 // 将size变量表示的对象和一个宽度50, 高度20的Size对象相减
18 size = Size.Subtract(size, new Size(50, 20));
19 Console.WriteLine(size);
20
21
22 // 定义SizeF类型对象
23 SizeF sizeF = new SizeF(200.123F, 100.321F);
24 Console.WriteLine(sizeF);
25
26 // 将sizeF变量表示的对象和一个宽度50, 高度50的SizeF对象相加
27 sizeF = SizeF.Add(sizeF, new SizeF(50, 50));
28 Console.WriteLine(sizeF);
29
30 // 将sizeF变量表示的对象和一个宽度50, 高度50的SizeF对象相减
31 sizeF = SizeF.Subtract(sizeF, new SizeF(50, 50));
32 Console.WriteLine(sizeF);
33
34 // 将SizeF类型对象转换为Size类型对象
35 // 得到的Size对象高度宽度属性不小于原始SizeF对象的高度宽度属性
36 Console.WriteLine(Size.Ceiling(sizeF));
37 // 将SizeF类型对象转换为Size类型对象
38 //得到的Size对象高度宽度属性为原始SizeF对象高度宽度属性四舍六入五成双的结果
39 Console.WriteLine(Size.Round(sizeF));
40 // 将SizeF类型对象转换为Size类型对象,
41 // 得到的Size对象高度宽度属性为原始SizeF对象高度宽度属性截掉小数部分的结果
42 Console.WriteLine(Size.Truncate(sizeF));
43
44 // 将Size.Empty赋值给size变量
45 size = Size.Empty;
46 Console.WriteLine(size);
47 }
48 }
49 }
很简单的代码,对照注释就应该可以理解,无需过多讲解。

2 坐标:Point和PointF类

坐标由Point和PointF类表示(整型属性和浮点数属性),是一个表示点的坐标的值类型类,具有X, Y两个属性,表示坐标系上的一个点。

注意:.net Framework设定屏幕右上角的点为(0, 0)坐标,并且y轴向下为正,x轴向右为正

其余和前面的完全类似,参考即可,看范例:

1 using System;
2 using System.Drawing;
3
4 namespace Edu.Study.Graphics.ToolClasses {
5 static class Program {
6 static void Main(string[] args) {
7
8 // 定义Point类型对象, 宽度200, 高度100
9 Point point = new Point(100, 200);
10 // 重新设置宽度为150
11 point.X = 150;
12 Console.WriteLine(point);
13
14 // 将point变量表示的对象和一个宽度300, 高度200的Size对象相加
15 point = Point.Add(point, new Size(300, 200));
16 Console.WriteLine(point);
17
18 // 将point变量表示的对象和一个宽度50, 高度20的Size对象相减
19 point = Point.Subtract(point, new Size(50, 20));
20 Console.WriteLine(point);
21
22
23 // 定义PointF类型对象
24 PointF pointF = new PointF(200.123F, 100.321F);
25 Console.WriteLine(pointF);
26
27 // 将pointF变量表示的对象和一个宽度50, 高度50的SizeF对象相加
28 pointF = PointF.Add(pointF, new SizeF(50, 50));
29 Console.WriteLine(pointF);
30
31 // 将pointF变量表示的对象和一个宽度50, 高度50的SIzeF对象相减
32 pointF = PointF.Subtract(pointF, new SizeF(50, 50));
33 Console.WriteLine(pointF);
34
35 // 将PointF类型对象转换为Point类型对象
36 // 得到的Point对象高度宽度属性不小于原始PointF对象的高度宽度属性
37 Console.WriteLine(Point.Ceiling(pointF));
38 // 将PointF类型对象转换为Point类型对象
39 //得到的Point对象高度宽度属性为原始PointF对象高度宽度属性四舍六入五成双的结果
40 Console.WriteLine(Point.Round(pointF));
41 // 将PointF类型对象转换为Point类型对象,
42 // 得到的Point对象高度宽度属性为原始PointF对象高度宽度属性截掉小数部分的结果
43 Console.WriteLine(Point.Truncate(pointF));
44
45 // 将Point.Empty赋值给point变量
46 point = Point.Empty;
47 Console.WriteLine(point);
48 }
49 }
50 }
注意:Point.Add和Point.Subtract方法的第二个参数是一个Size类型对象,PointF类以此类推。

3 边界:Rectangle和RectangleF类

边界是一个矩形,由Rectangle类和RectangleF类定义,依旧是值类型对象,具有一个表示左边界的Left属性,一个表示上边界的Top属性,一个表示宽度Width属性和一个表示高度Height属性组成。Rectangle类的属性为整数类型,RectangleF类的属性为单精度浮点类型。

边界不仅定义了坐标,还定义了尺寸。坐标为该矩形右上角点的左边,尺寸为矩形的大小。使用起来也很简单,看代码范例即可:

1 using System;
2 using System.Drawing;
3
4 namespace Edu.Study.Graphics.ToolClasses {
5 static class Program {
6 static void Main(string[] args) {
7 // 实例化一个左上角坐标为X=10, Y=10, 宽度100, 高度200的矩形
8 Rectangle rect = new Rectangle(10, 10, 100, 200);
9 Console.WriteLine(rect);
10 // 输出矩形的右下角坐标
11 Console.WriteLine(rect.Right + ", " + rect.Bottom);
12
13 // 将矩形的左上角X坐标向正方向移动2个单位
14 rect.X += 20;
15 Console.WriteLine(rect);
16
17 // 获取矩形左上角坐标, 返回一个Point类型对象
18 Console.WriteLine(rect.Location);
19
20 // 将矩形的边长各放大4个单位和6个单位
21 // 左上角X坐标向负方向移动2, Y坐标向负方向移动3
22 // 右下角X坐标向正方向移动2, Y坐标向正方向移动3
23 // Rectangle类具备一个普通方法和一个静态方法, 功能相同
24 Console.WriteLine(Rectangle.Inflate(rect, 2, 3)); // 使用类的静态方法
25 rect.Inflate(2, 3); // 使用普通方法
26 Console.WriteLine(rect);
27
28 // 获取矩形{X=30,Y=10,Width=100,Height=200}与
29 // 矩形{X=60,Y=90,Width=300,Height=500}的交集矩形
30 // Rectangle类具备一个普通方法和一个静态方法, 功能相同
31 Console.WriteLine(
32 Rectangle.Intersect(rect, new Rectangle(60, 90, 300, 500))); // 使用类的静态方法
33 rect.Intersect(new Rectangle(60, 90, 300, 500)); // 使用普通方法
34 Console.WriteLine(rect);
35
36 // 判断两个矩形是否相交
37 Console.WriteLine(rect.IntersectsWith(new Rectangle(100, 90, 70, 200)));
38
39 // 移动矩形, X坐标向正方向移动50单位, Y坐标向正方向移动100单位
40 rect.Offset(50, 100);
41 Console.WriteLine(rect);
42
43
44 // 下面的代码演示RectangleF类, 情况和Rectangle完全类似,
45 // 请参照Rectangle的代码理解
46 RectangleF rectF = new RectangleF(10.1F, 10.2F, 100.123F, 200.456F);
47 Console.WriteLine(rectF);
48 Console.WriteLine(rectF.Right + ", " + rectF.Bottom);
49
50 rectF.X += 20;
51 Console.WriteLine(rectF);
52
53 Console.WriteLine(rectF.Location);
54
55 Console.WriteLine(RectangleF.Inflate(rectF, 2.1F, 3.2F));
56 rectF.Inflate(2.1F, 3.2F);
57 Console.WriteLine(rectF);
58
59 Console.WriteLine(RectangleF.Intersect(rectF, new RectangleF(60, 90, 300, 500)));
60 rectF.Intersect(new RectangleF(60, 90, 300, 500));
61 Console.WriteLine(rectF);
62
63 Console.WriteLine(rectF.IntersectsWith(new RectangleF(100, 90, 70, 200)));
64
65 rectF.Offset(50.99F, 100.01F);
66 Console.WriteLine(rectF);
67
68 // RectangleF类对象转化为Rectangle类对象(四舍六入五成双)
69 rect = Rectangle.Round(rectF);
70 Console.WriteLine(rect);
71
72 // RectangleF类对象转化为Rectangle类对象(取不小于整数)
73 rect = Rectangle.Ceiling(rectF);
74 Console.WriteLine(rect);
75
76 // RectangleF类对象转化为Rectangle类对象(小数部分截断)
77 rect = Rectangle.Truncate(rectF);
78 Console.WriteLine(rect);
79
80 // 取Empty静态量
81 rect = Rectangle.Empty;
82 Console.WriteLine(rect);
83
84 // 用左上角坐标和右下角坐标初始化矩形
85 rect = Rectangle.FromLTRB(10, 20, 50, 90);
86 Console.WriteLine(rect);
87 }
88 }
89 }
注意,Rectangle类和RectangleF类中具有几个方法,既有普通方法形式,也有静态方法形式。普通方法会改变当前对象的属性,静态方法则返回一个新的矩形对象,不会改变原有矩形对象的属性。

最后在重申一次,这些类都是值类型类,注意其对象在比较和赋值时候与引用类型对象的区别

本章代码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: