Hi,

I would like to write a program that enable user to draw geometric shapes like circles, triangles, rectangles and so on.
I would like also to be able to drag and drop or resize a shape that was previously drawn.

1) I thought to draw the shapes inside a Panel. Does it seems reasonable ?
2) Say I drew several shapes. Then I want to resize the first drawn shape. How can I identify that the first shape was clicked ?

Thanks !

Recommended Answers

All 11 Replies

Hi dude,

Actually my answer might not answer your question directly, but I found an article with source code written in WPF which draws shapes and select each shape separately, you might read the code to know the main concepts!!!!

here's the download link of source code http://rapidshare.com/files/327901747/SingleChangeObjectApproch.zip.html

and the article is at http://www.codeproject.com/KB/architecture/UndoRedoPart1.aspx

I hope this benefits you, happy coding :)

[EL-Prince]

well u can study about paint method......
Also the link provided by MxDev are also gud.........

Hi,
Thank you all for the given links, but all I want is to understand the idea rather than reading a lot of code.
I do have some experience with drawing graphics in GDI+.
At this point I just want to understand the idea of resizing/moving a circle for example.
After I draw a circle, it becomes a part of a Bitmap. Of course, I do save circle's details in some other object.
But what I don't understand is how to implement the following:
when the mouse is over the circle, the circle is chosen, and then using some key enables the user to resize/move it.
How can I know that the mouse is over the circle ?
Do I need to check the mouse coordinates vs all circle pixel coordinates ?
I'm looking for simpler solution....

To check whether the mouse is over the circle's rectangle or not, use this code:

Rectangle rect = new Rectangle(new Point(30, 30), new Size(100, 100));

        private CreateRectangle()
        {
            Graphics g;
            g = this.CreateGraphics();
            g.DrawRectangle(new Pen(Color.Red), rect);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            CreateRectangle();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X >= rect.X && e.Y >= rect.Y && e.X <= rect.Width+rect.X && e.Y <= rect.Height+rect.Y)
                MessageBox.Show("Cursor is over the rectangle!");
        }

This code inside Mouse_Move method first makes sure that the mouse X/Y is greater than that of the rectangle. Then it checks if the mouse X/Y is smaller then rectangle's right/bottom.

I am sure you'll understand the code easily since you said you are familiar with drawing graphics.

Thanks

Thanks !
Of course, I do understand the code easily :)
For me, there are 2 problems with your code:
1) I would like to know whether the mouse is on rectangle's edge rather than inside the rectangle.
2) You do not take into account the pen width of the rectangle.
Can you suggest an improvement ?

Sorry, I forgot about pen width. It works for both, whether the mouse is on the edge or while inside the rectangle.

Here's the improved version:

I've used 10f for the penWidth just for testing. It works with any width.

Rectangle rect = new Rectangle(new Point(30, 30), new Size(100, 100));
float penWidth = 10.0f;

        private CreateRectangle()
        {
            Graphics g;
            g = this.CreateGraphics();
            g.DrawRectangle(new Pen(Color.Red, penWidth), rect);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            CreateRectangle();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            int intPenWidth = int.Parse(penWidth.ToString()) / 2;

            if (e.X >= rect.X - intPenWidth && e.Y >= rect.Y - intPenWidth&& e.X <= rect.Right + penWidth/2 && e.Y <= rect.Bottom + penWidth/2)
                MessageBox.Show("its on the rect!");
        }

Thanks

Thanks !
In your code the mouse is over the rectangle event when the mouse inside the rectangle, which is not what I want. I want it only on the edge.
Well, I got your idea, and I'm wondering whether all these manual calculations are the best approach...

That's not the best approach but its also not a bad approach. It does what you want.

an idea would be to create a list of shapeobjects, and in the paint event loop through them and paint them to the panel, to determine if you are on the line of the rectangle create a smaller and larger rectangle, check if you are inside the larger but not inside the smaller one. this seems to work great. I used it to draw resizable circles on a picturebox for use in selecting eyes in a redeye removal feature.

The Idea would be not to raster them to the image until you were sure you were done moving them, unless you were interested in creating layers, which would be a different story entirely. Things can get complicated quick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.