// 以下為 MyRectangle 類別的核心方法實作片段
public int LittleRectangleWidth { get; set; }
public int LittleRectangleHeight { get; set; }
public Rectangle LeftTopRectangle { get; set; }
public Rectangle LeftMiddleRectangle { get; set; }
public Rectangle LeftBottomRectangle { get; set; }
public Rectangle RightTopRectangle { get; set; }
public Rectangle RightMiddleRectangle { get; set; }
public Rectangle RightBottomRectangle { get; set; }
public Rectangle TopMiddleRectangle { get; set; }
public Rectangle BottomMiddleRectangle { get; set; }
public Rectangle Rect
{
get { return rect; }
set { rect = value; x = value.X; y = value.Y; width = value.Width; height = value.Height; }
}
public Size Size { get { return rect.Size; } }
public Image BackImage { get; set; }
public Cursor MyCursor { get; set; }
public MyRectangle()
{
Rect = new Rectangle();
setAllModeFalse();
LittleRectangleWidth = 4;
LittleRectangleHeight = 4;
MinHeight = 5;
MinWidth = 5;
LeftTopRectangle = new Rectangle();
LeftMiddleRectangle = new Rectangle();
LeftBottomRectangle = new Rectangle();
RightTopRectangle = new Rectangle();
RightMiddleRectangle = new Rectangle();
RightBottomRectangle = new Rectangle();
TopMiddleRectangle = new Rectangle();
BottomMiddleRectangle = new Rectangle();
MyCursor = new Cursor(@"..\..\Cursors\hcross.cur");
}
public void SetLittleRectangle()
{
int excursionX = LittleRectangleWidth / 2;
int excursionY = LittleRectangleHeight / 2;
LeftTopRectangle = new Rectangle(X - excursionX, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
LeftMiddleRectangle = new Rectangle(X - excursionX, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
LeftBottomRectangle = new Rectangle(X - excursionX, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
RightTopRectangle = new Rectangle(X - excursionX + Width, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
RightMiddleRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
RightBottomRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
TopMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
BottomMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
}
public void Draw(MouseEventArgs e, Color backColor)
{
Draw(backColor);
if (e.X < DownPointX) { Width = DownPointX - e.X; X = e.X; }
else { Width = e.X - DownPointX; }
if (e.Y < DownPointY) { Height = DownPointY - e.Y; Y = e.Y; }
else { Height = e.Y - DownPointY; }
Draw(backColor);
}
public void Draw(Color backColor)
{
SetLittleRectangle();
ControlPaint.DrawReversibleFrame(rect, backColor, FrameStyle.Dashed);
ControlPaint.FillReversibleRectangle(LeftTopRectangle, Color.White);
ControlPaint.FillReversibleRectangle(LeftMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(LeftBottomRectangle, Color.White);
ControlPaint.FillReversibleRectangle(RightTopRectangle, Color.White);
ControlPaint.FillReversibleRectangle(RightMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(RightBottomRectangle, Color.White);
ControlPaint.FillReversibleRectangle(TopMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(BottomMiddleRectangle, Color.White);
}
public void ChangeSize(MouseEventArgs e)
{
if (!ChangeSizeMode) return;
// 依滑鼠位置調整各邊/角,並檢查最小尺寸限制
// 程式碼略……
}
public void Move(int newX, int newY) { X = newX; Y = newY; }
public void CheckMouseLocation(MouseEventArgs e)
{
// 依滑鼠座標判斷落在哪個控制點或內部,設定游標與模式旗標
// 程式碼略……
}
public void setAllModeFalse()
{
// 將所有模式旗標設為 false,游標重設為十字
}
public bool onChangingMode()
{
return ((MouseOnLeftTop || MouseOnLeftMiddle || MouseOnLeftBottom || MouseOnRightTop || MouseOnRightMiddle || MouseOnRightBottom || MouseOnTopMiddle || MouseOnBottomMiddle) && changeSizeMode);
}
public void Initialize(int x, int y, int width, int height)
{
X = x; Y = y; Width = width; Height = height;
SetLittleRectangle();
}