日期:2014-05-19  浏览次数:21125 次

C#绘制时钟时出现闪烁,自己试着用双缓冲也没有解决,大家帮忙看看怎么回事,谢谢了!
如题

------解决方案--------------------
给你一个参考的例子,是一个时钟,双缓冲绘制的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DoubleBufferDraw
{
public partial class clock : Form
{
public clock()
{
InitializeComponent();
Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
}

void timer_Tick(object sender, EventArgs e)
{
this.paintClock(this.ClientRectangle);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.drawClock(e.Graphics, e.ClipRectangle);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.paintClock(this.ClientRectangle);
}
private void drawClock(Graphics g, Rectangle rectangle)
{
SolidBrush brush1 = new SolidBrush(Color.SteelBlue);
SolidBrush brush2 = new SolidBrush(Color.SkyBlue);
SolidBrush brush3 = new SolidBrush(Color.Teal);
SolidBrush brush4 = new SolidBrush(Color.Teal);
SolidBrush brush5 = new SolidBrush(Color.Black);
Pen pen1 = new Pen(brush1, 2.0f); // "时点 "画笔
Pen pen2 = new Pen(brush2, 1.5f); // "分点 "画笔
Pen pen3 = new Pen(brush3, 0.2f); // "时针 "画笔
Pen pen4 = new Pen(brush4, 0.2f); // "分针 "画笔
Pen pen5 = new Pen(brush5, 1); // "秒针 "画笔
Point center = new Point(this.ClientRectangle.Left + this.ClientRectangle.Width / 2, this.ClientRectangle.Top + this.ClientRectangle.Height / 2);
//画表盘
for (int i = 1; i <= 60; i++)
{
if (i % 5 == 0)
g.DrawEllipse(pen1, center.X + (int)(Math.Cos(Math.PI / 180 * i * 6) * 60), center.Y + (int)(Math.Sin(Math.PI / 180 * i * 6) * 60), 2.0f, 2.0f);
else
g.DrawEllipse(pen2, center.X + (int)(Math.Cos(Math.PI / 180 * i * 6) * 60), center.Y + (int)(Math.Sin(Math.PI / 180 * i * 6) * 60), 1.5f, 1.5f);
}
//画时针
Point ph = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour - 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 40), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour - 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 40));
Point phl = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour + 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 7), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour + 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 7));
Point pch1 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * -5), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * -5));
Point pch2 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * 5), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * 5));
g.FillPolygon(brush1, new Point[] { ph, pch1, phl, pch2 });
//画分针
Point pm = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute - 15) * 6) * 48), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute - 15) * 6) * 48));
Point pml = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute + 15) * 6) * 9), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute + 15) * 6) * 9));
Point pcm1 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute) * 6) * -4), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute) * 6) * -4));