如何创建自定义属性,如在c#中的按钮上添加图像?
我正在开发一个自定义UI编辑器。编辑器允许我们在按钮上放置背景图像。但是,与VS编辑器不同,我们不能放置前景图像 要在属性栏中添加属性,我们只需添加以下代码如何创建自定义属性,如在c#中的按钮上添加图像?,c#,winforms,C#,Winforms,我正在开发一个自定义UI编辑器。编辑器允许我们在按钮上放置背景图像。但是,与VS编辑器不同,我们不能放置前景图像 要在属性栏中添加属性,我们只需添加以下代码 [Browsable(true), Category("Custom controls"), Description("Background Image of Control")] //[EditorAttribute(typeof(UIFileNameEditor), typeof(System.Drawi
[Browsable(true), Category("Custom controls"), Description("Background Image of Control")]
//[EditorAttribute(typeof(UIFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
//public UIBitmap UIBackImage
public UIImageRes UIBackImage
{
get { return backImage; }
set { backImage = value; Invalidate(); }
}
现在,要添加背景图像,我们有以下代码
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
e.Graphics.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
GraphicsUnit units = GraphicsUnit.Pixel;
if (this.Is9Slice == UISlice.no && this.UIBackImage.UIImagePath != null && this.UIBackImage.UIImagePath !="" )
{
Bitmap BgI = new Bitmap(this.backImage.UIImagePath);
Rectangle dest = new Rectangle(0, 0, this.Width, this.Height);
Rectangle src = Rectangle.Empty;
if (backImage.UIUVRectangle.Width * backImage.UIUVRectangle.Height != 0)
{
src = backImage.UIUVRectangle;
}
else
{
src= new Rectangle(0, 0, BgI.Width, BgI.Height);
}
e.Graphics.DrawImage(BgI, dest, src, units);
return;
}
if (this.UIBackImage.UIImagePath != null && this.UIBackImage.UIImagePath != "")
{
Bitmap BgImage = new Bitmap(this.backImage.UIImagePath);
Size BgI = new Size(UIBackImage.UIUVRectangle.Width, UIBackImage.UIUVRectangle.Height);
Point Offset = new Point(UIBackImage.UIUVRectangle.X, UIBackImage.UIUVRectangle.Y);
Rectangle dTopLeft, dTopCenter, dTopRight, //Destination Rectangles
dMidLeft, dMidCenter, dMidRight,
dBottomLeft, dBottomCenter, dBottomRight;
Rectangle sTopLeft, sTopCenter, sTopRight, //Source Rectangles
sMidLeft, sMidCenter, sMidRight,
sBottomLeft, sBottomCenter, sBottomRight;
int side_control;
int side = side_control = (int)(BgI.Width < BgI.Height ? BgI.Width * 0.2f: BgI.Height * 0.2f);
if (2 * side > this.Size.Width || 2 * side > this.Size.Height)
side_control = (int)(this.Width < this.Height ? this.Width / 2 : this.Height / 2);
if (designScaleFactor != 1.0f)
side_control *= (int)designScaleFactor;
dTopLeft = dTopCenter = dTopRight = dMidLeft = dMidCenter = dMidRight = dBottomLeft = dBottomCenter = dBottomRight = new Rectangle();
sTopLeft = sTopCenter = sTopRight = sMidLeft = sMidCenter = sMidRight = sBottomLeft = sBottomCenter = sBottomRight = new Rectangle();
/* Top Left */
dTopLeft.Location = new Point(0, 0);
dTopLeft.Width = dTopLeft.Height = side_control;
sTopLeft.Location = new Point(0 + Offset.X, 0 + Offset.Y ); // Just add the offset here for reflecting in every other rectangle
sTopLeft.Width = sTopLeft.Height = side;
/* Top Center */
dTopCenter.Location = new Point(dTopLeft.Right, dTopLeft.Top);
dTopCenter.Width = this.Width - 2 * side_control;
dTopCenter.Height = dTopLeft.Height;
sTopCenter.Location = new Point(sTopLeft.Right, sTopLeft.Top);
sTopCenter.Width = BgI.Width - 2 * side;
sTopCenter.Height = sTopLeft.Height;
/* Top Right */
dTopRight.Location = new Point(dTopCenter.Right, dTopCenter.Top);
dTopRight.Width = dTopRight.Height = side_control;
sTopRight.Location = new Point(sTopCenter.Right, sTopCenter.Top);
sTopRight.Width = sTopRight.Height = side;
/* Middle Top */
dMidLeft.Location = new Point(dTopLeft.Left, dTopLeft.Bottom);
dMidLeft.Width = dTopLeft.Width;
dMidLeft.Height = this.Height - 2 * side_control;
sMidLeft.Location = new Point(sTopLeft.Left , sTopLeft.Bottom );
sMidLeft.Width = sTopLeft.Width;
sMidLeft.Height = BgI.Height - 2 * side;
/* Middle Center */
dMidCenter.Location = new Point(dMidLeft.Right, dMidLeft.Top);
dMidCenter.Width = this.Size.Width - 2 * side_control;
dMidCenter.Height = this.Size.Height - 2 * side_control;
sMidCenter.Location = new Point(sMidLeft.Right , sMidLeft.Top);
sMidCenter.Width = BgI.Width - 2 * side;
sMidCenter.Height = BgI.Height - 2 * side;
/* Middle Right */
dMidRight.Location = new Point(dMidCenter.Right, dMidCenter.Top);
dMidRight.Width = side_control;
dMidRight.Height = this.Size.Height - 2 * side_control;
sMidRight.Location = new Point(sMidCenter.Right , sMidCenter.Top );
sMidRight.Width = side;
sMidRight.Height = BgI.Height - 2 * side;
/* Bottom Left*/
dBottomLeft.Location = new Point(dMidLeft.Left, dMidLeft.Bottom);
dBottomLeft.Width = dBottomLeft.Height = side_control;
sBottomLeft.Location = new Point(sMidLeft.Left , sMidLeft.Bottom );
sBottomLeft.Width = sBottomLeft.Height = side;
/* Bottom Center */
dBottomCenter.Location = new Point(dBottomLeft.Right, dBottomLeft.Top);
dBottomCenter.Width = this.Width - 2 * side_control;
dBottomCenter.Height = side_control;
sBottomCenter.Location = new Point(sBottomLeft.Right , sBottomLeft.Top );
sBottomCenter.Width = BgI.Width - 2 * side;
sBottomCenter.Height = side;
/* Bottom Right */
dBottomRight.Location = new Point(dBottomCenter.Right, dBottomCenter.Top);
dBottomRight.Width = dBottomRight.Height = side_control;
sBottomRight.Location = new Point(sBottomCenter.Right , sBottomCenter.Top );
sBottomRight.Width = sBottomRight.Height = side;
Image BG = (Image)BgImage;
e.Graphics.DrawImage(BG, dTopLeft, sTopLeft, units);
e.Graphics.DrawImage(BG, dTopCenter, sTopCenter, units);
e.Graphics.DrawImage(BG, dTopRight, sTopRight, units);
e.Graphics.DrawImage(BG, dMidLeft, sMidLeft, units);
e.Graphics.DrawImage(BG, dMidCenter, sMidCenter, units);
e.Graphics.DrawImage(BG, dMidRight, sMidRight, units);
e.Graphics.DrawImage(BG, dBottomLeft, sBottomLeft, units);
e.Graphics.DrawImage(BG, dBottomCenter, sBottomCenter, units);
e.Graphics.DrawImage(BG, dBottomRight, sBottomRight, units);
}
}
protected override void OnPaint(PaintEventArgs e)
{
e、 Graphics.textrendinghint=
System.Drawing.Text.TextRenderingHint.AntiAlias;
e、 图形插值模式=
System.Drawing.Drawing2D.InterpolationMode.HighQuality双线性;
e、 Graphics.PixelOffsetMode=
System.Drawing.Drawing2D.PixelOffsetMode.High Quality;
e、 图形平滑模式=
System.Drawing.Drawing2D.SmoothingMode.High Quality;
GraphicsUnits=GraphicsUnits.Pixel;
如果(this.Is9Slice==UISlice.no&&this.UIBackImage.UIImagePath!=null&&this.UIBackImage.UIImagePath!=“”)
{
位图BgI=新位图(this.backImage.UIImagePath);
矩形dest=新矩形(0,0,this.Width,this.Height);
矩形src=Rectangle.Empty;
if(backImage.UIUVRectangle.Width*backImage.UIUVRectangle.Height!=0)
{
src=backImage.UIUVRectangle;
}
其他的
{
src=新矩形(0,0,BgI.Width,BgI.Height);
}
e、 图形.绘图图像(BgI、dest、src、单位);
返回;
}
if(this.UIBackImage.UIImagePath!=null&&this.UIBackImage.UIImagePath!=“”)
{
位图BgImage=新位图(this.backImage.UIImagePath);
大小BgI=新大小(UIBackImage.UIUVRectangle.Width,UIBackImage.UIUVRectangle.Height);
点偏移=新点(UIBackImage.UIUVRectangle.X,UIBackImage.UIUVRectangle.Y);
矩形dTopLeft、dTopCenter、dTopRight、//目标矩形
dMidLeft、dMidCenter、dMidRight、,
dBottomLeft、dBottomCenter、dBottomRight;
矩形sTopLeft、sTopCenter、sTopRight、//源矩形
sMidLeft,sMidCenter,sMidRight,
sBottomLeft、sBottomCenter、sBottomRight;
内部侧控制;
int side=side_control=(int)(BgI.Width此.Size.Width | | 2*边>此.Size.Height)
side_control=(int)(this.Width[Browsable(true), Category("Custom controls"), Description("Foreground Image of Control")]
public UIImageRes UIForeImage
{
get { return foreImage; }
set { foreImage = value; Invalidate(); }
}
if (this.Is9Slice == UISlice.no && this.UIForeImage.UIImagePath != null && this.UIForeImage.UIImagePath !="" )
{
// Add foreground drawing code here
}
if (this.UIForeImage.UIImagePath != null && this.UIForeImage.UIImagePath != "")
{
// Add foreground drawing code here
}