[英]Error with Pen C#
我做了一個使用鼠標選擇區域的系統。 但是,當我選擇該區域時:
對不起我的英文,我是巴西人......
我的代碼:
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);
}
}
}
我想知道是否有辦法解決這個問題或使其變得透明,只顯示矩形的邊緣。 謝謝,
你錯了使用Graphics
。
你永遠不應該調用CreateGraphics()
來繪制控件; 它將在下一個油漆上被抹去。
相反,您應該處理Paint
事件並在每次重繪時繪制所需的所有內容。
當鼠標移動時,調用Invalidate()
以強制重繪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.