自定义ui,form基类
私有成员
#region[===== 私有成员 =====]
private bool _showFormBorder = true;
private bool _showTitle = true;
private bool _controlBox = true;
private bool _maximizeBox = true;
private bool _minimizeBox = true;
private bool _extendBox = false;
private bool _showTitleIcon = false;
private bool _inControlBox = false;
private bool _inMaxBox = false;
private bool _inMinBox = false;
private bool _inExtendBox = false;
private bool _formMoveMouseDown = false;
private bool _isFullScreen = false;
private int _titleHeight = 36;
private readonly int _controlboxSide = 28;
private int _controlBoxLeft = 0;
private Color _formBorderColor = Color.Black;
private Point _formLocation;
private Point _mouseLocation;
private Size _beforeMaximizeSize;
private Point _beforeMaximizeLocation;
private Color _titleBackColor = Color.FromArgb(100, 204, 255);
private Color _titleForeColor = Color.White;
private Color _titleBorderColor = Color.FromArgb(38, 38, 38);
private Color _controlBoxFillHoverColor = Color.FromArgb(155, 121, 121, 121);
private Color _closeBoxFillHoverColor = Color.Red;
private Font _titleFont = new Font("微软雅黑", 10);
private StringAlignment _textAlignment = StringAlignment.Near;
private Color _controlBoxForeColor = Color.White;
private Rectangle _controlBoxRect;
private Rectangle _maximizeBoxRect;
private Rectangle _minimizeBoxRect;
private Rectangle _extendBoxRect;
#endregion[===== 私有成员 =====]
重定义属性
#region[===== 重定义属性 =====]
[Browsable(false)]
public new bool IsMdiContainer
{
get => base.IsMdiContainer;
set => base.IsMdiContainer = false;
}
[Browsable(false)]
public new bool AutoScroll
{
get => base.AutoScroll;
set => base.AutoScroll = false;
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new FormBorderStyle FormBorderStyle
{
get => base.FormBorderStyle;
set
{
if (!Enum.IsDefined(typeof(FormBorderStyle), value))
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(FormBorderStyle));
base.FormBorderStyle = FormBorderStyle.None;
}
}
[Description("是否显示控制框按钮"), Category("WindowStyle")]
[DefaultValue(true)]
public new bool ControlBox
{
get => this._controlBox;
set
{
this._controlBox = value;
if (!value) this._maximizeBox = this._minimizeBox = false;
CalcSystemBoxPosition();
Invalidate();
}
}
[DefaultValue(true)]
[Description("是否显示窗体的最大化按钮"), Category("WindowStyle")]
public new bool MaximizeBox
{
get => this._maximizeBox;
set
{
this._maximizeBox = value;
if (value) this._minimizeBox = true;
CalcSystemBoxPosition();
Invalidate();
}
}
[DefaultValue(true)]
[Description("是否显示窗体的最小化按钮"), Category("WindowStyle")]
public new bool MinimizeBox
{
get => this._minimizeBox;
set
{
this._minimizeBox = value;
if (!value) this._maximizeBox = false;
CalcSystemBoxPosition();
Invalidate();
}
}
#endregion[===== 重定义属性 =====]
自定义属性
#region[===== 自定义属性 =====]
[DefaultValue(true)]
[Description("是否显示窗体边框"), Category("WindowStyle")]
public bool ShowFormBorder
{
get => this._showFormBorder;
set
{
this._showFormBorder = value;
Invalidate();
}
}
[DefaultValue(true)]
[Description("是否显示标题栏"), Category("WindowStyle")]
public bool ShowTitle
{
get => this._showTitle;
set
{
this._showTitle = value;
Padding = new Padding(Padding.Left, value ? this._titleHeight : 0, Padding.Right, Padding.Bottom);
Invalidate();
}
}
[DefaultValue(36)]
[Description("标题栏高度"), Category("LemonUI")]
public int TitleHeight
{
get => this._titleHeight;
set
{
this._titleHeight = Math.Max(value, this._controlboxSide + 2);
Padding = new Padding(Padding.Left, this._showTitle ? this._titleHeight : Padding.Top, Padding.Right, Padding.Bottom);
CalcSystemBoxPosition();
Invalidate();
}
}
[Description("窗体边框颜色"), Category("LemonUI")]
public Color FormBorderColor
{
get => this._formBorderColor;
set
{
this._formBorderColor = value;
Invalidate();
}
}
[DefaultValue(typeof(Color), "153, 204, 255")]
[Description("标题栏背景颜色"), Category("LemonUI")]
public Color TitleBackColor
{
get => this._titleBackColor;
set
{
if (this._titleBackColor != value)
{
this._titleBackColor = value;
Invalidate();
}
}
}
[DefaultValue(typeof(Color), "White")]
[Description("标题栏前景颜色"), Category("LemonUI")]
public Color TitleForeColor
{
get => this._titleForeColor;
set
{
if (this._titleForeColor != value)
{
this._titleForeColor = value;
Invalidate();
}
}
}
[DefaultValue(typeof(Color), "White")]
[Description("标题栏边框颜色"), Category("LemonUI")]
public Color TitleBorderColor
{
get => this._titleBorderColor;
set
{
if (this._titleBorderColor != value)
{
this._titleBorderColor = value;
Invalidate();
}
}
}
[DefaultValue(typeof(Color), "(120, 225, 225, 225)")]
[Description("鼠标移动到矩形控制框上的高亮颜色"), Category("WindowStyle")]
public Color ControlBoxFillHoverColor
{
get => this._controlBoxFillHoverColor;
set => this._controlBoxFillHoverColor = value;
}
[DefaultValue(typeof(Color), "Red")]
[Description("鼠标移动到关闭按钮上的高亮颜色"), Category("WindowStyle")]
public Color CloseBoxFillHoverColor
{
get => this._closeBoxFillHoverColor;
set => this._closeBoxFillHoverColor = value;
}
[DefaultValue(typeof(Font), "微软雅黑, 10")]
[Description("标题字体"), Category("LemonUI")]
public Font TitleFont
{
get => this._titleFont;
set
{
_titleFont = value;
Invalidate();
}
}
[Description("标题文字对齐方式"), Category("LemonUI")]
public StringAlignment TextAlignment
{
get => this._textAlignment;
set
{
this._textAlignment = value;
Invalidate();
}
}
[DefaultValue(typeof(Color), "White")]
[Description("控制框前景颜色"), Category("LemonUI")]
public Color ControlBoxForeColor
{
get => this._controlBoxForeColor;
set
{
if (this.ControlBoxForeColor != value)
{
this._controlBoxForeColor = value;
Invalidate();
}
}
}
[DefaultValue(false)]
[Description("显示扩展按钮"), Category("LemonUI")]
public bool ExtendBox
{
get => this._extendBox;
set
{
this._extendBox = value;
CalcSystemBoxPosition();
Invalidate();
}
}
[DefaultValue(false)]
[Description("显示标题栏图标"), Category("WindowStyle")]
public bool ShowTitleIcon
{
get => this._showTitleIcon;
set
{
this._showTitleIcon = value;
Invalidate();
}
}
[Description("最大化是否铺满屏幕"), Category("WindowStyle")]
public bool IsFullScreen
{
get => this._isFullScreen;
set
{
this._isFullScreen = value;
base.MaximumSize = this._isFullScreen ? Screen.PrimaryScreen.Bounds.Size : Screen.PrimaryScreen.WorkingArea.Size;
}
}
#endregion[===== 自定义属性 =====]
私有函数
#region[===== 私有函数 =====]
private void CalcSystemBoxPosition()
{
this._controlBoxLeft = this.Width;
if (this._controlBox)
{
_controlBoxRect = new Rectangle(this.Width - 6 - this._controlboxSide, (this._titleHeight - this._controlboxSide) / 2, this._controlboxSide, this._controlboxSide);
if (this._maximizeBox)
{
this._maximizeBoxRect = new Rectangle(this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
this._controlBoxLeft = this._maximizeBoxRect.Left;
}
else
{
this._maximizeBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
}
if (this._minimizeBox)
{
this._minimizeBoxRect = new Rectangle(this._maximizeBox ? this._maximizeBoxRect.Left - this._controlboxSide - 2 : this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
this._controlBoxLeft = this._minimizeBoxRect.Left;
}
else
{
this._minimizeBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
}
if (this._extendBox)
{
if (this._minimizeBox)
{
this._extendBoxRect = new Rectangle(this._minimizeBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
this._controlBoxLeft = this._extendBoxRect.Left;
}
else
{
this._extendBoxRect = new Rectangle(this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
}
}
}
else
{
this._extendBoxRect = this._maximizeBoxRect = this._minimizeBoxRect = this._controlBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
}
}
private void ShowMaximizeForm(bool isOnMoving = false)
{
Screen screen = Screen.FromPoint(MousePosition);
base.MaximumSize = this._isFullScreen ? screen.Bounds.Size : screen.WorkingArea.Size;
if (screen.Primary)
{
this.MaximizedBounds = this._isFullScreen ? screen.Bounds : screen.WorkingArea;
}
else
{
MaximizedBounds = new Rectangle(0, 0, 0, 0);
}
if (WindowState == FormWindowState.Normal)
{
this._beforeMaximizeSize = this.Size;
this._beforeMaximizeLocation = isOnMoving ? _formLocation : this.Location;
WindowState = FormWindowState.Maximized;
}
else if (WindowState == FormWindowState.Maximized)
{
if (this._beforeMaximizeSize.Width == 0 || this._beforeMaximizeSize.Height == 0)
{
this._beforeMaximizeSize.Width = MinimumSize.Width > 0 ? MinimumSize.Width : 800;
this._beforeMaximizeSize.Height = MinimumSize.Height > 0 ? MinimumSize.Height : 600;
}
this.Size = this._beforeMaximizeSize;
if (_beforeMaximizeLocation.X == 0 || this._beforeMaximizeLocation.Y == 0)
{
this._beforeMaximizeLocation.X = screen.Bounds.Left + screen.Bounds.Width / 2;
this._beforeMaximizeLocation.Y = screen.Bounds.Top + screen.Bounds.Height / 2;
}
this.Location = this._beforeMaximizeLocation;
WindowState = FormWindowState.Normal;
}
Invalidate();
}
#endregion[===== 私有函数 =====]
重写函数
#region[===== 重写函数 ======]
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.Width <= 0 || this.Height <= 0 || this.FormBorderStyle != FormBorderStyle.None) return;
Graphics g = e.Graphics;
Point centerPoint = new Point(0, 0);
if (this._showTitle)
{
g.FillRectangle(this._titleBackColor, 0, 0, this.Width, this._titleHeight);
g.DrawLine(this.TitleBorderColor, 0, this._titleHeight, this.Width, this._titleHeight);
if (this._showTitleIcon && this.Icon != null)
{
using (Image image = this.Icon.IconToImage())
{
g.DrawIcon(this.Icon, new Rectangle(5, (this._titleHeight - 20) / 2, 20, 20));
}
}
if (!string.IsNullOrEmpty(this.Text))
{
SizeF sf = e.Graphics.MeasureString(this.Text, this._titleFont);
PointF point = new Point(0, 0);
if (this._textAlignment == StringAlignment.Center)
{
point.X = (this.Width - sf.Width) / 2;
}
else if (this._textAlignment == StringAlignment.Far)
{
point.X = (this._controlBoxLeft - sf.Width - 5);
}
else
{
point.X = ((this._showTitleIcon && this.Icon != null) ? 20 : 0) + 5;
}
point.Y = (this.TitleHeight - sf.Height) / 2;
g.DrawString(this.Text, this._titleFont, this._titleForeColor, point);
}
if (this._extendBox)
{
centerPoint.X = this._extendBoxRect.Left + this._extendBoxRect.Width / 2;
centerPoint.Y = this._extendBoxRect.Top + this._extendBoxRect.Height / 2;
if (this._inExtendBox)
{
g.FillRectangle(this.ControlBoxFillHoverColor, this._extendBoxRect);
}
List<Point> points = new List<Point>
{
new Point(centerPoint.X - 5,centerPoint.Y - 2),
new Point(centerPoint.X,centerPoint.Y + 3),
new Point(centerPoint.X + 5,centerPoint.Y - 2)
};
g.DrawLines(this.ControlBoxForeColor, points.ToArray());
}
if (this._minimizeBox)
{
centerPoint.X = this._minimizeBoxRect.Left + this._minimizeBoxRect.Width / 2;
centerPoint.Y = this._minimizeBoxRect.Top + this._minimizeBoxRect.Height / 2;
if (this._inMinBox)
{
g.FillRectangle(this.ControlBoxFillHoverColor, this._minimizeBoxRect);
}
g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y, centerPoint.X + 6, centerPoint.Y);
}
if (this._maximizeBox)
{
centerPoint.X = this._maximizeBoxRect.Left + this._maximizeBoxRect.Width / 2;
centerPoint.Y = this._maximizeBoxRect.Top + this._maximizeBoxRect.Height / 2;
if (this._inMaxBox)
{
g.FillRectangle(this.ControlBoxFillHoverColor, this._maximizeBoxRect);
}
if (WindowState == FormWindowState.Maximized)
{
g.DrawRectangle(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 3, 8, 8);
List<Point> points = new List<Point>
{
new Point(centerPoint.X - 3, centerPoint.Y - 3),
new Point(centerPoint.X - 3, centerPoint.Y - 6),
new Point(centerPoint.X + 6, centerPoint.Y - 6),
new Point(centerPoint.X + 6, centerPoint.Y + 3),
new Point(centerPoint.X + 3, centerPoint.Y + 3),
};
g.DrawLines(this.ControlBoxForeColor, points.ToArray());
}
if (WindowState == FormWindowState.Normal)
{
g.DrawRectangle(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 6, 12, 12);
}
}
if (this._controlBox)
{
centerPoint.X = this._controlBoxRect.Left + this._controlBoxRect.Width / 2;
centerPoint.Y = this._controlBoxRect.Top + this._controlBoxRect.Height / 2;
if (this._inControlBox)
{
g.FillRectangle(this.CloseBoxFillHoverColor, this._controlBoxRect);
}
g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 6, centerPoint.X + 6, centerPoint.Y + 6);
g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y + 6, centerPoint.X + 6, centerPoint.Y - 6);
}
if (this._showFormBorder)
{
g.DrawRectangle(this._formBorderColor, 0, 0, this.Width - 1, this.Height - 1);
}
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
CalcSystemBoxPosition();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
CalcSystemBoxPosition();
}
#region[===== 鼠标事件重写 =====]
protected override void OnMouseMove(MouseEventArgs e)
{
if (_formMoveMouseDown && !MousePosition.Equals(_mouseLocation))
{
if (WindowState == FormWindowState.Maximized)
{
int maximizeWidth = this.Width;
int locationX = this.Left;
ShowMaximizeForm();
float offsetRatio = 1 - (float)this.Width / maximizeWidth;
this._mouseLocation.X -= (int)((_mouseLocation.X - locationX) * offsetRatio);
}
int offsetX = this._mouseLocation.X - MousePosition.X;
int offsetY = this._mouseLocation.Y - MousePosition.Y;
this.Location = new Point(this._formLocation.X - offsetX, this._formLocation.Y - offsetY);
}
else
{
if (this.FormBorderStyle == FormBorderStyle.None)
{
bool incontrolBox = e.Location.PointInRect(this._controlBoxRect);
if (WindowState == FormWindowState.Maximized && this._controlBox)
{
if (e.Location.X > this._controlBoxRect.Left && e.Location.Y < this._titleHeight) incontrolBox = true;
}
bool inMaxBox = e.Location.PointInRect(this._maximizeBoxRect);
bool inMinBox = e.Location.PointInRect(this._minimizeBoxRect);
bool inExtendBox = e.Location.PointInRect(this._extendBoxRect);
bool isChange = false;
if (incontrolBox != this._inControlBox)
{
this._inControlBox = incontrolBox;
isChange = true;
}
if (inMaxBox != this._inMaxBox)
{
this._inMaxBox = inMaxBox;
isChange = true;
}
if (inMinBox != this._inMinBox)
{
this._inMinBox = inMinBox;
isChange = true;
}
if (inExtendBox != this._inExtendBox)
{
this._inExtendBox = inExtendBox;
isChange = true;
}
if (isChange) Invalidate();
}
else
{
this._inExtendBox = this._inMinBox = this._inMaxBox = this._inControlBox = false;
}
}
base.OnMouseMove(e);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this._inExtendBox = this._inMinBox = this._inMaxBox = this._inControlBox = false;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (!IsDisposed && this._formMoveMouseDown)
{
Screen screen = Screen.FromPoint(MousePosition);
if (MousePosition.Y == screen.WorkingArea.Top && this._maximizeBox)
{
ShowMaximizeForm(true);
}
if (this.Top < screen.WorkingArea.Top)
{
this.Top = screen.WorkingArea.Top;
}
if (this.Top > screen.WorkingArea.Bottom - this._titleHeight)
{
this.Top = screen.WorkingArea.Bottom - this._titleHeight;
}
}
this._formMoveMouseDown = false;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (this._inControlBox || this._inMaxBox || this._inMinBox || this._inExtendBox || !this._showTitle || e.Y > Padding.Top) return;
if (e.Button == MouseButtons.Left)
{
this._formMoveMouseDown = true;
this._formLocation = this.Location;
this._mouseLocation = MousePosition;
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
if (FormBorderStyle == FormBorderStyle.None && this._showTitle)
{
if (this._inControlBox)
{
this._inControlBox = false;
this.Close();
}
if (this._inMaxBox)
{
this._inMaxBox = false;
ShowMaximizeForm();
}
if (this._inMinBox)
{
this._inMinBox = false;
WindowState = FormWindowState.Minimized;
}
if (this._inExtendBox)
{
this._inExtendBox = false;
MessageBox.Show("点击了扩展按钮矩形框!");
}
}
base.OnMouseClick(e);
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (!this.MaximizeBox) return;
if (this._inControlBox || this._inMaxBox || this._inMaxBox || this._inExtendBox) return;
if (e.Y > Padding.Top) return;
ShowMaximizeForm();
}
#endregion[===== 鼠标事件重写 =====]
#endregion[===== 重写函数 ======]
其他
public readonly Guid Guid = Guid.NewGuid();
public LemonForm()
{
base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}