timer 发送了什么使得屏幕闪动。... 如果你做动画或者游戏可以看看下面我的代码using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Reflection; using System.IO; namespace DeskToolBar.Common { class Clock { Graphics g; Control p; Image backGround; Image secHand; Image minHand; Image houHand; Thread thr; public Clock() {
backGround =Resource1.BackGround; //Image.FromFile(Setting.Clock.ClockBackGround); secHand = Resource1.SecoundHand; //Image.FromFile(Setting.Clock.SecoundHand ); minHand = Resource1.MinutHand; //Image.FromFile(Setting.Clock.MinuteHand ); houHand = Resource1.HourHand;// Image.FromFile(Setting.Clock.HourHand); thr = new Thread(new ThreadStart(Draw)); } public Graphics SetGraphic { set { g = value; } } public Control SetPanel { set { p = value; p.Width = backGround.Width; p.Height = backGround.Height; } } public void Dispose() { thr.Abort (); } public void BeginDraw() { thr.Start(); } void Draw() { if (p != null && g != null) { int totalTime = 0; int secCount = DateTime.Now.Second ; int minCount = DateTime.Now.Minute ; int hourCount = ((DateTime.Now.Hour)); do { int timeSpan =1000; Point center = new Point(p.Width / 2, p.Height / 2); Graphics buffer ; //背景 Bitmap backGroundBit = new Bitmap(p.Width, p.Height); buffer = Graphics.FromImage(backGroundBit); buffer.Clear(Setting.Clock.MakeTransparentColor); buffer.DrawImage(backGround, new Rectangle(center.X - backGround.Width / 2, center.Y - backGround.Height / 2, backGround.Width, backGround.Height), new Rectangle(0, 0, backGround.Width, backGround.Height), GraphicsUnit.Pixel); //backGroundBit.MakeTransparent(Setting.Clock.MakeTransparentColor); buffer.Dispose();
只要不是实时系统,把Interval调到1000是基本感觉不到的~
如果你做动画或者游戏可以看看下面我的代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.IO;
namespace DeskToolBar.Common
{
class Clock
{
Graphics g;
Control p;
Image backGround;
Image secHand;
Image minHand;
Image houHand;
Thread thr;
public Clock()
{
backGround =Resource1.BackGround; //Image.FromFile(Setting.Clock.ClockBackGround);
secHand = Resource1.SecoundHand; //Image.FromFile(Setting.Clock.SecoundHand );
minHand = Resource1.MinutHand; //Image.FromFile(Setting.Clock.MinuteHand );
houHand = Resource1.HourHand;// Image.FromFile(Setting.Clock.HourHand);
thr = new Thread(new ThreadStart(Draw));
}
public Graphics SetGraphic
{
set
{
g = value;
}
}
public Control SetPanel
{
set
{
p = value;
p.Width = backGround.Width;
p.Height = backGround.Height;
}
}
public void Dispose()
{
thr.Abort ();
}
public void BeginDraw()
{
thr.Start();
}
void Draw()
{
if (p != null && g != null)
{
int totalTime = 0;
int secCount = DateTime.Now.Second ;
int minCount = DateTime.Now.Minute ;
int hourCount = ((DateTime.Now.Hour));
do
{ int timeSpan =1000;
Point center = new Point(p.Width / 2, p.Height / 2);
Graphics buffer ;
//背景
Bitmap backGroundBit = new Bitmap(p.Width, p.Height);
buffer = Graphics.FromImage(backGroundBit);
buffer.Clear(Setting.Clock.MakeTransparentColor);
buffer.DrawImage(backGround, new Rectangle(center.X - backGround.Width / 2, center.Y - backGround.Height / 2, backGround.Width, backGround.Height), new Rectangle(0, 0, backGround.Width, backGround.Height), GraphicsUnit.Pixel);
//backGroundBit.MakeTransparent(Setting.Clock.MakeTransparentColor);
buffer.Dispose();
//时针
Bitmap houHandBit = new Bitmap(p.Width, p.Height);
buffer = Graphics.FromImage(houHandBit);
buffer.TranslateTransform(center.X, center.Y);
buffer.RotateTransform((float)hourCount * 360f / 24f +90 );
buffer.TranslateTransform(0, -10);
buffer.DrawImage(houHand, 0 ,0);
//houHandBit.MakeTransparent(Setting.Clock.MakeTransparentColor);
buffer.Dispose();
//分针
Bitmap minHandBit = new Bitmap(p.Width, p.Height);
buffer = Graphics.FromImage(minHandBit);
buffer.TranslateTransform(center.X, center.Y);
buffer.RotateTransform((float)minCount * 6+180 );
buffer.TranslateTransform(0, -10);
buffer.DrawImage(minHand, 0,0 );
//minHandBit.MakeTransparent(Setting.Clock.MakeTransparentColor);
buffer.Dispose();
//秒针
Bitmap secHandBit = new Bitmap(p.Width, p.Height);
buffer = Graphics.FromImage(secHandBit);
buffer.TranslateTransform(center.X, center.Y);
buffer.RotateTransform(((float)secCount) * 6 +180 );
buffer.TranslateTransform(0, -10);
buffer.DrawImage(secHand, 0, 0 );
//secHandBit.MakeTransparent(Setting.Clock.MakeTransparentColor);
buffer.Dispose();
//输出时间
Font font = new Font("Arial", 12, FontStyle.Bold);
SizeF size = g.MeasureString(DateTime.Now.ToLongTimeString(), font);
Bitmap wordsBit = new Bitmap((int)size.Width+1, (int)size.Height+1);
buffer = Graphics.FromImage(wordsBit);
SolidBrush brush;
//brush = new SolidBrush(Color.White);
//buffer.DrawString(DateTime.Now.ToLongTimeString(), font, brush, new PointF(0, 0)); brush = new SolidBrush(Color.Pink);
buffer.DrawString(DateTime.Now.ToLongTimeString(), font, brush, new PointF(1, 1)); //合成
g.DrawImage(backGroundBit, new Point(0, 0));
g.DrawImage(houHandBit, new Point(0, 0));
g.DrawImage(minHandBit, new Point(0, 0));
g.DrawImage(secHandBit, new Point(0, 0));
g.DrawImage(wordsBit, new PointF(center.X - size.Width / 2, center.Y + 12));
wordsBit.Dispose();
backGroundBit.Dispose();
secHandBit.Dispose();
minHandBit.Dispose();
houHandBit.Dispose(); totalTime++; //secCount = (secCount % (60 * scale)) + 1;
//if (totalTime % 60 == 0)
//{
// minCount = (minCount % (60 * scale)) + 1;
//} //if (totalTime % 3600 == 0)
//{
// hourCount = (hourCount % (24 * scale)) + 1;
//}
secCount = DateTime.Now.Second;
minCount = DateTime.Now.Minute;
hourCount = ((DateTime.Now.Hour)); thr.Join(timeSpan); }
while (true);
}
}
}
}
我的窗体中有listview 控件,通过timer定时控制刷新该控件的项值,timer的时间间隔是设为1000ms的,如果加长,就起不到实时效果了!该咋办呢?
private void timer1_Tick(object sender, EventArgs e)
{
foreach (ListViewItem item in this.listView1.Items)
{
if (item.Tag != null)
{
ParamObject po = item.Tag as ParamObject;
item.SubItems[2].Text = po.Value;
}
} }