C# 设置CircularPictureBox边框颜色
我正在创建一个自定义图片盒 正如你所见,这是一个专为个人资料照片设计的图片盒 这是CircularPictureBox的类C# 设置CircularPictureBox边框颜色,c#,graphics,picturebox,C#,Graphics,Picturebox,我正在创建一个自定义图片盒 正如你所见,这是一个专为个人资料照片设计的图片盒 这是CircularPictureBox的类 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Threa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Hector.Framework.Controls
{
public class CircularPictureBox : PictureBox
{
private Color _idlecolor = Color.White;
public CircularPictureBox()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.DoubleBuffered = true;
this.BackColor = Color.White;
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.Size = new Size(100, 100);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
using (var gpath = new GraphicsPath())
{
var brush = new SolidBrush(this.IdleBorderColor);
var pen = new Pen(brush, 5);
var outrect = new Rectangle(-1, -1, this.Width + 5, this.Height + 5);
gpath.AddEllipse(outrect);
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
pe.Graphics.DrawPath(pen, gpath);
brush.Dispose();
pen.Dispose();
gpath.Dispose();
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
using (var gpath = new GraphicsPath())
{
var rect = new Rectangle(1, 1, this.Width - 1, this.Height - 1);
gpath.AddEllipse(rect);
this.Region = new Region(gpath);
gpath.Dispose();
}
}
public Color IdleBorderColor
{
get => this._idlecolor;
set => this._idlecolor = value;
}
}
}
我的问题是,因为它是一个可以从设计器使用的控件,所以我希望它具有诸如边宽度或边框颜色之类的属性
我开始测试颜色,但每次我改变颜色,
Visual Studio向我显示一条错误消息,指出属性的值无效我对您的代码进行了一些修改,以突出显示在自定义控件设计中可能有用的一些功能 我认为我所做的修改是不言自明的。
但是,请查看
OnPaint
事件。e.Graphics.Clip
区域用于隐藏不在选定区域中的所有图形部分。这意味着,在设计模式下拖动控件时,图像将被剪裁,并且在区域外看不到图像。和有助于渲染的整体质量(有注释掉的选项在其他情况下可能有用)。
边界偏移的计算必须参考边界大小宽度,并相应缩放。笔对象从其大小的中间开始绘制。如果笔的大小为3个像素,则在边框上绘制1个像素,一个在区域外,一个在区域内(奇怪?可能)。
这里的透明度设置只是一个“假”设置。
它可能在其他情况下有效使用(应为“平台”)。
当然,说明会向用户解释属性的用途。
类别用于为属性提供内部的有机配置。您可以使用标准名称(
外观
,行为
等)或指定其他任何内容。为
Category
提供一个自定义名称,当Categorized
视图正在使用时,它将被列在其他名称中。自定义控件的
图像
属性已被隐藏并替换为位图
属性: [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never), BrowsableAttribute(false)]
该属性是对Intellisense的提示,允许您确定是否在弹出菜单中显示属性或方法。它可以是从不
、始终
或高级
(适用于那些知道如何访问VS选项的人)。在部署自定义控件(作为dll)时,而不是在设计自定义控件时,属性和方法将被隐藏。
属性(或仅[Browsable]
)允许指定该属性是否应显示在PropertyGrid中。
使用DesignerSerializationVisibility
属性,可以指示
属性的值是否可见,并且是否应该持久化
在初始化代码中,隐藏
,不应在
初始化代码,或由内容组成,应具有
为每个公共而非隐藏属性生成的初始化代码
指定给属性的对象
也很有趣:
使用此属性,可以指示在PropertyGrid中列出类对象的公共属性。
此类对象可以是序列化控件复杂属性的内部类。
它本身非常有趣。
嗯,您输入的哪些值是无效的?@ForeverZer0在Visual Studio designer中,由IdleColor创建的属性通过更改颜色向我显示此消息。我可以看到一个严重的错误,请不要在paint事件中分配Region属性。太多的副作用,它会导致油漆再次被点燃,你会看到VS燃烧100%的核心。它属于OnResize()。这也会轰炸你的代码,因为你忘了在画笔和画笔上调用Dispose(),5000次画画之后,表演就结束了,这反过来会导致VS非常不安。在调用方法_0之前检查设计模式。(LicenseManager.UsageMode==LicenseSusageMode.Designtime)首选不设置区域。如果将区域设置为圆形区域,则会在图片框周围看到锯齿状边框,同时可以使用SetClip
将其设置为圆形路径,强制控件以圆形形状绘制自身。然后,对于透明效果,只需将颜色设置回color.Transparent
。要查看这两种情况的示例,请看。我复制了您的代码,但在分配图像时,它根本不显示您如何分配图像?您是否注意到图像
属性已禁用?必须使用新的Bitmap
属性将图像分配给控件。把它放到一个表单上,看看PropertyGrid。哦,太好了,我在代码中使用了Image属性,但是是的,它是位图,thanksI修改了它,只是为了显示如何抑制默认属性并用自定义属性替换。我想你可以把这门课当作运动场。当然,您可以将其恢复为标准行为。不会影响结果。好吧,如果这是真的,我已经知道,通过使用关键字new,你可以隐藏一个成员来创建一个自定义成员,但我不认为这是我想要做的事情的必要条件
[Description("Gets or Sets the Color of the Border drawn around the Image.")
[Category("Appearance")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never), BrowsableAttribute(false)]