一个C#写的CLOCK程序

来源:互联网 发布:乙丁网络 编辑:程序博客网 时间:2024/06/07 02:20

窗体设计代码:[Form1.Designer.cs]

 

namespace Game_One
{
    
partial class Form1
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


        
Windows 窗体设计器生成的代码

        
private System.Windows.Forms.Panel ClockPanel;
        
private System.Windows.Forms.PictureBox PicBox;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.TextBox TimeBox;
        
private System.Windows.Forms.Button CheckBtn;
        
private System.Windows.Forms.Button AnswerBtn;
        
private System.Windows.Forms.Label NameLabel;
    }

}

程序资源文件就不贴了,就是一张图片

程序实现代码:[Form1.cs]

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Game_One
{
    
public partial class Form1 : Form
    
{
        
private DateTime DT = new DateTime();
        
private Image ImgBak = Image.FromFile(ConfigurationManager.AppSettings["BackGroundImage"].ToString());
        System.IO.MemoryStream MEM 
= new System.IO.MemoryStream();
        
bool Saved = false;

        
public Form1()
        
{
            InitializeComponent();
            
this.SetStyle(ControlStyles.UserPaint, true);
            
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            
if (!Saved)
            
{
                ImgBak.Save(MEM, System.Drawing.Imaging.ImageFormat.Jpeg);
                Saved 
= true;
            }

            TimeBox.Focus();
            PicBox.Visible 
= false;            
        }

        
        
private void AnswerBtn_Click(object sender, EventArgs e)
        
{
            NameLabel.Text 
= ConfigurationManager.AppSettings[PicIndex.ToString()].ToString();
            NameLabel.Visible 
= true;
        }


        
private void DrawClock(Graphics g, Rectangle Rect)
        
{            
            
//g.Clear(Color.Empty);
            Point L = ClockPanel.Location;
            Point A_1 
= new Point(0-L.X, 0-L.Y);
            Point A_2 
= new Point(A_1.X + ClockPanel.Width, A_1.Y);
            Point A_3 
= new Point(A_1.X, A_1.Y + ClockPanel.Height);
            Point[] A 
= new Point[] { A_1, A_2, A_3 };
            Point B_1 
= new Point(00);
            Point B_2 
= new Point(ClockPanel.Width, 0);
            Point B_3 
= new Point(0, ClockPanel.Height);
            Point[] B 
= new Point[] {B_1,B_2,B_3 };
            Rectangle NewRect 
= new Rectangle(L.X, L.Y, ClockPanel.Width, ClockPanel.Height);
            g.DrawImage(ImgBak, B, NewRect, GraphicsUnit.Pixel);
            
//绘制圆周
            Pen RedPen = new Pen(Color.Red, 3);
            g.DrawEllipse(RedPen, 
77480480);

            Point Center 
= new Point(247247);

            
//绘制钟点
            Pen PPen_Ora = new Pen(Color.Blue, 4);
            Pen PPen_Special 
= new Pen(Color.Red, 5);
            
int SidePoint_X;
            
int SidePoint_Y;

            
int MovePoint_X;
            
int MovePoint_Y;

            
for (int i = 1; i <= 60; i++)
            
{
                SidePoint_X 
= 247 + (int)(Math.Sin(Math.PI / 180 * i * 6* 240);
                SidePoint_Y 
= 247 + (int)(Math.Cos(Math.PI / 180 * i * 6* 240);
                
if (i % 5 == 0)
                
{
                    MovePoint_X 
= 247 + (int)(Math.Sin(Math.PI / 180 * i * 6* 220);
                    MovePoint_Y 
= 247 + (int)(Math.Cos(Math.PI / 180 * i * 6* 220);
                    Point MovePoint 
= new Point(MovePoint_X, MovePoint_Y);
                    Point SidePoint 
= new Point(SidePoint_X, SidePoint_Y);
                    g.DrawLine(PPen_Special, MovePoint, SidePoint);
                }

                
else
                
{
                    MovePoint_X 
= 247 + (int)(Math.Sin(Math.PI / 180 * i * 6* 235);
                    MovePoint_Y 
= 247 + (int)(Math.Cos(Math.PI / 180 * i * 6* 235);
                    Point MovePoint 
= new Point(MovePoint_X, MovePoint_Y);
                    Point SidePoint 
= new Point(SidePoint_X, SidePoint_Y);
                    g.DrawLine(PPen_Ora, MovePoint, SidePoint);
                }

            }

            
int HourPoint = DT.Hour;
            
int MinutePoint = DT.Minute;

            
//三角形顶点
            int HourEnd_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 180);
            
int HourEnd_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 180);
            Point hourEnd 
= new Point(HourEnd_X, HourEnd_Y);

            
//三角形右节点
            int HourRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (90 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
            
int HourRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (90 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
            Point RightSide 
= new Point(HourRight_X, HourRight_Y);
            
//三角形左节点
            int HourLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (270 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
            
int HourLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (270 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
            Point LeftSide 
= new Point(HourLeft_X, HourLeft_Y);
            
//用画刷填充区域
            SolidBrush HourBrush = new SolidBrush(Color.Gray);
            Point[] HourA 
= new Point[] { RightSide, LeftSide, hourEnd };
            g.FillPolygon(HourBrush, HourA);
            
//箭头中节点
            int HHeadMiddle_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 135);
            
int HHeadMiddle_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 135);
            Point HHeadMiddle 
= new Point(HHeadMiddle_X, HHeadMiddle_Y);
            
//箭头左节点
            int HHeadLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 - 2)) * 150);
            
int HHeadLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 - 2)) * 150);
            Point HHeadLeft 
= new Point(HHeadLeft_X, HHeadLeft_Y);
            
//箭头右节点
            int HHeadRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 + 2)) * 150);
            
int HHeadRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 + 2)) * 150);
            Point HHeadRight 
= new Point(HHeadRight_X, HHeadRight_Y);
            Point[] HourHead 
= new Point[] { HHeadLeft, HHeadMiddle, HHeadRight, hourEnd, };
            g.FillPolygon(HourBrush, HourHead);
            
//绘制分针
            int MinuteEnd_X = 247 + (int)(Math.Sin(Math.PI / 180 * MinutePoint * 6* 230);
            
int MinuteEnd_Y = 247 - (int)(Math.Cos(Math.PI / 180 * MinutePoint * 6* 230);
            Point MinuteEnd 
= new Point(MinuteEnd_X, MinuteEnd_Y);
            
//三角形右节点
            int MinuteRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (90 + MinutePoint * 6)) * 9);
            
int MinuteRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (90 + MinutePoint * 6)) * 9);
            Point MinuteRightSide 
= new Point(MinuteRight_X, MinuteRight_Y);
            
//三角形左节点
            int MinuteLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (270 + MinutePoint * 6)) * 9);
            
int MinuteLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (270 + MinutePoint * 6)) * 9);
            Point MinuteLeftSide 
= new Point(MinuteLeft_X, MinuteLeft_Y);
            
//用画刷填充区域
            SolidBrush MinuteBrush = new SolidBrush(Color.Green);
            Point[] MinuteA 
= new Point[] { MinuteRightSide, MinuteLeftSide, MinuteEnd };
            g.FillPolygon(MinuteBrush, MinuteA);
            
//箭头左节点
            int MHeadLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6 - 2)) * 190);
            
int MHeadLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6 - 2)) * 190);
            Point MHeadLeft 
= new Point(MHeadLeft_X, MHeadLeft_Y);
            
//箭头右节点
            int MHeadRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6 + 2)) * 190);
            
int MHeadRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6 + 2)) * 190);
            Point MHeadRight 
= new Point(MHeadRight_X, MHeadRight_Y);
            
//箭头中节点
            int MHeadMiddle_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6)) * 180);
            
int MHeadMiddle_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6)) * 180);
            Point MHeadMiddle 
= new Point(MHeadMiddle_X, MHeadMiddle_Y);
            Point[] MinuteHead 
= new Point[] { MHeadLeft, MHeadMiddle, MHeadRight, MinuteEnd };
            g.FillPolygon(MinuteBrush, MinuteHead);

            
//绘制轴心
            Pen CPen = new Pen(Color.Red, 14);
            g.DrawEllipse(CPen, 
2402401414);
        }


        
private void Form1_Paint(object sender, PaintEventArgs e)
        
{
            paintClock(
this.ClientRectangle);
        }


        
private static int PicIndex = 0;

        
private void CheckBtn_Click(object sender, EventArgs e)
        
{
            
try
            
{
                DT 
= Convert.ToDateTime(TimeBox.Text.Trim() + ":00");
                
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.ClientRectangle));

                
if (DT.Hour >= 12)
                
{
                    PicIndex 
= (DT.Hour - 12* 3 + DT.Minute / 20 + 1;
                }

                
else
                
{
                    PicIndex 
= DT.Hour * 3 + DT.Minute / 20 + 1;
                }

                PicBox.ImageLocation 
= ConfigurationManager.AppSettings["ManPic"].ToString() + PicIndex.ToString() + ".jpg";
                PicBox.Visible 
= true;
                PicBox.Show();
            }

            
catch
            
{
            }

        }


        
internal void paintClock(Rectangle rect)
        
{
            
if (!this.IsDisposed && this.Visible)
            
{
                BufferedGraphicsContext myContext 
= BufferedGraphicsManager.Current;
                
                BufferedGraphics buffer 
= myContext.Allocate(ClockPanel.CreateGraphics(), rect);
                
if (buffer.Graphics != null)
                
{
                    buffer.Graphics.Clear(
this.BackColor);
                    
this.DrawClock(buffer.Graphics, this.ClientRectangle);
                }

                buffer.Render();
                buffer.Dispose();
            }

        }


        
private void TimeBox_TextChanged(object sender, EventArgs e)
        
{
            NameLabel.Visible 
= false;
            PicBox.ImageLocation 
= "";
            PicBox.Visible 
= false;
        }

    }

}

主要实现思路:程序每刷新一次,就重新绘制一次表盘,同时显示时间对应的图片(图片预设36张)

绘制表盘的时候,Panel的背景需要被Cache 一次,

然后再在Panel上重新绘制,这样可以避免重新绘制整个

Form的背景带来的消耗
 

原创粉丝点击