Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,613 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 957 people online right now. Registration is fast and FREE... Join Now!




Dragable rectangles

 
Reply to this topicStart new topic

Dragable rectangles

Dumpen
post 14 Jul, 2008 - 11:23 AM
Post #1


New D.I.C Head

*
Joined: 21 Mar, 2008
Posts: 40

Hello.

I have a question..

I have this script:
CODE
        int mouseDownX;
        int mouseDownY;

        int width;
        int height;

        private void Form1_MouseUp(object sender, MouseEventArgs mouseEv)
        {
            width = mouseEv.X - mouseDownX;
            height = mouseEv.Y - mouseDownY;

            textBox2.Text = Convert.ToString(mouseEv.Location);

            Graphics graphics = this.CreateGraphics();

            Rectangle rectangle = new Rectangle(
                    mouseDownX, mouseDownY, width, height);

            graphics.DrawRectangle(Pens.Red, rectangle);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs mouseEv)
        {
            textBox1.Text = Convert.ToString(mouseEv.Location);

            mouseDownX = mouseEv.X;
            mouseDownY = mouseEv.Y;
        }


Which works fine if I make a rectangle from the left top corner to the right bottom corner

But is there any easier way of just making rectangles?

Here is a video of what I want:
http://www.youtube.com/watch?v=V5YO4U1UnO0

And here is a download link to my project:
http://peecee.dk/upload/download/123329

So I want to be able to draw rectangles from any angle if you understand?
User is offlineProfile CardPM

Go to the top of the page

allensmith
post 14 Jul, 2008 - 12:12 PM
Post #2


New D.I.C Head

*
Joined: 4 Jun, 2008
Posts: 11



Thanked 3 times
My Contributions



Hello,

I replied to your query in another forum. Just for reference to this forum, I will reply again.

In your code sample, the problem was with the calculation of height and width of the rectangle.
When you move the mouse from Top Left to Right Bottom, the values are calculated correctly.
However, while going in the opposite version, either height or Width or even both gets calculated
in negative values. As a result, no rectangle is drawn. Try the given code in MouseUp() event.

BEGIN CDOE

private void Form1_MouseUp(object sender, MouseEventArgs mouseEv)
{

textBox2.Text = Convert.ToString(mouseEv.Location);

Graphics graphics = this.CreateGraphics();
Rectangle rectangle;

if (mouseEv.Y > mouseDownY)
{
height = mouseEv.Y - mouseDownY;
}
else
height = mouseDownY - mouseEv.Y;

if (mouseEv.X < mouseDownX)
{
width = mouseDownX - mouseEv.X;

rectangle = new Rectangle(mouseEv.X, mouseEv.Y, width, height);
}
else
{
width = mouseEv.X - mouseDownX;
rectangle = new Rectangle(mouseDownX, mouseDownY, width, height);
}

graphics.DrawRectangle(Pens.Red, rectangle);

}

END CODE

I hope this will help.

Regards,
User is offlineProfile CardPM

Go to the top of the page

Dumpen
post 15 Jul, 2008 - 04:59 AM
Post #3


New D.I.C Head

*
Joined: 21 Mar, 2008
Posts: 40

Hey

I have this project now:
http://peecee.dk/upload/download/123403

It allmost works, the preview thing just doesnt work properly

Could you guys take a look at it?

Because I cant seem to find a soloution
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:47AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month