与箭头锚线指向包含圆问题的小图片框 [英] Line with Arrow Anchor pointing to small pictureboxes containing circle problem

查看:98
本文介绍了与箭头锚线指向包含圆问题的小图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



这是我的第一个问题,所以请保持温柔:)



我正在开发VS2010 vb.net winform应用程序。该表格有一个大的图片框(Picturebox1)和动态创建的少量图片框,名为pin_00,pin_01 ... pin_nn,散落在周围。小图片盒的图像是一个14x14的小圆圈,小图片盒的sizeMode设置为AutoSize



主要目的是从一个引脚画一条线另一个在图片框上有一条线(ArrowAnchor上限)。线箭头的头部必须指向小圆圈的中心,我已经完成了。



问题是线条被绘制时,箭头锚头(由于线条延伸到小图片框的中心)被小图片框隐藏



现在,我想要的是让线条仅到圆周圆圈,使箭头可见,仍然指向小图片框的中心。



我知道解决方案只是简单的圆和线方程找到交点。但我不能做对。请帮忙。



这是我试过的代码:



Dear Friends,

This is my first question here, so please be gentle :)

I am working on a VS2010 vb.net winform application. The form has one big picturebox (Picturebox1) and dynamically created small numbers of picturebox, named pin_00, pin_01 ... pin_nn, scattered around. The small picturebox has an image a small circle which is 14x14 in size and the small picturebox's sizeMode is set to AutoSize

The main aim is to draw a line from one pin to another with a line (ArrowAnchor caps) on picturebox. The line arrow acnchor head must point to the center of the small circle, which i have accomplished.

The problem is when the line is drawn, the Arrow Anchor Head (since the line extends up to the center of the small picturebox) is hidden by the small picturebox

Now, what i want is to make the line reach only to the circumference of the circle, so that the arrow head will be visible and still point to the center of the small picturebox.

I know that the solution is just plain circle and line equation finding the point of intersection. But i cant make it right. Please help.

Here is the code i have tried:

 Dim mPen As Pen = New Pen(Brushes.RoyalBlue, 2)

mPen.EndCap = Drawing2D.LineCap.ArrowAnchor

gr = Graphics.FromImage(PictureBox1.Image)

Dim p1 As Point = New Point(startPIN.Location.X + (startPIN.Width / 2), _
                            startPIN.Location.Y + (startPIN.Height / 2)) 
'Since i want the line to start from the center i moved the coordinate to half of its height and width


Dim p2 As Point = New Point((endPIN.Location.X + (endPIN.Width / 2)), _
                                    (endPIN.Location.Y + (endPIN.Height / 2))) 'initially point to the center of the other pin



Dim pinRadius As Integer = endPIN.Width / 2

       
Dim m As Single = (p2.Y - p1.Y) / (p2.X - p1.X) 'slope of the line

         

p2 = New Point(p2.X + (pinRadius / Math.Sqrt(1 + (m ^ 2))), p2.Y + (pinRadius / Math.Sqrt(1 + (1 / (m ^ 2)))))


gr.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

Dim gpath As New Drawing2D.GraphicsPath
gpath.AddLine(p1, p2)



gr.DrawPath(mPen, gpath)
gr.FillPath(bbrush, gpath)

PictureBox1.Refresh()





我在谷歌上搜索但是找不到比如何使用线更多的东西和圆方程。我错过了什么或者我推断的交叉点公式是错误的吗?



这里是我想要的图片链接到目前为止我得到的http://tinypic.com/r/2pziudl/5 [ ^ ]

推荐答案

您正在绘制大图片框,它正确地执行,但是由于绘制顺序,绘制了大图片框,然后是较小的图片框,覆盖了大图片框中的图像。



透明度工作奇怪。 NET,其中子控件不只是获取控件下面的内容,它要求其下的控件重新绘制(它没有你的线),并将其用于顶部控件的背景。 br />


你需要做的是在绘制完所有控件之后在表单上绘制,或者在大图框上绘制透明控件并绘制在下面的控件被绘制之后。
You are drawing on the big picture box, which it does correctly, but due to draw order, the big picture box is drawn, then the smaller ones, covering up the image in the big picture box.

Transparency works oddly in .NET, where the child control doesn't just "get" what is underneath the control, it requests the control under it to repaint (which it does, without your line), and uses that for the background of the control on top.

What you need to do is to draw on top of the form after all the controls have been painted, or place a transparent control over the big picturebox and paint on that after the controls underneath have been painted.


这是关键: PictureBox 对你的情况没有帮助。这只是一个控件,严重过度使用。您应该使用方法 System.Windows.Forms.Control.OnPaint (或处理方法)立即渲染整个场景,而不是使用除代表整个场景的控件之外的任何控件。事件 System.Windows.Forms.Control.Paint )并且如果要移动/更改任何内容,则调用 Invalidate 。它还将解决透明度问题:您可以在GDI +对象级别使用它,但几乎无法控制。此外,要打击闪烁,请使用双缓冲。



请查看我过去的答案:



在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ];



mdi子窗体之间的绘制线 [ ^ ],

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

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

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



-SA
Here is the key: PictureBox is no help in your case. This is just a control, heavily overused. You rather should have rendered the whole scene at once, not using any controls except one representing the whole scene, using the method System.Windows.Forms.Control.OnPaint (or handling the event System.Windows.Forms.Control.Paint) and calling Invalidate if you want to move/change anything. It will also solve the problem of transparency: you can use it at the level of GDI+ object, but hardly controls. Also, to fight flicker, use double buffering.

Please see my past answers:

Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^];

Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

—SA


根据你的所有答案,我找到了自己的解决方案或一种新的编码方式。



答案真的清除了我应该使用的东西和我不应该的东西





再次感谢特别为 Sergey Alexandrovich Kryukov 带来的麻烦。这是你的答案链接和评论,它带来的亮点是图形化的东西只需要用最少的控件来处理,便于操作。



再次谢谢你
In light of all your answers i found my own solution or a new way of coding.

The answers really clears what i should use and what i shouldn't


Again thanks for the trouble specially to Sergey Alexandrovich Kryukov. It is your answer links and comments that brought light that graphical things should be handled only with minimal controls for easy handling.

Again thank you


这篇关于与箭头锚线指向包含圆问题的小图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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