C# 如何绘制圆形矩形作为圆形窗体的边框?
我正在创建一个具有圆形边框的表单(如所示)。C# 如何绘制圆形矩形作为圆形窗体的边框?,c#,winforms,rectangles,C#,Winforms,Rectangles,我正在创建一个具有圆形边框的表单(如所示)。 因为这个人似乎也有一个问题,我似乎不能画一个圆形的边界 这是我用来设置实际边框形状的代码: /。。。在InitializeComponent中。。。 this.FormBorderStyle=FormBorderStyle.None; IntPtr handle=CreateRoundRectRgn(0,0,宽度,高度,20,20); 区域=系统.Drawing.Region.FromHrgn(句柄); 删除对象(句柄); this.ResizeR
因为这个人似乎也有一个问题,我似乎不能画一个圆形的边界 这是我用来设置实际边框形状的代码:
/。。。在InitializeComponent中。。。
this.FormBorderStyle=FormBorderStyle.None;
IntPtr handle=CreateRoundRectRgn(0,0,宽度,高度,20,20);
区域=系统.Drawing.Region.FromHrgn(句柄);
删除对象(句柄);
this.ResizeRedraw=true;
这是覆盖OnPaint
并绘制边框轮廓的代码
protected override void OnPaint(PaintEventArgs e)
{
//我试着在这里修改参数。
GraphicsPath=MyRoundedRectangle.Create(0,0,宽度,高度,10,MyRoundedRectangle.RectangleCorners.All);
钢笔p=新钢笔(黑色,3f);
e、 图形绘制路径(p,路径);
}
MyRoundedRectangle
的内容与中提供的代码相同,其中答案链接到,其中包含MyRoundedRectangle
的代码
我希望有一个完整的边框,但我得到的是:注释中描述的基本实现。
表单
frmRoundCorners
提供了一些属性,允许使用自定义底色、自定义边框颜色和自定义内边框颜色绘制其圆角区域,作为表单边框内侧的阴影
表单本身是使用从表单
派生的基类baseForm
实现的,因此可以在表单设计器中设置表单的属性
将窗体的原始背景色设置为其TrasparencyKey
时,将激活透明度,使其客户端区域
完全透明,但可绘制。表单的原始边框设置为
FormBorderStyle。基类构造函数中没有
。我没有设置特定的
BackColor/TransparencyKey
Color(必须在表单的设计器中设置),因为我认为这是一个需要尝试的东西。我建议用中灰色。避免使用红色组件
可以移动表单,单击其客户端区域的任意点并拖动它
表单及其自定义边框的最小/最大曲率设置为15
和180
度。无法使用PropertyGrid将其更改为其他范围。
使用该方法绘制形状及其边框的圆形区域,然后对图形对象应用变换,包括比例和变换(位置)组件。大小组件未被触及
这就是它看起来的样子:
我认为您可以使用透明窗体(设置其TransparencyKey
=背景色
,一种要明智选择的颜色:例如,不是红色),然后使用GraphicsPath.AddArc()
绘制圆形背景和边框(如果需要)。您可以在这里举个例子:。使用表单更容易(透明效果非常好)。在表单的构造函数中添加以下样式:SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDubleBuffer | ControlStyles.ResizerDraw,true)代码>。使用GraphicsPath.AddArc()
,可以设置从15
到360
度的曲线。但是15-180
范围更好。否则,绘制一个椭圆。加上一个答案,我会告诉你你的声誉我一直在寻找类似的东西。谢谢
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
[ToolboxItem(false)]
public partial class frmRoundCorners : baseForm
{
private GraphicsPath pathRegion = new GraphicsPath(FillMode.Winding);
private GraphicsPath pathBorder;
Point pMousePosition = Point.Empty;
public frmRoundCorners()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw, true);
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
pMousePosition = e.Location;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
Point screenPos = PointToScreen(e.Location);
this.Location = new Point(screenPos.X - pMousePosition.X, screenPos.Y - pMousePosition.Y);
}
base.OnMouseMove(e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
RoundedCornerRectangle(this.ClientRectangle);
RectangleF rect = pathRegion.GetBounds();
float scaleX = 1 - (BorderSize / rect.Width);
float scaleY = 1 - (BorderSize / rect.Height);
using (Pen pen = new Pen(BorderColor, BorderSize))
using (Pen penBorder = new Pen(InternalBorderColor, 2))
using (Brush brush = new SolidBrush(FillColor))
using (Matrix mx = new Matrix(scaleX, 0, 0, scaleY, (pen.Width / 2), (pen.Width / 2)))
{
e.Graphics.Transform = mx;
e.Graphics.FillPath(brush, pathRegion);
e.Graphics.DrawPath(penBorder, pathBorder);
e.Graphics.DrawPath(pen, pathRegion);
}
}
private void RoundedCornerRectangle(Rectangle r)
{
pathRegion = new GraphicsPath(FillMode.Alternate);
float innerCurve = this.CurveAngle - this.m_PenSizeOffset;
pathRegion.StartFigure();
pathRegion.AddArc(r.X, r.Y, CurveAngle, CurveAngle, 180, 90);
pathRegion.AddArc(r.Right - CurveAngle, r.Y, CurveAngle, CurveAngle, 270, 90);
pathRegion.AddArc(r.Right - CurveAngle, r.Bottom - CurveAngle, CurveAngle, CurveAngle, 0, 90);
pathRegion.AddArc(r.X, r.Bottom - CurveAngle, CurveAngle, CurveAngle, 90, 90);
pathRegion.CloseFigure();
pathBorder = new GraphicsPath();
pathBorder.StartFigure();
pathBorder.AddArc(r.X + m_PenSizeOffset, r.Y + m_PenSizeOffset, innerCurve, innerCurve, 180, 90);
pathBorder.AddArc(r.Right - innerCurve - m_PenSizeOffset, r.Y + m_PenSizeOffset, innerCurve, innerCurve, 270, 90);
pathBorder.AddArc(r.Right - innerCurve - m_PenSizeOffset, r.Bottom - innerCurve- m_PenSizeOffset, innerCurve, innerCurve, 0, 90);
pathBorder.AddArc(r.X + m_PenSizeOffset, r.Bottom - innerCurve - m_PenSizeOffset, innerCurve, innerCurve, 90, 90);
pathBorder.CloseFigure();
}
}
public class baseForm : Form
{
private Color m_InternalBorderColor = Color.FromArgb(128, 128, 128);
private Color m_BorderColor = Color.Red;
private Color m_FillColor = Color.WhiteSmoke;
private float m_PenSize = 6f;
private float m_CurveAngle = 60.0f;
internal float m_PenSizeOffset = 3f;
public baseForm() => InitializeComponent();
private void InitializeComponent() => FormBorderStyle = FormBorderStyle.None;
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(60.0f)]
public virtual float CurveAngle
{
get => this.m_CurveAngle;
set {
this.m_CurveAngle = Math.Max(Math.Min(value, 180), 15);
Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(6.0f)]
public virtual float BorderSize
{
get => this.m_PenSize;
set {
this.m_PenSize = value;
this.m_PenSizeOffset = value / 2.0f;
this.Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual Color BorderColor
{
get => this.m_BorderColor;
set {
this.m_BorderColor = value;
this.Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual Color FillColor
{
get => this.m_FillColor;
set {
this.m_FillColor = value;
this.Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Description("Get or Set the Color of the internal border")]
public virtual Color InternalBorderColor
{
get => this.m_InternalBorderColor;
set {
this.m_InternalBorderColor = value;
this.Invalidate();
}
}
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[DefaultValue(FormBorderStyle.None)]
public new FormBorderStyle FormBorderStyle
{
get => base.FormBorderStyle;
set => base.FormBorderStyle = FormBorderStyle.None;
}
}