使用Draw方法C#连接2个点 [英] Connection between 2 points with Draw method C#

查看:140
本文介绍了使用Draw方法C#连接2个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的问题是,当我想在表单上绘制2个点并连接它们时,这些点不会出现,但连接仍在表单上。我正在使用DrawEllipse和DrawLine方法。

Hello guys,
My problem is that when I want to draw 2 points on the form and connect them, the points do not appear but the connections are still on the form. I am using DrawEllipse and DrawLine methods.

public partial class Form1 : Form
   {
       private Point p1, p2;
       List<Point> p1List = new List<Point>();
       List<Point> p2List = new List<Point>();

       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Paint(object sender, PaintEventArgs e)
       {


           using (var p = new Pen(Color.Blue, 4))
           {
               for (int x = 0; x < p1List.Count; x++)
               {
                   e.Graphics.DrawLine(p, p1List[x], p2List[x]);
               }
           }
private void Form1_MouseDown(object sender, MouseEventArgs e)
       {

           Graphics gr = this.CreateGraphics();

           int x = e.X;
           int y = e.Y;

           // Create pen.
           Pen whitePen = new Pen(Color.Blue, 3);
           Pen red = new Pen(Color.Red, 3);
           SolidBrush whiteBrush = new SolidBrush(Color.Blue);

           // Create rectangle for ellipse.
           Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);

           gr.DrawEllipse(whitePen, rect);
           gr.FillEllipse(whiteBrush, rect);

           if (p1.X == 0)
           {
               p1.X = e.X;
               p1.Y = e.Y;
           }
           else
           {
               p2.X = e.X;
               p2.Y = e.Y;

               p1List.Add(p1);
               p2List.Add(p2);

               Invalidate();
               p1.X = 0;
           }
        }



       }



第一张图片你可以看到我第一次点击它绘制一个点的表格,然后在第二张图片上看到了连接,但是没有积分。

我需要你的帮助!





图片:

< img src =http://i61.tinypic.com/n2io08.jpgborder =0alt =由TinyPic托管的图片和视频>



< img src =http://i59.tinypic .com / 34hurly.jpgborder =0alt =图片和视频托管由TinyPic>

推荐答案

看起来你好像不要不了解图形渲染的概念。请参阅我最近的评论。任何失效都会消除在 Paint 事件的处理程序中绘制不支持的任何内容(或者,可选地,重写方法 Control.OnPaint



尝试了解渲染。我过去的答案可以帮助你:

什么样的俏皮方法是Paint?(DataGridViewImageCell.Paint(...)) [ ^ ],

捕获面板上的图纸 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



-SA
It looks like you don't understand the concept of graphic rendering. Please see my most recent comment. Any invalidation will wipe out anything not supported by drawing in the handler of the Paint event (or, optionally, overridden method Control.OnPaint.

Try to understand rendering. My past answers can help you:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
How to speed up my vb.net application?[^],
Drawing Lines between mdi child forms[^].

—SA


Graphics gr;

public Form1()

{

InitializeComponent();

}

private Point p1,p2;

List< point> p1List = new List< point>();

List< point> p2List = new List< point>();





private void Form1_Paint_1(object sender,PaintEventArgs e)

$







}

private void Form1_MouseDown_1(对象发送者) ,MouseEventArgs e)

{



gr = this.CreateGraphics();



int x = eX;

int y = eY;



//创建笔。

Pen whitePen = new Pen(Color.Blue,3);

Pen red = new Pen(Color.Red,3);

SolidBrush whiteBrush = new SolidBrush(Color。蓝色);



//为椭圆创建矩形。

矩形矩形=新矩形(x - 5,y - 5,10, 10);



gr.DrawEl lipse(whitePen,rect);

gr.FillEllipse(whiteBrush,rect);



if(p1.X == 0)

{

p1.X = eX;

p1.Y = eY;

}

else

{
Graphics gr;
public Form1()
{
InitializeComponent();
}
private Point p1, p2;
List<point> p1List = new List<point>();
List<point> p2List = new List<point>();


private void Form1_Paint_1( object sender, PaintEventArgs e )
{



}
private void Form1_MouseDown_1(object sender, MouseEventArgs e)
{

gr = this.CreateGraphics();

int x = e.X;
int y = e.Y;

// Create pen.
Pen whitePen = new Pen(Color.Blue, 3);
Pen red = new Pen(Color.Red, 3);
SolidBrush whiteBrush = new SolidBrush(Color.Blue);

// Create rectangle for ellipse.
Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);

gr.DrawEllipse(whitePen, rect);
gr.FillEllipse(whiteBrush, rect);

if (p1.X == 0)
{
p1.X = e.X;
p1.Y = e.Y;
}
else
{
<pre lang="c#">



p2.X = eX;

p2.Y = eY;



p1List.Add(p1);

p2List.Add(p2);





使用(var p =新笔(Color.Blue,4))

{

for(int i = 0;我< p1List.Count; i ++)

{

gr.DrawLine(p,p1List [i],p2List [i]);

}

}

p1.X = 0;

}

}



这可能对你有所帮助


p2.X = e.X;
p2.Y = e.Y;

p1List.Add(p1);
p2List.Add(p2);


using (var p = new Pen(Color.Blue, 4))
{
for (int i = 0; i < p1List.Count; i++)
{
gr.DrawLine(p, p1List[i], p2List[i]);
}
}
p1.X = 0;
}
}

this might help you


这篇关于使用Draw方法C#连接2个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆