Pergunta

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,

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top