C#绘制圆角矩形
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using Jasen.Framework.Drawing;
namespace Jasen.Framework.WinApp
{
public partial class MainForm : Form
{
private bool _isLoading = false;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
this._isLoading = true;
LoadData();
this._isLoading = false;
DrawRectangleRoundStyle();
}
private void LoadData()
{
LoadRoundStyles();
LoadGradientModes();
LoadSmoothingModes();
LoadInterpolationModes();
string[] colorNames = typeof(Color).GetProperties().AsParallel().
Where(p => p.PropertyType == typeof(Color)).Select(p => ).ToArray();
if (colorNames == null || colorNames.Count() == 0)
{
return;
}
LoadStartColors(colorNames);
LoadEndColors(colorNames);
LoadBorderColors(colorNames);
LoadInnerBorderColors(colorNames);
}
private void LoadBorderColors(string[] colorNames)
{
this.cboBorderColors.Items.AddRange(colorNames);
this.cboBorderColors.SelectedIndex = new Random().Next(0,colorNames.Length);
}
private void LoadInnerBorderColors(string[] colorNames)
{
this.cboInnerBorderColors.Items.AddRange(colorNames);
this.cboInnerBorderColors.SelectedIndex = new Random().Next(0, colorNames.Length);
}
private void LoadEndColors(string[] colorNames)
{
this.cboEndColors.Items.AddRange(colorNames);
this.cboEndColors.SelectedIndex = new Random().Next(0, colorNames.Length);
}
private void LoadStartColors(string[] colorNames)
{
this.cboStartColors.Items.AddRange(colorNames);
this.cboStartColors.SelectedIndex = new Random().Next(0, colorNames.Length);
}
private void LoadInterpolationModes()
{
this.cboInterpolationModes.Items.AddRange(Enum.GetNames(typeof(InterpolationMode)));
this.cboInterpolationModes.SelectedIndex = 0;
}
private void LoadSmoothingModes()
{
this.cboSmoothingModes.Items.AddRange(Enum.GetNames(typeof(SmoothingMode)));
this.cboSmoothingModes.SelectedIndex = 2;
}
private void LoadGradientModes()
{
this.cboGradientModes.Items.AddRange(Enum.GetNames(typeof(LinearGradientMode)));
this.cboGradientModes.SelectedIndex = 0;
}
private void LoadRoundStyles()
{
this.clstRoundStyles.Items.AddRange(Enum.GetNames(typeof(Drawing.RoundStyle)));
}
private void DrawRectangleRoundStyle()
{
InterpolationMode interpolationMode;
Enum.TryParse
SmoothingMode smoothingMode;
Enum.TryParse
if(smoothingMode==SmoothingMode.Invalid)
{
smoothingMode=SmoothingMode.None;
}
using (Graphics graphic = this.pictureBox.CreateGraphics())
{
graphic.Clear(Color.White);
graphic.InterpolationMode = interpolationMode;
graphic.SmoothingMode = smoothingMode;
Rectangle rectangle = this.pictureBox.ClientRectangle;
rectangle.Inflate(-50, -50);
RoundStyle roundStyle = GetRoundStyle();
int radius = GetRadius(ref rectangle);
Color startColor = Color.FromName((this.cboStartColors.SelectedItem ?? string.Empty).ToString());
Color endColor = Color.FromName((this.cboEndColors.SelectedItem ?? string.Empty).ToString());
Color borderColor = Color.FromName((this.cboBorderColors.SelectedItem ?? string.Empty).ToString());
Color innerBorderColor = Color.FromName((this.cboInnerBorderColors.SelectedItem ?? string.Empty).ToString());
if (this.ckbDrawBg.Checked)
{
graphic.FillRectangle(new SolidBrush(Color.Black), rectangle);
}
using (GraphicsPath path = GraphicsPathHelper.CreatePath(rectangle,
radius, roundStyle, this.ckbShowSelfBorder.Checked))
{
LinearGradientMode gradientMode;
Enum.TryParse
using (LinearGradientBrush brush = new LinearGradientBrush(
rectangle, startColor, endColor, gradientMode))
{
Blend blend = new Blend();
blend.Factors = new float[] { 1f, 0.5f, 0f };
blend.Positions = new float[] { 0f, 0.5f, 1f };
graphic.FillPath(brush, path);
}
//using (Pen pen = new Pen(new SolidBrush(borderColor), 1))
//{
// graphic.DrawPath(pen, path);
//}
}
//rectangle.Inflate(-1, -1);
//using (GraphicsPath path = Drawing
.GraphicsPathHelper.CreatePath(
// rectangle, radius, roundStyle, false))
//{
// using (Pen pen = new Pen(new SolidBrush(innerBorderColor), 1))
// {
// graphic.DrawPath(pen, path);
// }
//}
}
}
private Drawing.RoundStyle GetRoundStyle()
{
int count = this.clstRoundStyles.Items.Count;
Drawing.RoundStyle roundStyle = Drawing.RoundStyle.None;
for (int index = 0; index < count; index++)
{
if (this.clstRoundStyles.GetItemChecked(index))
{
Drawing.RoundStyle tempRoundStyle;
Enum.TryParse
roundStyle |= tempRoundStyle;
}
}
return roundStyle;
}
private int GetRadius(ref Rectangle rectangle)
{
int radius;
int.TryParse(tbRadius.Text, out radius);
if (radius <= 0)
{
radius = 0;
}
if (radius > Math.Min(rectangle.Width, rectangle.Height))
{
radius = Math.Min(rectangle.Width, rectangle.Height);
}
return radius;
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (this._isLoading)
{
return;
}
DrawRectangleRoundStyle();
}
private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
DrawRectangleRoundStyle();
}
private void btnDraw_Click(object sender, EventArgs e)
{
DrawRectangleRoundStyle();
}
private void ckbShowSelfBorder_CheckedChanged(object sender, EventArgs e)
{
DrawRectangleRoundStyle();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace Jasen.Framework.Drawing
{
public static class GraphicsPathHelper
{
private static IDictionary
RoundStylesEventContainer
{
get;
set;
}
static GraphicsPathHelper()
{
RoundStylesEventContainer
= new Dictionary
}
///
/// 建立带有圆角样式的路径。
///
/// 用来建立路径的矩形。
/// 圆角半径。
///
ame="style">圆角的样式。
/// 是否显示边框。
///
public static GraphicsPath CreatePath(Rectangle rect, int radius,
RoundStyle style = RoundStyle.All, bool showBorder = false)
{
GraphicsPath path = new GraphicsPath();
int radiusAdjustment = showBorder ? 1 : 0;
if (RoundStylesEventContainer.Count == 0)
{
RoundStylesEventContainer = CreateRoundStylesEventContainer();
}
if (!RoundStylesEventContainer.ContainsKey(style))
{
return path;
}
var currEvent = RoundStylesEventContainer[style];
currEvent.DynamicInvoke(new object[] { path, rect, radius, radiusAdjustment });
path.CloseFigure();
return path;
}
private static IDictionary
CreateRoundStylesEventContainer()
{
var container = new Dictionary
container.Add(RoundStyle.None, (path, rect, radius, radiusAdjustment) => { path.AddRectangle(rect); });
container.Add(RoundStyle.All, AddAllRoundStyle);
container.Add(RoundStyle.BottomLeft, AddBottomLeftRoundStyle);
container.Add(RoundStyle.BottomRight, AddBottomRightRoundStyle);
container.Add(RoundStyle.TopLeft, AddTopLeftRoundStyle);
container.Add(RoundStyle.TopRight, AddTopRightRoundStyle);
container.Add(RoundStyle.BottomRight | RoundStyle.BottomLeft, AddBottomRightAndBottomLeftRoundStyle);
container.Add(RoundStyle.BottomRight | RoundStyle.TopRight, AddBottomRightAndTopRightRoundStyle);
container.Add(RoundStyle.TopRight | RoundStyle.TopLeft, AddTopRightAndTopLeftRoundStyle);
container.Add(RoundStyle.BottomLeft | RoundStyle.TopLeft, AddBottomLeftAndTopLeftRoundStyle);
container.Add(RoundStyle.BottomRight | RoundStyle.TopLeft, AddBottomRightAndTopLeftRoundStyle);
container.Add(RoundStyle.TopRight | RoundStyle.BottomLeft, AddTopRightAndBottomLeftRoundStyle);
container.Add(RoundStyle.BottomLeft | RoundStyle.BottomRight | RoundStyle.TopLeft, AddBottomAndTopLeftRoundStyle);
container.Add(RoundStyle.BottomLeft | RoundStyle.BottomRight | RoundStyle.TopRight, AddBottomAndTopRightRoundStyle);
container.Add(RoundStyle.BottomLeft | RoundStyle.TopLeft | RoundStyle.TopRight, AddTopAndBottomLeftRoundStyle);
container.Add(RoundStyle.BottomRight | RoundStyle.TopRight | RoundStyle.TopLeft, AddTopAndBottomRightRoundStyle);
return container;
}
#region Two Arc
private static void AddTopRightAndBottomLeftRoundStyle(GraphicsPath
path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomLineWithLeftOffset(path, rect, radius, radiusAdjustment, false);
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLineWithRightOffset(path, rect, radius, radiusAdjustment);
}
private static void AddBottomRightAndTopLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLineWithRightOffset(path, rect, radius, radiusAdjustment);
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopLineWithLeftOffset(path, rect, radius, radiusAdjustment);
}
private static void AddBottomLeftAndTopLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddRightLine(rect, path, radiusAdjustment);
}
private static void AddTopRightAndTopLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomLine(rect, path, radiusAdjustment);
}
private static void AddBottomRightAndTopRightRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddLeftLine(rect, path, radiusAdjustment);
}
private static void AddBottomRightAndBottomLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLine(rect, path, radiusAdjustment);
}
#endregion
#region One Arc
private static void AddBottomLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLine(rect, path, radiusAdjustment);
AddRightLine(rect, path, radiusAdjustment);
}
private static void AddBottomRightRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddLeftLine(rect, path, radiusAdjustment);
AddTopLine(rect,
path, radiusAdjustment);
}
private static void AddTopRightRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomLine(rect, path, radiusAdjustment);
AddLeftLine(rect, path, radiusAdjustment);
}
private static void AddTopLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddRightLine(rect, path, radiusAdjustment);
AddBottomLine(rect, path, radiusAdjustment);
}
#endregion One Arc
#region Three Arc
private static void AddTopAndBottomRightRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLineWithRightOffset(path, rect, radius, radiusAdjustment);
}
private static void AddTopAndBottomLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomLineWithLeftOffset(path, rect, radius, radiusAdjustment, false);
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
}
private static void AddBottomAndTopRightRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLineWithRightOffset(path, rect, radius, radiusAdjustment);
AddTopRightArc(rect, radius, path, radiusAdjustment);
}
private static void AddBottomAndTopLeftRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLeftArc(rect, radius, path, radiusAdjustment);
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopLineWithLeftOffset(path, rect, radius, radiusAdjustment);
}
#endregion
private static void AddAllRoundStyle(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
AddTopLeftArc(rect, radius, path, radiusAdjustment);
AddTopRightArc(rect, radius, path, radiusAdjustment);
AddBottomRightArc(rect, radius, path, radiusAdjustment);
AddBottomLeftArc(rect, radius, path, radiusAdjust
ment);
}
#region Add Line
///
/// 增加底部直线
///
///
///
///
private static void AddBottomLine(Rectangle rect, GraphicsPath path, int radiusAdjustment)
{
path.AddLine(rect.Right - radiusAdjustment, rect.Bottom - radiusAdjustment,
rect.Left + radiusAdjustment, rect.Bottom - radiusAdjustment);
}
///
/// 增加左侧直线
///
///
///
///
private static void AddLeftLine(Rectangle rect, GraphicsPath path, int radiusAdjustment)
{
path.AddLine(rect.Left + radiusAdjustment, rect.Bottom - radiusAdjustment,
rect.Left + radiusAdjustment, rect.Top + radiusAdjustment);
}
///
/// 增加顶部直线
///
///
///
///
private static void AddTopLine(Rectangle rect, GraphicsPath path, int radiusAdjustment)
{
path.AddLine(
rect.Left + radiusAdjustment, rect.Top + radiusAdjustment,
rect.Right - radiusAdjustment, rect.Top + radiusAdjustment);
}
///
/// 增加右侧直线
///
///
///
///
private static void AddRightLine(Rectangle rect, GraphicsPath path, int radiusAdjustment)
{
path.AddLine(
rect.Right - radiusAdjustment, rect.Y + radiusAdjustment,
rect.Right - radiusAdjustment, rect.Bottom - radiusAdjustment);
}
#endregion
#region Add Arc
private static void AddBottomLeftArc(Rectangle rect, int radius, GraphicsPath path, int radiusAdjustment)
{
path.AddArc(rect.X + radiusAdjustment,
rect.Bottom - radius - radiusAdjustment,
radius,
radius,
90,
90);
}
private static void AddBottomRightArc(Rectangle rect, int radius, GraphicsPath path, int radiusAdjustment)
{
path.AddArc(rect.Right - radius - radiusAdjustment,
rect.Bottom - radius - radiusAdjustment,
radius,
radius, 0, 90);
}
private static void AddTopRightArc(Rectangle rect, int radius, GraphicsPath path, int radiusAdjustment)
{
path.AddArc(rect.Right - radius - radiusAdjustment,
rect.Y + radiusAdjustment,
radius,
radius,
270,
90);
}
private static void AddTopLeftArc(Rectangle rect, int radius, GraphicsPath path, int radiusAdjustment)
{
path.AddArc(rect.X + radiusAdjustment, rect.Y + radiusAdjustment, radius, radius, 180, 90);
}
#endregion
private static void AddTopLineWithRightOffset(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
path.AddLine(
rect.Left + radiusAdjustment, rect.Top + radiusAdjustment,
rect.Right - radiusAdjustment - radius, rect.Top + radiusAdjustment);
}
private static void AddTopLineWithLeftOffset(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment, bool leftToRight = true)
{
Point startPoint = new Point(rect.Left + radiusAdjustment + radius, rect.Top + radiusAdjustment);
Point endPoint = new Point(rect.Right - radiusAdjustment, rect.Top + radiusAdjustment);
path.AddLine(leftToRight ? startPoint : endPoint, leftToRight ? endPoint : startPoint);
}
private static void AddBottomLineWithLeftOffset(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment, bool leftToRight = true)
{
Point startPoint = new Point(rect.Left + radiusAdjustment + radius, rect.Bottom - radiusAdjustment);
Point endPoint = new Point(rect.Right - radiusAdjustment, rect.Bottom - radiusAdjustment);
path.AddLine(leftToRight ? startPoint : endPoint, leftToRight ? endPoint : startPoint);
}
private static void AddBottomLineWithRightOffset(GraphicsPath path,
Rectangle rect, int radius, int radiusAdjustment)
{
path.AddLine(
rect.Right - radiusAdjustment - radius, rect.Bottom - radiusAdjustment,
rect.Left + radiusAdjustment, rect.Bottom - radiusAdjustment);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Jasen.Framework.Drawing
{
[Flags]
public enum RoundStyle
{
None = 0,
TopLeft = 1,
TopRight = 2,
BottomLeft = 4,
BottomRight = 8 ,
All = TopLeft | TopRight | BottomLeft | BottomRight
}
}