Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In wpf have a control called Line, Line line = new Line(); ...
Posted
Comments
Kenneth Haugland 14-Oct-13 11:47am    
http://msdn.microsoft.com/en-us/library/cyh3c8h8.aspx
Rieth L 14-Oct-13 11:53am    
It's different, because i can't assign an event on it for example, i need a line that can raise events when i click on it.
Sergey Alexandrovich Kryukov 14-Oct-13 11:54am    
You should have include this problem in your question. I already answered, and now will have to add on handling of such events.
—SA
Sergey Alexandrovich Kryukov 14-Oct-13 12:03pm    
So, please see my answer now. I added explanation of the click handling after [EDIT].
—SA

1 solution

There are no those graphical primitives as objects in System.Windows.Forms. You can create them based on the graphic rendering methods provided by the class System.Drawing.Graphics. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

You can render graphics using this class on any Control, including Form. You need to override the virtual method System.Windows.Forms.Control.OnPaint or handle the event System.Windows.Forms.Control.Paint and use the instance of System.Drawing.Graphics passed to this method or to your event handler as a parameter:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx[^].

Please see my past answers for further advice:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

See also this one, explaining how it's mapped to raw Windows API: Zoom image in C# .net mouse wheel[^],
MFC Drawed lines is disappeared[^].

[EDIT]
Member 10267665 wrote:
It's different, because i can't assign an event on it for example, i need a line that can raise events when i click on it.
You have to handle click and other input events on the control where you render you lines or other elements. This is the control on which you handle the event Paint or override the method OnPaint.

In case of click handling, you will need to override the virtual method System.Windows.Forms.Control.OnClick or handle the event System.Windows.Forms.Control.Click:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx[^].

But then, the hard part starts: you would need to calculate some acceptable vicinity of the line points which you would use as a "hot area" where the click is associated with your line object. In you handler of the click, you will have to determine if the click coordinates hit the area of this vicinity (so called "hit test") and, depending on that criterion, proceed with line-related handling, or handle some other graphical object, or ignore the event. Some mathematics is involved.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900