请教一个问题:用c#编了一个winforms 窗体,在窗体中加入 timer 定时器,执行循环发送命令,现在的问题是屏幕闪烁得厉害!不知道怎么解决?求救

解决方案 »

  1.   

    timer 发送了什么使得屏幕闪动。
      

  2.   

    要不用线程控制..不要用timertimer也是线程+委托实现的..自己写个线程类控制更灵活
      

  3.   

    Interval值调高
    只要不是实时系统,把Interval调到1000是基本感觉不到的~
      

  4.   

    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();
                        
                
                        
                        //时针
                        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);
                }            
            }
        }
    }
      

  5.   

    谢谢这么多热心人!
    我的窗体中有listview 控件,通过timer定时控制刷新该控件的项值,timer的时间间隔是设为1000ms的,如果加长,就起不到实时效果了!该咋办呢?
      

  6.   

    我的写法是:
            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;
                    }
                }        }
      

  7.   

    先Clear再逐项添加肯定闪,建议RemoveAt(),Insert()之类,或对对应项进行修改