using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace panelModify
{
/// <summary>
/// 邊框模式(無、單色、三維)
/// </summary>
public enum BorderMode { None, Single, ThreeD }
/// <summary>
/// 可設置邊框樣式的Panel
/// </summary>
public class PanelEx : Panel
{
private Color borderColor;
private Border3DStyle border3DStyle;
private ToolStripStatusLabelBorderSides borderSide;
private otherSide.other_side _os;
private BorderMode borderMode;
[DefaultValue(BorderMode.None), Description("邊框模式。可設置單色模式或三維
模式")]
public BorderMode BorderMode
{
get { return borderMode; }
set
{
if (borderMode == value) { return; }
borderMode = value;
this.Invalidate();
}
}
[DefaultValue(typeof(Color), "Black"), Description("邊框顏色。僅當邊框為單色模式
時有效")]
public Color BorderColor
{
get { return borderColor; }
set
{
if (borderColor == value) { return; }
borderColor = value;
this.Invalidate();
}
}
[DefaultValue(Border3DStyle.Etched), Description("邊框三維樣式。僅當邊框為三
維模式時有效")]
public Border3DStyle Border3DStyle
{
get { return border3DStyle; }
set
{
if (border3DStyle == value) { return; }
border3DStyle = value;
this.Invalidate();
}
}
//
[DefaultValue(ToolStripStatusLabelBorderSides.All), Description("邊框位置。可自
由啟用各個方位的邊框")]
public ToolStripStatusLabelBorderSides BorderSide
{
get { return borderSide; }
set
{
if (borderSide == value) { return; }
borderSide = value;
this.Invalidate();
}
}
public otherSide.other_side os
{
get { return _os; }
set
{
if (_os == value) { return; }
_os = value;
this.Invalidate();
}
}
private int _BorderSize = 1;
[Browsable(true), Description("邊框粗細"), Category("自定義分類")]
public int BorderSize
{
get { return _BorderSize; }
set
{
_BorderSize = value;
this.Invalidate();
}
}
public PanelEx()
{
this.borderMode = BorderMode.None;
this.borderColor = Color.Black;
this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched;
this.borderSide = ToolStripStatusLabelBorderSides.All;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.BorderStyle != System.Windows.Forms.BorderStyle.None
|| BorderMode == BorderMode.None
|| BorderSide == ToolStripStatusLabelBorderSides.None)
{ return; }
using (Graphics g = e.Graphics)
{
//三維模式
if (this.BorderMode == BorderMode.ThreeD)
{
ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.Border3DStyle,
(Border3DSide)BorderSide);
//將ToolStripStatusLabelBorderSides轉換為Border3DSide
}
else //單色模式
{
using (Pen pen = new Pen(BorderColor, _BorderSize))
{
//2018.10.18 ovveride
if (os == otherSide.other_side.All)
{
g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.top)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
}
if (os == otherSide.other_side.left)
{
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
}
if (os == otherSide.other_side.right)
{
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.bottom)
{
g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.left_right)
{
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.top_bottom)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.top_right)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.top_left)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
}
if (os == otherSide.other_side.top_left_bottom)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.top_right_bottom)
{
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.left_top_right)
{
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
g.DrawLine(pen, 0, 0, this.Width - 1, 0);
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
}
if (os == otherSide.other_side.left_bottom_right)
{
g.DrawLine(pen, 0, 0, 0, this.Height - 1);
g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
}
}
}
}
}
}
}