0

A simple code for drawing a line.

using (Graphics g = this.CreateGraphics())
        {
            g.DrawLine(Pens.Black, new Point(50, 50), new Point(100, 100));
        }

This will draw a typical line. However i want to this line to be selectable so that user can manipulate it further(streching, resizing etc) at run time. Initially i attempted to use controls that can be manipulated at runtime with the line as background however that could not work due to overlapping controls issue.

My question is how can i select this line at runtime ?

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • What controls did you try to use for manipulating the line? – called2voyage Jun 27 '13 at 13:42
  • This question is very general and there is no precise answer to it. You should take a look at graphics manipulation. I think you will have to come up with a unique solution to your problem. – Romano Zumbé Jun 27 '13 at 13:42
  • You also can draw a flat polygon , instead a line, then the graphics manipulation options could allow You to "access the line" when Your mouse click resides "inside the flat polygon". There are some functions like these. – icbytes Jun 27 '13 at 13:44
  • @called2voyage picturebox, i was able to make it re sizable by the user at runtime but the results were not satisfactory – Win Coder Jun 27 '13 at 13:44
  • @RomanoZumbé i was hoping not to implement a custom solution but it seems i may have to thanks anyway. – Win Coder Jun 27 '13 at 13:45
  • As an aside, calling CreateGraphics like that is poor practice. See this for more information: http://bobpowell.net/creategraphics.htm – Chris Dunaway Jun 27 '13 at 15:00
  • @ChrisDunaway didn't knew that, was following the method used in Head first C# – Win Coder Jun 27 '13 at 15:08

4 Answers4

2

You need to write it yourself.

  1. Write a class that wraps Line
  2. Add all needed additional behavior to the class (what happens when the line is selected, what happens when the line is stretched, deleted, changed color ...)
  3. Write a class that manages either object was was picked by mouse or not (RayTracer)

etc...

Or simply use: piccolo2d framework

Structured 2D Graphics Framework

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • i am sorry if this question is stupid but i am really new to this and i didn't exactly follow "class that wraps a line". How can a class wrap a line ? – Win Coder Jun 27 '13 at 13:56
  • @WinCoder: the class that has Draw methd that draws the line on Graphics, remove method, Stratch, GetLength and so on... May be it's better you look on Piccolo2D framework, how it manages lines. – Tigran Jun 27 '13 at 14:26
0

I think what you're looking to do is make the Graphics object to be selectable? If so, you could put your logic into the MouseOver and MouseButton events. Check this out, it may give you some insight.

Selectable Graphics Object

Community
  • 1
  • 1
Th3BFG
  • 305
  • 1
  • 12
0

You must create it yourself. Declare interfaces that your graphics objects implement. Suggestion:

public interface IObject
{
    bool HitTest(Point mouseLocation);
    void Paint(Graphics g);
    List<IAdorner> Adorners { get; }
}

public interface IAdorner
{
    bool HitTest(Point mouseLocation);
    void Paint(Graphics g);

    void StartMoving(Point mouseLocation);
    void Move(Point mouseLocation);
}

The adorners are the selectable end points of a line object for instance.

Your main paint routine will look something like this:

private void drawingSurface_Paintobject sender, PaintEventArgs e)
{
    foreach (IObject o in _objects) {
        o.Paint(e.Graphics);
        if (o == _selectedObject) {
            foreach (IAdorner a in o.Adorners) {
                a.Paint(e.Graphics);
            }
        }
    }
}

And of cause you need all the mouse event handling.

These interfaces are abstract enough in order to allow the implementation of any shapes. For instance they don't include any coordinates, as different types of objects need different numbers and kinds of coordinates and parameters.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • That's all well and good to define an interface, but the hard part is figuring out how `HitTest` works. – Gabe Jun 27 '13 at 14:53
0

Maybe It's a little too late, since you already accepted an answer, but you should really consider WPF for this.

https://stackoverflow.com/a/15469477/643085

That's an exact sample of what you're after, in WPF + MVVM. With a real whole LOT of advantages over any winforms approach (such as NO flicker due to hardware acceleration).

Please at least give it a try. Full CSProj project Source available.

You're REALLY reiventing the wheel implementing all this yourself in GDI. And in the end it will be unusable due to flickering and stuff like that.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • many thanks for your suggestion friend its never too late :) . I hear WPF has a steep learning curve and i want to be done with this application of mine quickly. You are right there would be flickering but first i must done with this project. One thing i don't understand though is people keep say saying that i am reinventing the wheel, i don't understand how i mean its not like this selectable lines are built in winforms – Win Coder Jun 29 '13 at 05:36
  • @WinCoder What you're saying sounds contradictory to me. WPF does have a steep learning curve, but if you want to finish your project quickly just grab my code and modify it to meet your needs. winforms will not help you finish this quickly, because the infrastructure you need is not supported by it. – Federico Berasategui Jun 29 '13 at 06:08
  • @WinCoder I'm saying you reinvent the wheel because you're basically trying to build features that WPF already has, but winforms has not. It doesn't make sense, Microsoft already did that for you. And I'm sure they can do it better than any of us. – Federico Berasategui Jun 29 '13 at 06:11
  • @WinCoder Just looking at that post I realized I had not posted the full code anywhere, so [here it is](https://anonfiles.com/file/b2a1eb2fcc1fa2bf053ac4a94813fc60). You may also want to look at my other similar samples [1](http://stackoverflow.com/a/15580293/643085) [2](http://stackoverflow.com/a/15821573/643085) [3](http://stackoverflow.com/a/16947081/643085) – Federico Berasategui Jun 29 '13 at 07:09
  • Indeed you are right i do sound contradictory :) .I was trying to say that i want the project to end quickly and if i start learning WPF it would take more time because first i would have to get started with WPF(but its my personal project not with a deadline)and because i am learning i would also prefer to use my own code. To be exact i am trying to make a clone of UMLet [link](http://www.umlet.com/) – Win Coder Jun 29 '13 at 08:53
  • @wincoder if you're doing this just to learn, then forget winforms. it's dead, obsolete, useless. It makes no sense learning winforms in 2013, it's legacy. Learn the current stuff, not some random dinosaur framework from 20 years ago. – Federico Berasategui Jun 29 '13 at 09:15
  • @wincoder if you learn winforms, you're stuck with winforms and it's horrible hacks forever. If you learn XAML and MVVM (which is what you learn when you learn WPF), then you can port your knowledge to all of the other XAML-based technologies (WinRT, Silverlight, Windows Phone) – Federico Berasategui Jun 29 '13 at 09:17
  • @Wincoder by the way, that UML tool screenshot looks really bad. It says "eclipse" everywhere so I assume it's java-based, therefore not only the frontend is horrible but the code itself too. I suggest you start looking at [Sacha's Diagram Designer](http://www.codeproject.com/Articles/484616/MVVM-Diagram-Designer), which is WPF and MVVM based. – Federico Berasategui Jun 29 '13 at 09:24
  • nahh it isn't just eclipse based i use its executable version on windows. I have used visio,edraw,argo and other UML tools, and UMLet is by far the most lightest, easy to use tool. Don't go with screenshots download the tool(it will only take a moment) and use it. Anyways i am having real trouble so far working with winforms for my special requirements. Maybe i'll start looking for alternative technology or maybe i'll just give up the whole project. – Win Coder Jun 29 '13 at 10:24
  • @WinCoder NEVER give up. If you look at my samples plus Sacha's work I linked, most of the work is already done. I implemented basic diagram functionality, support for advanced UI elements inside the nodes, zooming, panning, drag and drop, just look at my samples 1 2 and 3 from the previous comment. It's just a matter of putting all pieces together, all while you learn to use current technology. winforms will only give you pain, while WPF will give you results. – Federico Berasategui Jun 29 '13 at 14:40
  • appreciate your encouragement :) – Win Coder Jun 29 '13 at 15:42