[急!100分]如何用C#写绘图代码,请高手帮帮忙啊 就是像Windows附件里的绘图工具画图的方法一样啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private bool isdown=false;private Point p1 = Point.Empty;private Point p2 = Point.Empty;private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { isdown=true; pic.Capture = true; p1 = pic.PointToScreen(new Point(e.X, e.Y)); } private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { isdown=false; pic.Capture = false; p2 = Point.Empty; } private void pic_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if(isdown) { Point p = pic.PointToScreen(new Point(e.X, e.Y)); if(p2 != Point.Empty) { ControlPaint.DrawReversibleLine(p1, p2, Color.Black); } p2 = p; ControlPaint.DrawReversibleLine(p1, p2, Color.Black); } }这只是一个橡皮筋法的写法,你如果要刷新时保留所画的线,你把每条线(鼠标up)的坐标放到集合里,在onpaint里重绘。 程序中的pic是picture控件,那几个方法你需要挂到相应的事件上。 GDI+没有提供这功能,只能调用winapi了codeproject有示例 xxdneu(xxd) 的方法我试试看不知 Yok(黄旭) 说的“只能调用winapi了codeproject有示例”作何解,能否明示谢过大家了先 to lengyuewuhen(无痕) :橡皮筋都解决了,重绘不难了吧,任何窗口上的物件都得记录在内存里啊,你不记录怎么重绘,怕耗内存就不要重绘。 耗内存只是一个相对的概念而已,就是你声明一个int变量也要耗内存的啊, 记录几个点连1k的内存也用不了,如果你的内存连32M 都没有,那你就不要试着运行.NET应用程序。 其实即使是画图程序也需要通过某种方式记录上面画的东西的,否则的话你想它窗口怎么实现的重绘。下边是实现重绘的代码private ArrayList lines = new ArrayList();struct Line { public Point p1; public Point p2; public Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } }private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { isdown=false; pic.Capture = false; if(p2 != Point.Empty) { lines.Add(new Line(pic.PointToClient(p1), pic.PointToClient(p2))); } p2 = Point.Empty; }private void pic_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Pen p = new Pen(Color.Black, 1); foreach(Object l in lines) { Line line = (Line)l; e.Graphics.DrawLine(p, line.p1, line.p2); } p.Dispose(); }把pic_Paint方法挂到 pic的paint事件上 谢谢 xxdneu(xxd)我刚开始接触这东东但又要交个作业,所以来不及自己慢慢琢磨本以为不行了的,我就想放弃用C#来做了。你的指点让我看到了希望,我真舍不得放弃C#以后肯定还有好多地方要向请教你的,希望不吝赐教谢谢了先 C# winform窗体登录代码 combobox c#怎样实现桌面下雪 控件问题,解决另外开贴送分 WEB服务,谁给我个简单例子, C#中如何使用水晶报表 有做过自定义控件的高手请进 c#中dll调用问题 DataView 绑定 CheckBox,高手请进,高手不难(送分题) 怎么让控件自动判断被用户输入的值 程序为什么不监听呢? MDI父窗口显示MDI子窗口的位置能不能搞个背景图片上去啊?应该怎么搞呢?多谢
private Point p1 = Point.Empty;
private Point p2 = Point.Empty;private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
isdown=true;
pic.Capture = true;
p1 = pic.PointToScreen(new Point(e.X, e.Y));
} private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isdown=false;
pic.Capture = false;
p2 = Point.Empty;
} private void pic_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(isdown)
{
Point p = pic.PointToScreen(new Point(e.X, e.Y));
if(p2 != Point.Empty)
{
ControlPaint.DrawReversibleLine(p1, p2, Color.Black);
}
p2 = p;
ControlPaint.DrawReversibleLine(p1, p2, Color.Black);
}
}这只是一个橡皮筋法的写法,你如果要刷新时保留所画的线,你把每条线(鼠标up)的坐标放到集合里,在onpaint里重绘。 程序中的pic是picture控件,那几个方法你需要挂到相应的事件上。
codeproject有示例
不知 Yok(黄旭) 说的“只能调用winapi了codeproject有示例”作何解,能否明示
谢过大家了先
struct Line
{
public Point p1;
public Point p2;
public Line(Point p1, Point p2)
{
this.p1 = p1;
this.p2 = p2;
}
}
private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isdown=false;
pic.Capture = false;
if(p2 != Point.Empty)
{
lines.Add(new Line(pic.PointToClient(p1), pic.PointToClient(p2)));
}
p2 = Point.Empty;
}
private void pic_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pen p = new Pen(Color.Black, 1);
foreach(Object l in lines)
{
Line line = (Line)l;
e.Graphics.DrawLine(p, line.p1, line.p2);
}
p.Dispose();
}把pic_Paint方法挂到 pic的paint事件上
我刚开始接触这东东
但又要交个作业,所以来不及自己慢慢琢磨
本以为不行了的,我就想放弃用C#来做了。
你的指点让我看到了希望,我真舍不得放弃C#
以后肯定还有好多地方要向请教你的,希望不吝赐教
谢谢了先