首页 > 其他 > 详细

自定义控件

时间:2019-05-06 15:58:22      阅读:128      评论:0      收藏:0      [点我收藏+]

在WinForm项目上右键——>添加——>新建项——>找到Windows Forms ——>点击自定义控件——>命名——>添加——>找到定义的控件(命名.cs)——>双击打开——>删除其中的(命名.Designer.cs)的文件——>找到命名文件双击打开——>删除public partial class中的partial以及public MyButtom()
{
    InitializeComponent();
}中的InitializeComponent();

 

 自定义空间的参考

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm测试
{
public class MyButton : Control
{
public MyButton()
{

}

protected override void OnPaint(PaintEventArgs pe)
{
//base.OnPaint(pe);
Graphics g = pe.Graphics;

if (backgroundImageM != null) //判断是否有背景图
{
g.DrawImage(backgroundImageM, this.ClientRectangle); //画出背景图参数1背景图,参数2默认的矩形
}
g.FillRectangle(new SolidBrush(backColorM), this.ClientRectangle); //画背景参数1实心的backColorM颜色的,参数2默认的矩形
g.DrawString(textM, fontM, new SolidBrush(FontMColor), this.ClientRectangle); //画文字参数1要画的文字,参数2文字的大小等,参数3文字的颜色,参数4默认的矩形
}

/// <summary>
/// 控件的默认图片
/// </summary>
private Image backgroundImageM = null;
[Description("控件的默认图片")] //在属性中点击此属性的提示
public Image BackgroundImageM
{
get { return backgroundImageM; }
set
{
backgroundImageM = value;
}
}

/// <summary>
/// 控件的背景色
/// </summary>
private Color backColorM = Color.Transparent;
[Description("控件的背景色")]
public Color BackColorM
{
get { return backColorM; }
set
{
backColorM = value;
}
}

/// <summary>
/// 用于显示文本的字体
/// </summary>
private Font fontM = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
[Description("用于显示文本的字体")]
public Font FontM
{
get { return fontM; }
set
{
fontM = value;
}
}

/// <summary>
/// 文字的颜色
/// </summary>
private Color fontMColor = Color.Black;
[Description("文字的颜色")]
public Color FontMColor
{
get { return fontMColor; }
set
{
fontMColor = value;
}
}

/// <summary>
/// 控件的文字提示
/// </summary>
private string textM = "";
[Description("显示的文字")]
public string TextM
{
get { return textM; }
set
{
textM = value;
}
}

}
}

自定义控件

原文:https://www.cnblogs.com/jmy9/p/10820086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!