用GDI+绘制了一个钟表盘的类似物,显示当前的时间
2006-12-15 16:49
531 查看
要绘制了一个钟表盘的类似物(如下图所示),显示当前的时间。绘制工作是由FillRectangle(绘制一个填充的矩形)和FillPolygon(绘制一个填充的多边形)完成的,但转换却实现了真实时间的显示。
通过使x和y坐标的量值等于窗体宽度和高度的一半,TranslateTransform将原点移动到了窗体的中心。RotateTransform旋转了准备绘制时针和分针坐标系统和表盘上指示时间位置的小方块。ScaleTransform缩放了输出,因此不管窗体的大小怎样,表盘都能将其充满。传递给FillRectangle和FillPolygon的坐标假定窗体的客户区正好是200单位宽和200单位高。ScaleTransform应用了x和y的缩放比例值,它用一个与物理客户区大小和逻辑客户区大小的比率相等的量值缩放输出。
下面所示的Clock.cs是名为Clock的程序源代码,绘制了一个钟表盘的类似物(如上图所示),显示当前的时间(运行生成EXE文件,具体时间为你的系统时间):
using System;
using System.Windows.Forms;
using System.Drawing;
class MyForm: Form
{
MyForm()
{
Text = "Clock maded by GDI+";
SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush Green = new SolidBrush(Color.Green);
SolidBrush blue = new SolidBrush(Color.Blue);
SolidBrush yellow = new SolidBrush(Color.Yellow );
//初始化数据
InitializeTransform(e.Graphics);
//绘制表盘上表示小格的小方块
for(int i = 0; i < 60; i++)
{
e.Graphics.RotateTransform(6.0f);
e.Graphics.FillRectangle(black, 85, 0, 8, 1);
}
//绘制表盘上表示小时的小方块
for(int i = 0; i < 12; i++)
{
e.Graphics.RotateTransform(30.0f);
e.Graphics.FillRectangle(Green, 85, -5, 10, 10);
}
//获取当前的时间
DateTime now = DateTime.Now;
int second = now.Second;
int minute = now.Minute;
int hour = now.Hour % 12;
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制时针
e.Graphics.RotateTransform((hour * 30) + (minute / 2) + (second / 600));
DrawHand(e.Graphics, blue, 45, 12);
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制分针
e.Graphics.RotateTransform((minute * 6) + (second / 10));
DrawHand(e.Graphics, red, 60, 9);
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制秒针
e.Graphics.RotateTransform(second * 6);
DrawHand(e.Graphics, yellow, 90, 6);
//释放画笔
red.Dispose();
Green.Dispose();
blue.Dispose();
yellow.Dispose();
black.Dispose();
}
void DrawHand(Graphics g, Brush brush, int length, int width)
{
//绘制一个直指向上的指针
//RotateTransform将其指向适当的角度
Point[] points = new Point[4];
points[0].X = 0;
points[0].Y = -length;
points[1].X = -width;
points[1].Y = 0;
points[2].X = 0;
points[2].Y = width;
points[3].X = width;
points[3].Y = 0;
g.FillPolygon(brush, points);
}
void InitializeTransform(Graphics g)
{
//应用转换,将圆心移动到客户区的中心,并使输出
//适合窗体的宽度和高度
g.ResetTransform();
g.TranslateTransform(ClientSize.Width/2, ClientSize.Height/2);
float scale = System.Math.Min(ClientSize.Width, ClientSize.Height/200.0f);
g.ScaleTransform(scale, scale);
}
static void Main()
{
Application.Run(new MyForm());
}
}
编译上面的CS文件:
然后运行:
结果:
通过使x和y坐标的量值等于窗体宽度和高度的一半,TranslateTransform将原点移动到了窗体的中心。RotateTransform旋转了准备绘制时针和分针坐标系统和表盘上指示时间位置的小方块。ScaleTransform缩放了输出,因此不管窗体的大小怎样,表盘都能将其充满。传递给FillRectangle和FillPolygon的坐标假定窗体的客户区正好是200单位宽和200单位高。ScaleTransform应用了x和y的缩放比例值,它用一个与物理客户区大小和逻辑客户区大小的比率相等的量值缩放输出。
下面所示的Clock.cs是名为Clock的程序源代码,绘制了一个钟表盘的类似物(如上图所示),显示当前的时间(运行生成EXE文件,具体时间为你的系统时间):
using System;
using System.Windows.Forms;
using System.Drawing;
class MyForm: Form
{
MyForm()
{
Text = "Clock maded by GDI+";
SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush Green = new SolidBrush(Color.Green);
SolidBrush blue = new SolidBrush(Color.Blue);
SolidBrush yellow = new SolidBrush(Color.Yellow );
//初始化数据
InitializeTransform(e.Graphics);
//绘制表盘上表示小格的小方块
for(int i = 0; i < 60; i++)
{
e.Graphics.RotateTransform(6.0f);
e.Graphics.FillRectangle(black, 85, 0, 8, 1);
}
//绘制表盘上表示小时的小方块
for(int i = 0; i < 12; i++)
{
e.Graphics.RotateTransform(30.0f);
e.Graphics.FillRectangle(Green, 85, -5, 10, 10);
}
//获取当前的时间
DateTime now = DateTime.Now;
int second = now.Second;
int minute = now.Minute;
int hour = now.Hour % 12;
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制时针
e.Graphics.RotateTransform((hour * 30) + (minute / 2) + (second / 600));
DrawHand(e.Graphics, blue, 45, 12);
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制分针
e.Graphics.RotateTransform((minute * 6) + (second / 10));
DrawHand(e.Graphics, red, 60, 9);
//重新初始化转换矩阵
InitializeTransform(e.Graphics);
//绘制秒针
e.Graphics.RotateTransform(second * 6);
DrawHand(e.Graphics, yellow, 90, 6);
//释放画笔
red.Dispose();
Green.Dispose();
blue.Dispose();
yellow.Dispose();
black.Dispose();
}
void DrawHand(Graphics g, Brush brush, int length, int width)
{
//绘制一个直指向上的指针
//RotateTransform将其指向适当的角度
Point[] points = new Point[4];
points[0].X = 0;
points[0].Y = -length;
points[1].X = -width;
points[1].Y = 0;
points[2].X = 0;
points[2].Y = width;
points[3].X = width;
points[3].Y = 0;
g.FillPolygon(brush, points);
}
void InitializeTransform(Graphics g)
{
//应用转换,将圆心移动到客户区的中心,并使输出
//适合窗体的宽度和高度
g.ResetTransform();
g.TranslateTransform(ClientSize.Width/2, ClientSize.Height/2);
float scale = System.Math.Min(ClientSize.Width, ClientSize.Height/200.0f);
g.ScaleTransform(scale, scale);
}
static void Main()
{
Application.Run(new MyForm());
}
}
编译上面的CS文件:
然后运行:
结果:
相关文章推荐
- 绘制一个显示当前时间的StiilClock
- android cavas 绘制坐标系并显示矩形波方波-横轴为当前时间动态更新(不用achartengine)
- Python绘制数码管显示当前时间
- python学习之获取当前时间的方法(可以任意取时间的任何一个对象显示)
- 利用BIOS中断或DOS中断实现一个显示当前时间的程序
- 实现一个用于显示当前时间的Google Android 窗口小部件(AppWidget) 推荐
- 现一个用于显示当前时间的Google Android 窗口小部件(AppWidget)
- C#--第11周实验--任务1(建立Windows窗体应用程序)--设计一个窗体--单击按钮时,在标签上显示当前系统时间 .
- 开发第一个安卓程序(按钮显示一个当前的系统时间)
- VC6对话框中,如何实现点击一个按钮,在相应的Edit框里,显示当前时间
- 一个日历形式显示的当前时间
- 【C语言】编写一个程序显示当前时间。
- C#在一个控件上显示电脑当前的时间
- 如何用JavaScript在页面上显示一个时间钟表
- C#--第11周实验--任务1(建立Windows窗体应用程序)--设计一个窗体--单击按钮时,在标签上显示当前系统时间
- JS定时程序,设定一个一直刷新,又时间间隔的js,读取当前的时间并显示
- 一个显示计算机时间的钟表的程序代码
- 实现一个用于显示当前时间的Google Android 窗口小部件(AppWidget)
- 在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
- 从数据库取出一个时间字段,当取出的时间超过当前时间就显示"1" 不超过当前时间就显示"0"