- 积分
- 0
- 在线时间
- 3 小时
- 主题
- 2
- 注册时间
- 2019-12-12
- 帖子
- 4
- 最后登录
- 2019-12-16
- 帖子
- 4
- 软币
- 96
- 在线时间
- 3 小时
- 注册时间
- 2019-12-12
|
大家好,本人是C#的初学者,现在碰到个DrawLine的问题。
1.我如果直接调用DrawLine没有任何问题,主要代码如下:
foreach (var lines in marchingSquares.contourData)
{
foreach (var line in lines.Value)
{
int cur_startY = Convert.ToInt32(line.startPoint.Y * length);
int cur_startX = Convert.ToInt32(image.Height - line.startPoint.X * length);
int cur_endY = Convert.ToInt32(line.endPoint.Y * length);
int cur_endX = Convert.ToInt32(image.Height - line.endPoint.X * length);
g.DrawLine(pen, cur_startY, cur_startX, cur_endY, cur_endX);
}
}
2.但是现在为了实现线条的可编辑,自己定义了一个控件,在控件中drawLine,主要代码如下:
List<Line_EXT> lineExtList = new List<Line_EXT>();
foreach (var lines in marchingSquares.contourData)
{
foreach (var line in lines.Value)
{
int cur_startY = Convert.ToInt32(line.startPoint.Y * length);
int cur_startX = Convert.ToInt32(image.Height - line.startPoint.X * length);
int cur_endY = Convert.ToInt32(line.endPoint.Y * length);
int cur_endX = Convert.ToInt32(image.Height - line.endPoint.X * length);
lineExtList.Add(new Line_EXT(cur_startY,cur_startX,cur_endY,cur_endX));
}
}
mouseDragCurveImpl = new MouseDragCurve(g,pen,lineExtList);
mouseDragCurveImpl.Location = new Point(0, 0);
mouseDragCurveImpl.Dock = System.Windows.Forms.DockStyle.Fill;
this.contourMap.Controls.Add(mouseDragCurveImpl);
lineExtList保存了所有线段的起点和终点。MouseDragCurve class定义如下:
class MouseDragCurve : Control
{
// client rectangle
Rectangle clientRect;
//flag
bool mouseDown = false;
private Graphics g;
private Pen pen;
List<Line_EXT> lineExtList;
#region Inits
public MouseDragCurve(Graphics g,Pen pen,List<Line_EXT> lineExtList)
{
this.g = g;
this.pen = pen;
this.lineExtList = lineExtList;
this.BackColor = Color.White;
this.DoubleBuffered = true; // 双缓冲,避免闪烁
}
void drawCurve()
{
for(int i = 0;i < lineExtList.Count;i++)
{
g.DrawLine(pen,lineExtList[i].startY,lineExtList[i].startX,lineExtList[i].endY,lineExtList[i].endX);
}
}
protected override void OnPaint(PaintEventArgs e)
{
drawCurve();
}
经过调试OnPaint,drawCurve都调用到了,为什么图没显示出来,请大家指教,谢谢。不胜感激!
|
|