在屏幕上画图后如何让他不消失

wangearn 2007-01-24 06:18:11
本来已经完成一个小程序,通过鼠标的移动在屏幕上画线。但是只要被其它窗体覆盖后原来画的就消失了,怎样才能让他保持不消失呢
...全文
542 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2007-01-24
  • 打赏
  • 举报
回复
这样啊,我这里有一个画线段的,楼主可以参考下(环境是VS2005):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DoubleBufferDraw
{
class LineObj
{
private Point m_start;
private Point m_end;
public LineObj(Point start, Point end)
{
this.m_start = start;
this.m_end = end;
}
public void Draw(Graphics g, Pen pen)
{
g.DrawLine(pen, m_start, m_end);
}
}
public partial class Form2 : Form
{
private Point m_startPoint = Point.Empty;
List<LineObj> lineList = new List<LineObj>();
public Form2()
{
InitializeComponent();
}
public event EventHandler someEvent;
private void drawCircle(Graphics graphics, Point startPoint, Point endPoint)
{
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle);
bg.Graphics.Clear(this.BackColor);
foreach (LineObj line in this.lineList)
{
line.Draw(bg.Graphics, SystemPens.ControlText);
}
bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint);
bg.Render();
bg.Dispose();
bg = null;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (LineObj line in this.lineList)
{
line.Draw(e.Graphics, SystemPens.ControlText);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.m_startPoint = new Point(e.X, e.Y);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
this.drawCircle(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y));
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
LineObj line = new LineObj(this.m_startPoint, e.Location);
this.lineList.Add(line);
}
}
}
wangearn 2007-01-24
  • 打赏
  • 举报
回复
hbxtlhx(平民百姓)大哥,看来你是这方面的专家呀,还有代码吗,贴一段让偶学习学习
viena 2007-01-24
  • 打赏
  • 举报
回复
是窗体吗,在Form.Activated事件重画就可以了
lizhizhe2000 2007-01-24
  • 打赏
  • 举报
回复
用一个结构保存一下你直线的位置(起始坐标和终止传标),然后在重给事件中还原
北京的雾霾天 2007-01-24
  • 打赏
  • 举报
回复
要想被其它窗体覆盖后原来画的不消失,你必需要把这原来车的信息存起来,有两种方式:
1)建立一个Bitmap,所有的画图都画到这个Bitmap中,然后在程序里显示这个Bitmap且要在Paint事件里或重写的Onpaint方法里对这个Bitmap画出.
2)建立一个集合,用来存放原来的所有的以画的对象的数据信息.在Paint事件里或重写的OnPaint方法里循环这个集合以次画出这个集合里的所有的对象.
viena 2007-01-24
  • 打赏
  • 举报
回复
不可能不消失,实际上是系统重画桌面把你画的覆盖掉了,这是你无法避免的~

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧