最新做一个实验,有两个界面form1和temForm,现在遇到两个问题.问题一:在temForm中我要做的是一个根据数据库里的数据来绘制出不同颜色的矩形若干个,并且还要保持实时刷新,能从界面上矩形颜色的变化看出数据库数据的变化,在这个界面中我用了一个时钟time1进行每3秒刷新一次temForm,但事实上这个功能并没有实现,绘图我用了双缓冲,图像是绘出来了,可是数据库里的数据在变化,界面却没有实现预期的刷新功能.请大家帮帮忙,如何实现?
我写的代码在下面:请帮帮忙,我该怎么做,谢谢.问题二:在form1中调用temform,让temform得以显示,并且还要实现  问题一 中期望的那样显示,但实际上,temform的界面是显示出来了,可是绘出来的图像只是一闪就没有了,过一会又是一闪又没有了,一直这样直到你关闭窗口.form1中调用temform的代码也在下面,请大家帮帮忙,我该怎么做,谢谢.以上两个问题答对任何一个均有分.问题一的代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace temperature
{
    public partial class temForm : Form
    {
        public temForm()
        {
            InitializeComponent();
        }
        private void temForm_Load(object sender, EventArgs e)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {           
            Graphics buffergraphics = e.Graphics;
            Image mybuffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
            Graphics g = Graphics.FromImage(mybuffer);            SqlConnection thisconnection = new SqlConnection(@"Data Source=(local);Initial Catalog=experiment;Integrated Security=True");
            thisconnection.Open();
            g.FillRectangle(Brushes.White, ClientRectangle);
            int i;
            int j;
            double m;
            Brush colorflag = Brushes.White;
            for (i = 10; i <= 300; i += 13)
            {
                for (j = 20; j <= 500; j += 16)
                {
                    SqlCommand thiscommand = thisconnection.CreateCommand();
                    thiscommand.CommandText = "select data from exp3 where x=" + i + " and y=" + j + " ";
                    SqlDataReader thisreader = thiscommand.ExecuteReader();
                    while (thisreader.Read())
                    {
                        m = Convert.ToDouble(thisreader["data"]);
                        if (m < 50)
                        { colorflag = Brushes.Lime; }
                        else if (m >= 50 & m < 70)
                        { colorflag = Brushes.SpringGreen; }
                        else if (m >= 70 & m < 90)
                        { colorflag = Brushes.PaleGreen; }
                        else if (m >= 90 )
                        { colorflag = Brushes.SeaGreen; }     
                    }
                    g.FillRectangle(colorflag, new Rectangle(i, j, 10, 10));
                    thisreader.Close();
                }
            }
            buffergraphics.DrawImage(mybuffer, ClientRectangle);
            mybuffer.Dispose();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate(); 
        }   }  }
问题二的代码如下:        private void temperaturemenu_Click(object sender, EventArgs e)
        {
            temperature tempfrm = new temperature();
            try
            {
                tempfrm.ShowDialog();
            }
            finally
            {
                tempfrm.Dispose();
            }
        }

解决方案 »

  1.   

    我做了一简单时钟,利用时钟激活 刷新panel,然后在panel上面加上Onpaint代码,这样就可以更新了。如果一闪一闪的,可能被其他刷新事件刷掉了,图像放在一个单独的控件里或许比较好
      

  2.   

    试过了,refresh 根本不行,求高手帮帮忙,感激不尽.!
      

  3.   

    要想让图形一直在界面上,就重写Onpaint事件。不过我建议你还是用第三方控件做比较好。
    参看
    http://blog.csdn.net/tjvictor/archive/2006/11/24/1412546.aspx
      

  4.   

    需要重写OnPaint事件  不过可以建议你用 listview 速度还可以 我做过一个类似的 就用的listview 不同颜色直接做成图片
      

  5.   

    怎么重写OnPaint事件啊?拜托大家帮我想个方法.谢了.
      

  6.   

    重写OnPaint事件,我有个显示Unicode字符的程序,写得不好,不过刚好可以给你参考。tjs1971 at gmail.com
      

  7.   

    怎么重写onPaint事件啊?不会是没有人知道吧,可以告诉我吗?我着急用啊,拜托了,拜托了!
      

  8.   

    我已经把onpaint事件的代码放在一个panel上了.可是还是不行啊?可不可以告诉我怎么做才可以重写onPaint事件啊?着急啊.上面代码都给出来了.我是初学者,可不可以说详细点.或是直接帮我写下代码好不好啊?谢谢大家了.
      

  9.   

    看下面的代码行不行?using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.Xml;namespace temperature
    {
        public partial class temForm : Form
        {
            public temForm()
            {
                InitializeComponent();            Timer timer = new Timer();
                timer.Enabled = true;
                timer.Interval = 3000;
                timer.Tick += new EventHandler(timer_Tick);                  }         SqlConnection thisconnection = new SqlConnection(@"Data Source=(local);Initial Catalog=experiment;Integrated Security=True");          private void temForm_Load(object sender, EventArgs e)
            {
                thisconnection.Open();
            }        void timer_Tick(object sender, EventArgs e)
            {
                this.paintTemp(this.ClientRectangle);
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                this.drawTemp(e.Graphics, e.ClipRectangle);
            }
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                this.paintTemp(this.ClientRectangle);
            }
            private void drawTemp(Graphics g, Rectangle rectangle)
            {
                int i;
                int j;
                double m;            Brush colorflag = Brushes.White;
                for (i = 60; i <= 918; i += 13)
                {
                    for (j = 100; j <= 628; j += 16)
                    {                    SqlCommand thiscommand = thisconnection.CreateCommand();
                        thiscommand.CommandText = "select data from exp3 where x=" + (((i - 50) / 13) + 1) + " and y=" +(((628 - j) / 16) + 1) + " ";                    SqlDataReader thisreader = thiscommand.ExecuteReader();                    while (thisreader.Read())
                        {
                            m = Convert.ToDouble(thisreader["data"]);                        if (m < 50)                        { colorflag = Brushes.Lime; }                        else if (m >= 50 & m < 70)
                            { colorflag = Brushes.SpringGreen; }                        else if (m >= 70 & m < 90)
                            { colorflag = Brushes.PaleGreen; }                        else if (m >= 90 )
                            { colorflag = Brushes.SeaGreen; }                       
                        }
                        g.FillRectangle(colorflag, new Rectangle(i, j, 10, 10));
                        thisreader.Close();
                    }
                }
            }
            internal void paintTemp(Rectangle rect)
            {
                if (!this.IsDisposed && this.Visible)
                {
                    BufferedGraphicsContext myContext = BufferedGraphicsManager.Current;
                    BufferedGraphics buffer = myContext.Allocate(this.CreateGraphics(), rect);
                    if (buffer.Graphics != null)
                    {
                        buffer.Graphics.Clear(this.BackColor);
                        this.drawTemp(buffer.Graphics, this.ClientRectangle);
                    }
                    buffer.Render();
                    buffer.Dispose();
                }
            }
        }
    }