C# C笔错误#

C# C笔错误#,c#,pen,C#,Pen,我用鼠标做了一个选择区域的系统。 但是,当我选择该区域时,该区域如下所示: 对不起,我的英语,我是巴西人 我的代码: private void ResizeSelection() { if (CurrentAction == ClickAction.LeftSizing) { if (Cursor.Position.X < CurrentBottomRight.X - 10) {

我用鼠标做了一个选择区域的系统。 但是,当我选择该区域时,该区域如下所示:

对不起,我的英语,我是巴西人

我的代码:

    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);

            }

        }

    }
private void ResizeSelection()
{
如果(CurrentAction==ClickAction.LeftSize)
{
if(光标位置XCurrentTopLeft.Y+10)
{
//删除上一个矩形
g、 DrawRectangle(橡皮擦、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
CurrentTopLeft.X=Cursor.Position.X;
CurrentBottomRight.Y=Cursor.Position.Y;
矩形宽度=CurrentBottomRight.X-CurrentTopLeft.X;
矩形高度=CurrentBottomRight.Y-CurrentTopLeft.Y;
g、 DrawRectangle(MyPen、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
}
}
如果(CurrentAction==ClickAction.RightSize)
{
if(Cursor.Position.X>CurrentTopLeft.X+10)
{
//删除上一个矩形
g、 DrawRectangle(橡皮擦、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
CurrentBottomRight.X=Cursor.Position.X;
矩形宽度=CurrentBottomRight.X-CurrentTopLeft.X;
g、 DrawRectangle(MyPen、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
}
}
如果(CurrentAction==ClickAction.TopRightSize)
{
if(Cursor.Position.X>CurrentTopLeft.X+10&&Cursor.Position.YCurrentTopLeft.X+10&&Cursor.Position.Y>CurrentTopLeft.Y+10)
{
//删除上一个矩形
g、 DrawRectangle(橡皮擦、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
CurrentBottomRight.X=Cursor.Position.X;
CurrentBottomRight.Y=Cursor.Position.Y;
矩形宽度=CurrentBottomRight.X-CurrentTopLeft.X;
矩形高度=CurrentBottomRight.Y-CurrentTopLeft.Y;
g、 DrawRectangle(MyPen、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
}
}
如果(CurrentAction==ClickAction.TopSizing)
{
if(光标位置YCurrentTopLeft.Y+10)
{
//删除上一个矩形
g、 DrawRectangle(橡皮擦、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
CurrentBottomRight.Y=Cursor.Position.Y;
矩形高度=CurrentBottomRight.Y-CurrentTopLeft.Y;
g、 DrawRectangle(MyPen、CurrentTopLeft.X、CurrentTopLeft.Y、矩形宽度、矩形高度);
}
}
}
我想知道是否有办法解决这个问题,或者让它变得透明,只显示矩形的边缘。
谢谢,

您误用了
图形

决不能调用
CreateGraphics()
在控件上绘制;它将在下一道漆上擦除

相反,您应该处理
Paint
事件,并在每次重新绘制时绘制所需的所有内容

当鼠标移动时,调用
Invalidate()