求助:VS2010编译器正常设置,但是却不抛出异常 你代码有用到 Paint参数 clipBounds 吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只是重新绘制窗口而已protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (arr.Count > 2) for (int i = 1; i < arr.Count; i++) g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i])); } 能画出来还是会抛出异常?我的机器就是有异常死憋着不放,上次有个数组越界的异常也不抛出,后来直接运行程序的时候才发现,就是不知道这到底是什么情况没异常 一切运行正常 根据你问题改变窗口啥的,你可以发相对全一点代码上来看看去掉了窗口上的一个按钮,这是完整的源代码: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;using System.Collections;using System.Threading;namespace 鼠标划线{ public partial class Form1 : Form { public Form1() { InitializeComponent(); p = new Point(); arr = new ArrayList(); } bool bmove=false; Point p; Graphics g; ArrayList arr; private void Form1_MouseDown(object sender, MouseEventArgs e) { g = this.CreateGraphics(); bmove = true; p = new Point(e.X,e.Y); arr.Add(p); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (bmove) { g.DrawLine(Pens.Red,p,new Point(e.X,e.Y)); p = new Point(e.X,e.Y); arr.Add(p); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { bmove = false; g.Dispose(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (arr.Count > 2) for (int i = 1; i < arr.Count; i++) g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i])); } private void Form1_Paint(object sender, PaintEventArgs e) { } }} 当你MouseUp的时候 g.Dispose();释放了 所以执行下面语句会出错for (int i = 1; i < arr.Count; i++) g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i])); 你代码可以这样简化public partial class Form1 : Form { IList<Point> arr; public Form1() { InitializeComponent(); DoubleBuffered = true; arr = new List<Point>(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { arr.Add(new Point(e.X, e.Y)); this.Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (arr.Count == 1) { e.Graphics.FillRectangle(Brushes.Red, arr[0].X, arr[0].Y, 1, 1); } else { for (int i = 1; i < arr.Count; i++) e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]); } } } 这个代码好像只能把所有的线画成一条,每次鼠标重新点击都会把原来的线和现在的线连成一条,我把它改了一下,你看行不? public partial class Form1 : Form { List<Point> arr; IList<IList> arr_a; public Form1() { InitializeComponent(); DoubleBuffered = true; arr = new List<Point>(); arr_a = new List<IList>(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { arr.Add(new Point(e.X, e.Y)); this.Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (arr_a.Count == 0) { for (int i = 1; i < arr.Count; i++) e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]); } else { for (int i = 0; i < arr_a.Count; i++) for (int j = 1; j < arr_a[i].Count; j++) e.Graphics.DrawLine(Pens.Red, (Point)(arr_a[i])[j - 1], (Point)(arr_a[i])[j]); } } private void Form1_MouseDown(object sender, MouseEventArgs e) { arr = new List<Point>(); List<Point> newarr = arr; arr_a.Add(newarr); } } Form1_MouseDown: arr = new List<Point>(); arr_a.Add(arr); 紧急求助!很急!关于DLL调用的问题! DEV高手 求解 求邵志东视频里讲水晶报表里的一段代码 在线编辑器项目外包 串口通讯的.DLL文件 怎么可以自己填充表里的自动编号 ?希望指点. C#中如何清除画上去的文本? INSERT INTO 语句的语法错误,菜鸟求助各路大神啊! 在线等待!!!关于建立文件的问题 怎样读取word括号中选择题的答案,并将它存入数组 动态编译产生的程序集怎么释放 C#Socket异步通信在处理批量并发时有时出现数据重叠
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (arr.Count > 2)
for (int i = 1; i < arr.Count; i++)
g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
}
没异常 一切运行正常 根据你问题改变窗口啥的,你可以发相对全一点代码上来看看
去掉了窗口上的一个按钮,这是完整的源代码:
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;
using System.Collections;
using System.Threading;namespace 鼠标划线
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
p = new Point();
arr = new ArrayList();
}
bool bmove=false;
Point p;
Graphics g;
ArrayList arr;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
g = this.CreateGraphics();
bmove = true;
p = new Point(e.X,e.Y);
arr.Add(p);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (bmove)
{
g.DrawLine(Pens.Red,p,new Point(e.X,e.Y));
p = new Point(e.X,e.Y);
arr.Add(p);
}
} private void Form1_MouseUp(object sender, MouseEventArgs e)
{
bmove = false;
g.Dispose();
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (arr.Count > 2)
for (int i = 1; i < arr.Count; i++)
g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
} private void Form1_Paint(object sender, PaintEventArgs e)
{
}
}
}
g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
{
IList<Point> arr;
public Form1()
{
InitializeComponent(); DoubleBuffered = true;
arr = new List<Point>();
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
arr.Add(new Point(e.X, e.Y));
this.Invalidate();
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (arr.Count == 1)
{
e.Graphics.FillRectangle(Brushes.Red, arr[0].X, arr[0].Y, 1, 1);
}
else
{
for (int i = 1; i < arr.Count; i++)
e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]);
}
}
}
public partial class Form1 : Form
{
List<Point> arr;
IList<IList> arr_a;
public Form1()
{
InitializeComponent(); DoubleBuffered = true;
arr = new List<Point>();
arr_a = new List<IList>();
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
arr.Add(new Point(e.X, e.Y));
this.Invalidate();
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (arr_a.Count == 0)
{
for (int i = 1; i < arr.Count; i++)
e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]);
}
else
{
for (int i = 0; i < arr_a.Count; i++)
for (int j = 1; j < arr_a[i].Count; j++)
e.Graphics.DrawLine(Pens.Red, (Point)(arr_a[i])[j - 1], (Point)(arr_a[i])[j]);
}
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
arr = new List<Point>();
List<Point> newarr = arr;
arr_a.Add(newarr);
}
}
Form1_MouseDown:
arr = new List<Point>();
arr_a.Add(arr);