0

I did a system to select an area using the mouse. However, when I select the area looks like:

http://i.imgur.com/xxc0ayn.png

Sorry my english, i'm brazilian...

My Code:

    private void ResizeSelection()
    {

        if (CurrentAction == ClickAction.LeftSizing)
        {

            if (Cursor.Position.X < CurrentBottomRight.X - 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentTopLeft.X = Cursor.Position.X;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }

        }
        if (CurrentAction == ClickAction.TopLeftSizing)
        {

            if (Cursor.Position.X < CurrentBottomRight.X - 10 && Cursor.Position.Y < CurrentBottomRight.Y - 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentTopLeft.X = Cursor.Position.X;
                CurrentTopLeft.Y = Cursor.Position.Y;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }
        }
        if (CurrentAction == ClickAction.BottomLeftSizing)
        {

            if (Cursor.Position.X < CurrentBottomRight.X - 10 && Cursor.Position.Y > CurrentTopLeft.Y + 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentTopLeft.X = Cursor.Position.X;
                CurrentBottomRight.Y = Cursor.Position.Y;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }

        }
        if (CurrentAction == ClickAction.RightSizing)
        {

            if (Cursor.Position.X > CurrentTopLeft.X + 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentBottomRight.X = Cursor.Position.X;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }
        }
        if (CurrentAction == ClickAction.TopRightSizing)
        {

            if (Cursor.Position.X > CurrentTopLeft.X + 10 && Cursor.Position.Y < CurrentBottomRight.Y - 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentBottomRight.X = Cursor.Position.X;
                CurrentTopLeft.Y = Cursor.Position.Y;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }
        }
        if (CurrentAction == ClickAction.BottomRightSizing)
        {

            if (Cursor.Position.X > CurrentTopLeft.X + 10 && Cursor.Position.Y > CurrentTopLeft.Y + 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentBottomRight.X = Cursor.Position.X;
                CurrentBottomRight.Y = Cursor.Position.Y;
                RectangleWidth = CurrentBottomRight.X - CurrentTopLeft.X;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }
        }
        if (CurrentAction == ClickAction.TopSizing)
        {

            if (Cursor.Position.Y < CurrentBottomRight.Y - 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentTopLeft.Y = Cursor.Position.Y;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }
        }
        if (CurrentAction == ClickAction.BottomSizing)
        {

            if (Cursor.Position.Y > CurrentTopLeft.Y + 10)
            {

                //Erase the previous rectangle
                g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);
                CurrentBottomRight.Y = Cursor.Position.Y;
                RectangleHeight = CurrentBottomRight.Y - CurrentTopLeft.Y;
                g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, RectangleWidth, RectangleHeight);

            }

        }

    }

I wonder if there is a way to fix this or make it become transparent showing only the edge of the rectangle. Thanks,

gwin003
  • 7,432
  • 5
  • 38
  • 59

1 Answers1

2

You're mis-using Graphics.

You should never call CreateGraphics() to draw on a control; it will be erased on the next paint.

Instead, you should handle the Paint event and draw everything you need on every repaint.

When the mouse moves, call Invalidate() to force it to repaint.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thus delete the rectangle when I re-select, it should remain and increase necessary to and not be recreated. – Marlon Roberto Colhado Jun 07 '13 at 21:09
  • @MarlonRobertoColhado: You need to redraw _everything_ on each `Paint` event. Store a list of things to draw in a `List`. If you don't do this, things will vanish randomly. – SLaks Jun 07 '13 at 21:10
  • I took a modified in eraser, got this: http://i.imgur.com/tQWY3xT.png However, some things are missed, letters inside the rectangle, it would make the eraser to create a color with transparency after the red pen? or opacity in eraser.. – Marlon Roberto Colhado Jun 07 '13 at 21:15
  • @MarlonRobertoColhado: Yes; that's because you're still doing it wrong. **You need to move all of this to a `Paint` handler.** – SLaks Jun 07 '13 at 21:28
  • I do not know how I can do this. :/ – Marlon Roberto Colhado Jun 07 '13 at 21:34
  • I'll send you all my code to better understand what is happening: https://dl.dropboxusercontent.com/u/68327673/RemotePrintUpload.rar – Marlon Roberto Colhado Jun 07 '13 at 21:39
  • @MarlonRobertoColhado: What don't you understand? – SLaks Jun 07 '13 at 21:54
  • Got it, changed the TransparentKey to: 255, 255, 192 And PenEraser to: Pen EraserPen = new Pen(Color.FromArgb(255, 255, 192), 3.0F); Result: http://i.imgur.com/QNQZqQ3.png (Thanks anyway.) :D – Marlon Roberto Colhado Jun 07 '13 at 21:56