最新做一个实验,有两个界面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();
}
}
我写的代码在下面:请帮帮忙,我该怎么做,谢谢.问题二:在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();
}
}
参看
http://blog.csdn.net/tjvictor/archive/2006/11/24/1412546.aspx
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();
}
}
}
}