用GDI+绘制了一个钟表盘的类似物,显示当前的时间

    要绘制了一个钟表盘的类似物(如下图所示),显示当前的时间。绘制工作是由FillRectangle(绘制一个填充的矩形)和FillPolygon(绘制一个填充的多边形)完成的,但转换却实现了真实时间的显示。
  用GDI+绘制了一个钟表盘的类似物,显示当前的时间

    通过使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, 
85081);
        }

        
//绘制表盘上表示小时的小方块
        for(int i = 0; i < 12; i++)
        {
            e.Graphics.RotateTransform(
30.0f);
            e.Graphics.FillRectangle(Green, 
85-51010);
        }

        
//获取当前的时间
        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, 
4512);

        
//重新初始化转换矩阵
        InitializeTransform(e.Graphics);

        
//绘制分针
        e.Graphics.RotateTransform((minute * 6+ (second / 10));
        DrawHand(e.Graphics, red, 
609);

        
//重新初始化转换矩阵
        InitializeTransform(e.Graphics);

        
//绘制秒针
        e.Graphics.RotateTransform(second * 6);
        DrawHand(e.Graphics, yellow, 
906);
        
        
//释放画笔
        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文件:
用GDI+绘制了一个钟表盘的类似物,显示当前的时间

然后运行:
用GDI+绘制了一个钟表盘的类似物,显示当前的时间

结果:
用GDI+绘制了一个钟表盘的类似物,显示当前的时间