how to draw arrow line using GDI? (to draw class association such as inheritance)

icy7th

Member
Joined
Oct 16, 2008
Messages
6
Programming Experience
1-3
I'm developing a class diagram editor now. but i'm stuck till now cause I can't find way to draw arrow using GDI.Help me... :(
and also how to draw line to separate attributes and methods in a class?
thx for the help!:D
 
Create a Pen and set its EndCap to be ArrowAnchor, drawing a line with this pen will produce an arrow. GDI+ Pens. Part 1.
 
Hello

You'll have to create a GraphicsPath and transform that one.

Bobby

@Robert_Zenz
I never used GraphicsPath before. Can you give me any example code? Thank's a lot. Sorry i I'm keeping on asking. I'm still at the early age to become .net programmer... :D
 
Create a Pen and set its EndCap to be ArrowAnchor, drawing a line with this pen will produce an arrow. GDI+ Pens. Part 1.

@JohnH
Thank's a lot. It is very helpfl. But now I need another kind of EndCap for line of implementation and aggregation. By the way, can I change the size of EndCap? It's very small till I can't see it clearly.
 
I'm facing another problem now (again). Haha. I need an event that enables me to select the line (class association) and then delete it. I've tried the advice given by Robert and John (thank's a lot for both of you!). This is what I write on my code. I create line first, then I draw it on form by cloning the bitmap object. But in this way, I can only delete the line in sequence order, from the last one. What I want t build is that user can select a line that has been drawed and then they can delete it. Help me.. (again).. Hahaha
 
Hello!

Of course I can give you an example of this.
This code was originally designed to be placed within a control...

VB.NET:
        'this will draw an simple arrow
        'center is the center of the Object (f.e. Me.Width/2)
        Dim path As New GraphicsPath()
        path.AddPolygon(New Point() {New Point(center, 0), _
                                     New Point(center * 0.9, Me.Height * 0.2), _
                                     New Point(center * 0.97, Me.Height * 0.15), _
                                     New Point(center, Me.Height / 2), _
                                     New Point(center * 1.03, Me.Height * 0.15), _
                                     New Point(center * 1.1, Me.Height * 0.2)} _
                        )

        'rotate it 90° clockwise using the center of the object
        Dim rot As New Matrix()
        rot.RotateAt(90, New PointF(Me.Width / 2, Me.Height / 2))
        path.Transform(rot)

        'draw it
        e.Grphics.FillPath(Brushes.Black, path)

If you want to select something you've drawn, then you'll have to process the Click-Event and interpret the Y-Position of the cursor. But I don't really understand what you mean with that you can only delete the last line. Keep the lines in a List or a Collection and you'll be able to delete any line in here.

Bobby
 
@JohnH
Thank's a lot. It is very helpfl. But now I need another kind of EndCap for line of implementation and aggregation. By the way, can I change the size of EndCap? It's very small till I can't see it clearly.
You can change the line width of the Pen to get a bolder line and larger end cap.
I'm facing another problem now (again). Haha. I need an event that enables me to select the line (class association) and then delete it. I've tried the advice given by Robert and John (thank's a lot for both of you!). This is what I write on my code. I create line first, then I draw it on form by cloning the bitmap object. But in this way, I can only delete the line in sequence order, from the last one. What I want t build is that user can select a line that has been drawed and then they can delete it. Help me.. (again).. Hahaha
You'd have to manage your point pairs and draw them dynamically with some Paint event, keeping a collection of GraphicsPath seems viable, you should also be able to hittest a MouseDown on each of these paths. Haha.
 
If you want to select something you've drawn, then you'll have to process the Click-Event and interpret the Y-Position of the cursor. But I don't really understand what you mean with that you can only delete the last line. Keep the lines in a List or a Collection and you'll be able to delete any line in here.

Bobby


Sorry. My English is not so good! :)
What I mean is that I need to place an event on line that I draw so I can select them and then delete. So far what I can do is only deleting the line sequencely from the last one to the first. By the way, may I know how do you keep the line you've drawed in the form so that it won't be gone after you make a new line. I find the solution but I'm, not sure. I draw it and then using MouseUp event I clone the line as the form background. Is it correct? Or maybe there's another solution? One more question. Is there any third arty component that can help me? Thank's alot Robert!!! :)
 
You can change the line width of the Pen to get a bolder line and larger end cap.

You'd have to manage your point pairs and draw them dynamically with some Paint event, keeping a collection of GraphicsPath seems viable, you should also be able to hittest a MouseDown on each of these paths. Haha.

I've tried it John but the size is not really proportional. By the way, may I know how do you place the line you've drawed so that it will stay in the form after you've draw new one? I clone it as the form background everytime I finish drawing but it makes everything difficult especially when I want to delete the line. Help me... :confused:
 
Back
Top