Pen C#出错

时间:2013-06-07 20:49:57

标签: c# pen

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

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

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

我的代码:

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

            }

        }

    }

我想知道是否有办法解决这个问题或让它变得透明,只显示矩形的边缘。 谢谢,

1 个答案:

答案 0 :(得分:2)

你误用了Graphics

你永远不应该叫CreateGraphics()画一个控件;它将在下一个油漆上被删除。

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

当鼠标移动时,请调用Invalidate()强制重新绘制。