Invalidate触发paint事件,但是想保存前一步的成果 RT小弟使用Invalidate触发paint事件画图,分很多部分画。怎么样才能使得下次的Invalidate调用不影响我前面已经画好的部分呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的问题没有具体的要求,我不好给出你需要的代码。这里我举个例子。我的方法是把已画的内容存到位图中。以下例子实现了每按一次按钮在窗口绘制一条横线。using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace InvalidateSample{ public partial class Form1 : Form { private int _height = 0; private Bitmap _bitmap; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { _bitmap = new Bitmap(this.Width, this.Height); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics graphics = Graphics.FromImage(_bitmap); graphics.DrawLine(new Pen(Color.Blue), new Point(0, _height), new Point(100, _height)); graphics.Dispose(); e.Graphics.DrawImage(_bitmap, 0, 0); _height += 10; } private void button1_Click(object sender, EventArgs e) { this.Invalidate(); } }}不过提醒下位图的大小是有限制的。 base.OnPaint(e);是什么意思呢?呵呵我的大概就是这个意思哈 我以前做的就保存在位图里面的,但是我现在换了个方法。就出不来了 小弟是新手 对GDI很不熟悉 public void CreatGrid(int index)//画网格的事件触发方法 { OleDbConnection conn = new OleDbConnection(Oledbconn); string olestr = "select * from GridBook"; OleDbDataAdapter Object_adapter = new OleDbDataAdapter(olestr, conn); Object_adapter.Fill(myds, "GridBook"); n_x = int.Parse(myds.Tables["GridBook"].Rows[0][0].ToString()); n_y = int.Parse(myds.Tables["GridBook"].Rows[0][1].ToString()); PaintIndex = index; pictureBox1.Invalidate();private void pictureBox1_Paint(object sender, PaintEventArgs e)//paint事件 { Graphics gph =e.Graphics; switch(PaintIndex) { case 1://画坐标PaintIndex=1 Font AxisFont = new Font("Stylus BT", 12, FontStyle.Bold);//定义坐标轴的字体 Font LableFont = new Font("黑体", 13, FontStyle.Bold); SolidBrush AxisBrush = new SolidBrush(Color.Black);//定义坐标轴字体笔刷 StringFormat StrFormat = new StringFormat();//定义文本布局方式 StrFormat.FormatFlags = StringFormatFlags.DirectionVertical; gph.Clear(Color.Gainsboro); Ptu_H = this.pictureBox1.Height; Pen pen = new Pen(Color.Black); pen.Width = 2.5F; gph.DrawString(xn, LableFont, AxisBrush, (float)(lx * xsle / 2 + 60), (Ptu_H - 50)); //画坐标label gph.DrawString(yn, LableFont, AxisBrush, 20, (float)(Ptu_H - ly * ysle / 2 - 120), StrFormat);//画坐标label gph.DrawLine(pen, 100, (float)(Ptu_H - 100), 100, (float)(Ptu_H - ly * ysle - 100));//画Y轴 gph.DrawLine(pen, 100, (float)(Ptu_H - 100), (float)(lx * xsle + 100), (float)(Ptu_H - 100));//画X轴 double x = 0, y = 0; double nx = 0, ny = 0;//定义坐标点的个数 nx = lx / dx; ny = ly / dy; for (int i = 0; i < (nx + 1); i++) { gph.DrawString("-", AxisFont, AxisBrush, (float)(x + 89), (float)(Ptu_H - 110), StrFormat);//画X坐标刻度 gph.DrawString(Convert.ToString((int)(x / 10 / xsle)), AxisFont, AxisBrush, (float)(x + 90), (float)(Ptu_H - 90));//画X坐标值 x = x + dx * xsle; } for (int j = 0; j < (ny + 1); j++) { gph.DrawString("-", AxisFont, AxisBrush, 97, (float)(Ptu_H - y - 111));//画Y坐标刻度 gph.DrawString(Convert.ToString((int)(y / 10 / ysle)), AxisFont, AxisBrush, 60, (float)(Ptu_H - y - 108));//画Y坐标值 y = y + dy * ysle; } //AxisBrush.Dispose(); //AxisFont.Dispose(); pictureBox1.Image = b1; break; case 2://画对象 Brush mySb = new SolidBrush(clr); Point[] myP = new Point[4]; myP[0].X = (int)(x1 * xsle + 100); myP[0].Y = (int)(Ptu_H - 100 - y1 * ysle); myP[1].X = (int)(x2 * xsle + 100); myP[1].Y = (int)(Ptu_H - 100 - y1 * ysle); myP[2].X = (int)(x2 * xsle + 100); myP[2].Y = (int)(Ptu_H - 100 - y2 * ysle); myP[3].X = (int)(x1 * xsle + 100); myP[3].Y = (int)(Ptu_H - 100 - y2 * ysle); Pen MtePen = new Pen(Color.DarkSlateGray, 3F); gph.DrawPolygon(MtePen, myP); gph.FillPolygon(mySb, myP); //pictureBox1.Invalidate(); //pictureBox1.Refresh(); break; case 3://画网格 double mx = MX * 10; double my = MY * 10; Pen gridPen = new Pen(Color.Black, 0.1F); double xx = 0, yy = 0; double dxx, dyy; dxx = mx / n_x; dyy = my / n_y; for (int i = 0; i < (n_x + 1); i++) { gph.DrawLine(gridPen, (float)(xx + 100), (float)(Ptu_H - 100), (float)(xx + 100), (float)(Ptu_H - 100 - my * ysle)); xx = (double)(xx + dxx * xsle); } for (int j = 0; j < (n_y + 1); j++) { gph.DrawLine(gridPen, 100, (float)(Ptu_H - yy - 101), (float)(100 + mx * xsle), (float)(Ptu_H - yy - 101)); yy = (double)(yy + dyy * ysle); } pictureBox1.Image = b1; break; case 4://画边界条件 Pen myPen = new Pen(BoundaryColor, 0.1F); gph.DrawEllipse(myPen, (float)(BoundaryX * xsle + 97), (float)(Ptu_H - BoundaryY * ysle - 103), 6, 8); break; case 5://画监视点 Brush mySB = new SolidBrush(Color.Black); Pen mypotPen = new Pen(Color.Black, 0.1F); gph.DrawEllipse(mypotPen, (float)(MonitorX * xsle + 97), (float)(Ptu_H - MonitorY * ysle - 103), 6, 6); gph.FillEllipse(mySB, (float)(MonitorX * xsle + 97), (float)(Ptu_H - MonitorY * ysle - 103), 6, 6); break; }}我的程序时画一个数据网格,先画坐标,再画对象,网格,边界条件,监视点。(我是列出了网格的类)我先画了坐标 在画对象的时候 就清空了 画网格再把前面的清空了。大侠帮忙解决一下 。不胜感激 你仔细看看我上面的例子。我是先把图画到一个bitmap上,然后再把这个bitmap画到指定的位置。而这个bitmap是这个窗口的一个私有属性。绘制的所有结果都保存在这个bitmap里,所以不会丢失。在你的程序中,你的所有内容都是用Graphics gph =e.Graphics;绘制的。每次Invalidate自然原来的内容都没了。你看看我的。我的是用Graphics graphics = Graphics.FromImage(_bitmap);绘制的。然后才把这张图用e.Graphics绘制到指定位置的。 我发现我的作图思想没对哈 ,不应该用swith每次都需要重绘的,我设置了bool变量 没问题了 兄弟,感谢你的提点了哈 一个结构体构造方法参数旁边加:this()的问题 Attrubutes.Add() 隐藏托盘图标的问题!!希望得到大家的帮助谢谢 PropertyGrid中的项目是否能够通过数据库来取得? 没搞过winform开发,请给个例子 快速开贴揭帖,求一个简单的字符串处理算法 请教,关于textbox控件的焦点事件顺序中,CancelEventArgs.Cancel=true的疑惑 文件定位 请教 jQuery ajax队列怎样实现?第二个队列获取第一个队列的返回值 前台怎么调用后台变量? 派生类中如果写一个和父类方法签名完全相同的但没有new和+new有什么区别
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace InvalidateSample
{
public partial class Form1 : Form
{
private int _height = 0;
private Bitmap _bitmap; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
_bitmap = new Bitmap(this.Width, this.Height);
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = Graphics.FromImage(_bitmap);
graphics.DrawLine(new Pen(Color.Blue), new Point(0, _height), new Point(100, _height));
graphics.Dispose(); e.Graphics.DrawImage(_bitmap, 0, 0); _height += 10;
} private void button1_Click(object sender, EventArgs e)
{
this.Invalidate();
}
}
}
不过提醒下位图的大小是有限制的。
是什么意思呢?
呵呵我的大概就是这个意思哈 我以前做的就保存在位图里面的,但是我现在换了个方法。
就出不来了 小弟是新手 对GDI很不熟悉
{
OleDbConnection conn = new OleDbConnection(Oledbconn);
string olestr = "select * from GridBook";
OleDbDataAdapter Object_adapter = new OleDbDataAdapter(olestr, conn);
Object_adapter.Fill(myds, "GridBook"); n_x = int.Parse(myds.Tables["GridBook"].Rows[0][0].ToString());
n_y = int.Parse(myds.Tables["GridBook"].Rows[0][1].ToString());
PaintIndex = index;
pictureBox1.Invalidate();private void pictureBox1_Paint(object sender, PaintEventArgs e)//paint事件
{
Graphics gph =e.Graphics;
switch(PaintIndex)
{
case 1://画坐标PaintIndex=1
Font AxisFont = new Font("Stylus BT", 12, FontStyle.Bold);//定义坐标轴的字体
Font LableFont = new Font("黑体", 13, FontStyle.Bold);
SolidBrush AxisBrush = new SolidBrush(Color.Black);//定义坐标轴字体笔刷
StringFormat StrFormat = new StringFormat();//定义文本布局方式
StrFormat.FormatFlags = StringFormatFlags.DirectionVertical;
gph.Clear(Color.Gainsboro); Ptu_H = this.pictureBox1.Height;
Pen pen = new Pen(Color.Black);
pen.Width = 2.5F; gph.DrawString(xn, LableFont, AxisBrush, (float)(lx * xsle / 2 + 60), (Ptu_H - 50)); //画坐标label
gph.DrawString(yn, LableFont, AxisBrush, 20, (float)(Ptu_H - ly * ysle / 2 - 120), StrFormat);//画坐标label gph.DrawLine(pen, 100, (float)(Ptu_H - 100), 100, (float)(Ptu_H - ly * ysle - 100));//画Y轴
gph.DrawLine(pen, 100, (float)(Ptu_H - 100), (float)(lx * xsle + 100), (float)(Ptu_H - 100));//画X轴
double x = 0, y = 0;
double nx = 0, ny = 0;//定义坐标点的个数
nx = lx / dx;
ny = ly / dy;
for (int i = 0; i < (nx + 1); i++)
{ gph.DrawString("-", AxisFont, AxisBrush, (float)(x + 89), (float)(Ptu_H - 110), StrFormat);//画X坐标刻度
gph.DrawString(Convert.ToString((int)(x / 10 / xsle)), AxisFont, AxisBrush, (float)(x + 90), (float)(Ptu_H - 90));//画X坐标值 x = x + dx * xsle;
}
for (int j = 0; j < (ny + 1); j++)
{ gph.DrawString("-", AxisFont, AxisBrush, 97, (float)(Ptu_H - y - 111));//画Y坐标刻度
gph.DrawString(Convert.ToString((int)(y / 10 / ysle)), AxisFont, AxisBrush, 60, (float)(Ptu_H - y - 108));//画Y坐标值
y = y + dy * ysle;
} //AxisBrush.Dispose();
//AxisFont.Dispose();
pictureBox1.Image = b1; break;
case 2://画对象
Brush mySb = new SolidBrush(clr);
Point[] myP = new Point[4];
myP[0].X = (int)(x1 * xsle + 100); myP[0].Y = (int)(Ptu_H - 100 - y1 * ysle);
myP[1].X = (int)(x2 * xsle + 100); myP[1].Y = (int)(Ptu_H - 100 - y1 * ysle);
myP[2].X = (int)(x2 * xsle + 100); myP[2].Y = (int)(Ptu_H - 100 - y2 * ysle);
myP[3].X = (int)(x1 * xsle + 100); myP[3].Y = (int)(Ptu_H - 100 - y2 * ysle); Pen MtePen = new Pen(Color.DarkSlateGray, 3F);
gph.DrawPolygon(MtePen, myP);
gph.FillPolygon(mySb, myP);
//pictureBox1.Invalidate();
//pictureBox1.Refresh(); break;
case 3://画网格
double mx = MX * 10;
double my = MY * 10; Pen gridPen = new Pen(Color.Black, 0.1F); double xx = 0, yy = 0;
double dxx, dyy;
dxx = mx / n_x;
dyy = my / n_y;
for (int i = 0; i < (n_x + 1); i++)
{
gph.DrawLine(gridPen, (float)(xx + 100), (float)(Ptu_H - 100), (float)(xx + 100), (float)(Ptu_H - 100 - my * ysle));
xx = (double)(xx + dxx * xsle); }
for (int j = 0; j < (n_y + 1); j++)
{
gph.DrawLine(gridPen, 100, (float)(Ptu_H - yy - 101), (float)(100 + mx * xsle), (float)(Ptu_H - yy - 101));
yy = (double)(yy + dyy * ysle); }
pictureBox1.Image = b1;
break; case 4://画边界条件
Pen myPen = new Pen(BoundaryColor, 0.1F);
gph.DrawEllipse(myPen, (float)(BoundaryX * xsle + 97), (float)(Ptu_H - BoundaryY * ysle - 103), 6, 8);
break; case 5://画监视点
Brush mySB = new SolidBrush(Color.Black);
Pen mypotPen = new Pen(Color.Black, 0.1F);
gph.DrawEllipse(mypotPen, (float)(MonitorX * xsle + 97), (float)(Ptu_H - MonitorY * ysle - 103), 6, 6);
gph.FillEllipse(mySB, (float)(MonitorX * xsle + 97), (float)(Ptu_H - MonitorY * ysle - 103), 6, 6);
break;
}
}我的程序时画一个数据网格,先画坐标,再画对象,网格,边界条件,监视点。(我是列出了网格的类)我先画了坐标 在画对象的时候 就清空了 画网格再把前面的清空了。大侠帮忙解决一下 。不胜感激
在你的程序中,你的所有内容都是用Graphics gph =e.Graphics;绘制的。每次Invalidate自然原来的内容都没了。你看看我的。我的是用Graphics graphics = Graphics.FromImage(_bitmap);
绘制的。然后才把这张图用e.Graphics绘制到指定位置的。
每次都需要重绘的,我设置了bool变量 没问题了 兄弟,感谢你的提点了哈