C#怎么写一个圆形或者椭圆型的自定义控件 50
推荐于2018-04-12 · 知道合伙人互联网行家
关注
展开全部
开发的新的控件,一般继承自Control,重写OnPaint方法;还要自己写添加事件、处理消息等等。这样的控件,对应你的业务可以达到很好的效果,功能最灵活。同时对开发人员要求也最高,一般要了解图形绘制GDI+以及API的一些知识。比如,我们需要一个类似Label的控件,但是不需要Label那么多的属性和方法。那么就自己开发一个类似Label的自定义控件。
如下代码:直接继承自Control,其它代码会自动生成好。
[csharp] view plaincopyprint?
[ToolboxItem(true)]
public partial class CustomClassifyLabelItem : Control
{
private Color mColorOut = Color.FromArgb(255, 137, 37);
private StringFormat format;
private Color textColor;
private Font strFormat = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
public StringFormat Format
{
get
{
if (format == null)
{
format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.NoWrap;
format.Trimming = StringTrimming.EllipsisCharacter;
}
return format;
}
}
public Color TextColor
{
get
{
{
textColor = Color.FromArgb(22, 95, 162);
}
return textColor;
}
}
public CustomClassifyLabelItem()
{
InitializeComponent();
this.MouseEnter += new EventHandler(UCSelectClassifyItem_MouseEnter);
this.MouseLeave += new EventHandler(UCSelectClassifyItem_MouseLeave);
}
void UCSelectClassifyItem_MouseLeave(object sender, EventArgs e)
{
strFormat = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
Invalidate();
}
void UCSelectClassifyItem_MouseEnter(object sender, EventArgs e)
{
strFormat = new Font("宋体", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics graphics = pe.Graphics;
using (SolidBrush b = new SolidBrush(TextColor))
{
graphics.DrawString(this.Text, strFormat, b, new Rectangle(0, 0, this.Width, this.Height), Format);
}
graphics.Dispose();
}
}
如下代码:直接继承自Control,其它代码会自动生成好。
[csharp] view plaincopyprint?
[ToolboxItem(true)]
public partial class CustomClassifyLabelItem : Control
{
private Color mColorOut = Color.FromArgb(255, 137, 37);
private StringFormat format;
private Color textColor;
private Font strFormat = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
public StringFormat Format
{
get
{
if (format == null)
{
format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.NoWrap;
format.Trimming = StringTrimming.EllipsisCharacter;
}
return format;
}
}
public Color TextColor
{
get
{
{
textColor = Color.FromArgb(22, 95, 162);
}
return textColor;
}
}
public CustomClassifyLabelItem()
{
InitializeComponent();
this.MouseEnter += new EventHandler(UCSelectClassifyItem_MouseEnter);
this.MouseLeave += new EventHandler(UCSelectClassifyItem_MouseLeave);
}
void UCSelectClassifyItem_MouseLeave(object sender, EventArgs e)
{
strFormat = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
Invalidate();
}
void UCSelectClassifyItem_MouseEnter(object sender, EventArgs e)
{
strFormat = new Font("宋体", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics graphics = pe.Graphics;
using (SolidBrush b = new SolidBrush(TextColor))
{
graphics.DrawString(this.Text, strFormat, b, new Rectangle(0, 0, this.Width, this.Height), Format);
}
graphics.Dispose();
}
}
展开全部
自己重写控件的OnPaint方法,或者贴图,不过不管哪种方法都很麻烦,即使使用WPF做,也很麻烦,需要自己写控件模版,如果控件是按钮那种有动态效果的,那就更麻烦了,所以还是建议少做这种事情。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C#下有个开源项目叫CircleDock 他是教你怎么做圆形界面的.
你搜搜CircleDock0.9.2Alpha8.2 这个名称. 原理不难 看看它的源码是怎么实现就可以了.
你搜搜CircleDock0.9.2Alpha8.2 这个名称. 原理不难 看看它的源码是怎么实现就可以了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
this.Size = new Size(100, 200);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(base.BackColor), 0.0f, 0.0f, 100, 200);
g.DrawEllipse(new Pen(Color.Black,1), 0.0f, 0.0f, 99, 199);
}
}
}
简单的,重写OnPaint
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
this.Size = new Size(100, 200);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(base.BackColor), 0.0f, 0.0f, 100, 200);
g.DrawEllipse(new Pen(Color.Black,1), 0.0f, 0.0f, 99, 199);
}
}
}
简单的,重写OnPaint
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
GraphicsPath myPath = new GraphicsPath();
myPath.AddEllipse(0, 0, 50, 50);
this.button1.Region = new Region(myPath);
用这个就行了,想要什么形状就什么形状
myPath.AddEllipse(0, 0, 50, 50);
this.button1.Region = new Region(myPath);
用这个就行了,想要什么形状就什么形状
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询