您的位置:首页 > 其它

Winform自定义控件—Button(上)

2015-12-18 00:31 351 查看
在开发中用的最多的相信就是Button控件,但是Button本身是在是太丑陋了,自己还背景图还会产生"黑线",为了使用方便,并且美观,我们采用迂回的方式来实现Button的效果。

在这里使用UserControl+Label进行封装

先来看代码:

ButtonM.cs

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace landptf.controls
{
public partial class ButtonM : UserControl
{

public ButtonM()
{
InitializeComponent();
}
/// <summary>
/// 控件的默认图片
/// </summary>
private Image imageM = null;
[Description("控件的默认图片")]
public Image ImageM
{
get { return imageM; }
set
{
imageM = value;
label.Image = imageM;
}
}
/// <summary>
/// 光标移动到控件上方显示的图片
/// </summary>
private Image imageMove = null;
[Description("光标移动到控件上方显示的图片")]
public Image ImageMove
{
get { return imageMove; }
set { imageMove = value; }
}
/// <summary>
/// 光标离开控件显示的图片
/// </summary>
private Image imageLeave = null;
[Description("光标离开控件显示的图片")]
public Image ImageLeave
{
get { return imageLeave; }
set { imageLeave = value; }
}
/// <summary>
/// 控件的背景色
/// </summary>
private Color backColorM = Color.Transparent;
[Description("控件的背景色")]
public Color BackColorM
{
get { return backColorM; }
set
{
backColorM = value;
label.BackColor = backColorM;
}
}
/// <summary>
/// 光标移动到控件上方显示的颜色
/// </summary>
private Color backColorMove = Color.Transparent;
[Description("光标移动到控件上方显示的颜色")]
public Color BackColorMove
{
get { return backColorMove; }
set { backColorMove = value; }
}
/// <summary>
/// 光标离开控件显示的背景色
/// </summary>
private Color backColorLeave = Color.Transparent;
[Description("光标离开控件显示的背景色")]
public Color BackColorLeave
{
get { return backColorLeave; }
set { backColorLeave = value; }
}
/// <summary>
/// 控件的文字提示
/// </summary>
private string textM = "";
[Description("显示的文字")]
public string TextM
{
get { return textM; }
set
{
textM = value;
label.Text = textM;
}
}
/// <summary>
/// 文字的颜色
/// </summary>
private Color textColor = Color.Black;
[Description("文字的颜色")]
public Color TextColor
{
get { return textColor; }
set
{
textColor = value;
label.ForeColor = textColor;
}
}
/// <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;
label.Font = fontM;
}
}
/// <summary>
/// 单击事件
/// </summary>
public event EventHandler ButtonClick;
/// <summary>
/// 单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label_Click(object sender, EventArgs e)
{
if (ButtonClick != null)
{
ButtonClick(sender, e);
}
}

private void label_MouseMove(object sender, MouseEventArgs e)
{
if (backColorMove != Color.Transparent)
{
BackColorM = backColorMove;
}
if (imageMove != null)
{
ImageM = imageMove;
}
}

private void label_MouseLeave(object sender, EventArgs e)
{
if (backColorLeave != Color.Transparent)
{
BackColorM = backColorLeave;
}
if (imageLeave != null)
{
ImageM = imageLeave;
}
}
}
}


ButtonM.Designer.cs

namespace landptf.controls
{
partial class ButtonM
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label
//
this.label.Dock = System.Windows.Forms.DockStyle.Fill;
this.label.Location = new System.Drawing.Point(0, 0);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(205, 69);
this.label.TabIndex = 0;
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label.Click += new System.EventHandler(this.label_Click);
this.label.MouseLeave += new System.EventHandler(this.label_MouseLeave);
this.label.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label_MouseMove);
//
// ButtonM
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.BackColor = System.Drawing.Color.Transparent;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.Controls.Add(this.label);
this.Name = "ButtonM";
this.Size = new System.Drawing.Size(205, 69);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Label label;
}
}


View Code
我们对外提供了设置背景图片,背景色,显示的文本,字体颜色,文本样式,点击事件。在开发中还会遇到其他需要的方法可自行完善。

明天我们在来看一下如何使用自定义控件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: