C# WinForm(表單) 畫面UI『邊框畫線元件&四圓角(RoundPanel&PanelEx)』
C# WinForm(表單) 畫面UI『邊框畫線元件&四圓角(RoundPanel&PanelEx)』
資料來源:
https://blog.csdn.net/breakbridge/article/details/113403842
https://www.cnblogs.com/JiYF/p/9047559.html
GITHUB: https://github.com/jash-git/CS_RoundPanel-PanelEx
RoundPanel Class
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CS_VPOS
{
//https://blog.csdn.net/breakbridge/article/details/113403842
public partial class RoundPanel : Panel
{
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
Color _borderColor = Color.Black;
int _borderWidth = 1;
[Description("组件的边框颜色。"), Category("Appearance")]
public Color BorderColor
{
get
{
return _borderColor;
}
set
{
_borderColor = value;
this.Invalidate();
}
}
[Description("组件的边框宽度。"), Category("Appearance")]
public int BorderWidth
{
get
{
return _borderWidth;
}
set
{
_borderWidth = value;
this.Invalidate();
}
}
Color _currBColor = Color.Empty;
public void SetBackColorImg(Color color)
{
if (_currBColor != color)
{
drawBackColor(100, color);
_currBColor = color;
}
}
public RoundPanel()
{
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, false);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.Paint += new PaintEventHandler(PanelEx_Paint);
}
/// <summary>
/// 绘制背景色,以图片填充
/// </summary>
void drawBackColor(int percentage, Color drawColor)
{
percentage = Math.Min(percentage, 100);
Bitmap b = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(b);
int height = (int)(this.Height * (double)((100 - percentage) / (double)100));
Rectangle rectangle = new Rectangle(new Point(0, height), new Size(this.Width, this.Height));
g.DrawRectangle(new Pen(drawColor), rectangle);
g.FillRectangle(new SolidBrush(drawColor), rectangle);
g.Dispose();
this.BackgroundImage = b;
}
[Browsable(true)]
[Category("外观"), Description("生成该颜色的图片填充背景图片")]
public Color BackImgColor
{
get { return _currBColor; }
set
{
SetBackColorImg(value);
}
}
void PanelEx_Paint(object sender, PaintEventArgs e)
{
if (this.BorderStyle == BorderStyle.FixedSingle)
{
IntPtr hDC = GetWindowDC(this.Handle);
Graphics g = Graphics.FromHdc(hDC);
ControlPaint.DrawBorder(
g,
new Rectangle(0, 0, this.Width, this.Height),
_borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
_borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
_borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
_borderColor,
_borderWidth,
ButtonBorderStyle.Solid);
g.Dispose();
ReleaseDC(Handle, hDC);
}
}
//<summary>
//解决加载闪烁,背景透明等问题
//</summary>
protected override CreateParams CreateParams
{
get
{
var parms = base.CreateParams;
parms.Style &= ~0x02000000;
return parms;
}
}
uint _topLeftRadius;
[Browsable(true)]
[Category("倒角"), Description("左上圆角弧度(0为不要圆角)")]
public uint TopLeftRadius
{
get
{
return _topLeftRadius;
}
set
{
_topLeftRadius = value;
radiusChanged(value);
}
}
uint _topRightRadius;
[Browsable(true)]
[Category("倒角"), Description("右上圆角弧度(0为不要圆角)")]
public uint TopRightRadius
{
get
{
return _topRightRadius;
}
set
{
_topRightRadius = value;
radiusChanged(value);
}
}
uint _bottomLeftRadius;
[Browsable(true)]
[Category("倒角"), Description("左下圆角弧度(0为不要圆角)")]
public uint BottomLeftRadius
{
get
{
return _bottomLeftRadius;
}
set
{
_bottomLeftRadius = value;
radiusChanged(value);
}
}
uint _bottomRightRadius;
[Browsable(true)]
[Category("倒角"), Description("右下圆角弧度(0为不要圆角)")]
public uint BottomRightRadius
{
get
{
return _bottomRightRadius;
}
set
{
_bottomRightRadius = value;
radiusChanged(value);
}
}
void radiusChanged(uint radius)
{
base.Refresh();
}
uint _allRound = 0;
[Browsable(true)]
[Category("倒角"), Description("统一圆角弧度")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public uint AllRound
{
get { return _allRound; }
set
{
_allRound = value;
if (value != 0)
{
_topLeftRadius = _allRound;
_topRightRadius = _allRound;
_bottomLeftRadius = _allRound;
_bottomRightRadius = _allRound;
radiusChanged(value);
}
}
}
// 圆角代码
public void Round(System.Drawing.Region region)
{
System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();
int x = 0;
int y = 0;
int thisWidth = this.Width;
int thisHeight = this.Height;
if (_topLeftRadius > 0)
{
oPath.AddArc(x, y, _topLeftRadius, _topLeftRadius, 180, 90); // 左上角
}
oPath.AddLine(x + _topLeftRadius, y, thisWidth - _topRightRadius, y); // 顶端
if (_topRightRadius > 0)
{
oPath.AddArc(thisWidth - _topRightRadius, y, _topRightRadius, _topRightRadius, 270, 90); // 右上角
}
oPath.AddLine(thisWidth, y + _topRightRadius, thisWidth, thisHeight - _bottomRightRadius); // 右边
if (_bottomRightRadius > 0)
{
oPath.AddArc(thisWidth - _bottomRightRadius, thisHeight - _bottomRightRadius, _bottomRightRadius, _bottomRightRadius, 0, 90); // 右下角
}
oPath.AddLine(thisWidth - _bottomRightRadius, thisHeight, x + _bottomLeftRadius, thisHeight); // 底边
if (_bottomLeftRadius > 0)
{
oPath.AddArc(x, thisHeight - _bottomLeftRadius, _bottomLeftRadius, _bottomLeftRadius, 90, 90); // 左下角
}
oPath.AddLine(x, thisHeight - _bottomLeftRadius, x, y + _topLeftRadius); // 左边
oPath.CloseAllFigures();
this.Region = new System.Drawing.Region(oPath);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Round(this.Region); // 圆角
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
base.Refresh();
}
}
}
PanelEx Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CS_VPOS
{
//https://www.cnblogs.com/JiYF/p/9047559.html
public partial class PanelEx : Panel
{
private Color _BorderColor = Color.Black;
[Browsable(true), Description("边框颜色"), Category("自定义分组")]
public Color BorderColor
{
get { return _BorderColor; }
set
{
_BorderColor = value;
this.Invalidate();
}
}
private int _BorderSize = 1;
[Browsable(true), Description("边框粗细"), Category("自定义分组")]
public int BorderSize
{
get { return _BorderSize; }
set
{
_BorderSize = value;
this.Invalidate();
}
}
public PanelEx()
{
InitializeComponent();
}
public PanelEx(IContainer container)
{
container.Add(this);
InitializeComponent();
}
/// <summary>
/// 重写OnPaint方法
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics,
this.ClientRectangle,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid,
this._BorderColor,
this._BorderSize,
ButtonBorderStyle.Solid);
}
}
}
namespace CS_VPOS
{
partial class PanelEx
{
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該處置受控資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 元件設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// PanelEx
//
this.ResumeLayout(false);
}
#endregion
}
}
Use Code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(main));
this.panelEx1 = new CS_VPOS.PanelEx(this.components);
this.roundPanel1 = new CS_VPOS.RoundPanel();
this.SuspendLayout();
//
// panelEx1
//
this.panelEx1.BorderColor = System.Drawing.Color.Brown;
this.panelEx1.BorderSize = 3;
this.panelEx1.Location = new System.Drawing.Point(47, 26);
this.panelEx1.Name = "panelEx1";
this.panelEx1.Size = new System.Drawing.Size(397, 178);
this.panelEx1.TabIndex = 0;
//
// roundPanel1
//
this.roundPanel1.AllRound = ((uint)(20u));
//this.roundPanel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(74)))), ((int)(((byte)(110)))));
//this.roundPanel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("roundPanel1.BackgroundImage")));
//this.roundPanel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
//this.roundPanel1.BackImgColor = System.Drawing.Color.Transparent;
this.roundPanel1.BorderColor = System.Drawing.Color.Red;
this.roundPanel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.roundPanel1.BorderWidth = 5;
//this.roundPanel1.BottomLeftRadius = ((uint)(20u));
//this.roundPanel1.BottomRightRadius = ((uint)(20u));
this.roundPanel1.Location = new System.Drawing.Point(507, 187);
this.roundPanel1.Margin = new System.Windows.Forms.Padding(0);
this.roundPanel1.Name = "roundPanel1";
this.roundPanel1.Size = new System.Drawing.Size(859, 443);
this.roundPanel1.TabIndex = 1;
//this.roundPanel1.TopLeftRadius = ((uint)(20u));
//this.roundPanel1.TopRightRadius = ((uint)(20u));
//
// main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(74)))), ((int)(((byte)(110)))));
this.ClientSize = new System.Drawing.Size(1920, 1080);
this.Controls.Add(this.roundPanel1);
this.Controls.Add(this.panelEx1);
this.Font = new System.Drawing.Font("微軟正黑體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "main";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
private PanelEx panelEx1;
private RoundPanel roundPanel1;
