参考了这篇文章:
http://www.cnblogs.com/qianlifeng/archive/2010/04/13/1710869.html
我的代码:
JButton.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Reflection;
- namespace JiaYiControls
- {
- [DefaultEvent("Click")]
- public partial class JButton : UserControl
- {
- private Image _normalImage = null;
- private Image _hoverImage = null;
- private Image _downImage = null;
- public JButton()
- {
- _normalImage=Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"JiaYiControls.assert.button.normal.bmp"));
- (_normalImage as Bitmap).MakeTransparent(Color.FromArgb(255, 0, 255));
- _hoverImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"JiaYiControls.assert.button.hover.bmp"));
- (_hoverImage as Bitmap).MakeTransparent(Color.FromArgb(255, 0, 255));
- _downImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"JiaYiControls.assert.button.down.bmp"));
- (_downImage as Bitmap).MakeTransparent(Color.FromArgb(255, 0, 255));
- InitializeComponent();
- this.BackgroundImage = _normalImage;
- }
- [Category("JiaYiControls")]
- public Image NormalImage
- {
- get { return _normalImage; }
- set { _normalImage = value; }
- }
- [Category("JiaYiControls")]
- public Image HoverImage
- {
- get { return _hoverImage; }
- set { _hoverImage = value; }
- }
- [Category("JiaYiControls")]
- public Image DownImage
- {
- get { return _downImage; }
- set { _downImage = value; }
- }
- [Category("JiaYiControls")]
- public string Caption
- {
- get { return this.label1.Text; }
- set { this.label1.Text = value; }
- }
- private void label1_Click(object sender, EventArgs e)
- {
- this.OnClick(e);
- }
- private void label1_MouseUp(object sender, MouseEventArgs e)
- {
- this.BackgroundImage = _normalImage;
- }
- private void label1_MouseDown(object sender, MouseEventArgs e)
- {
- this.BackgroundImage = _downImage;
- }
- private void label1_MouseHover(object sender, EventArgs e)
- {
- this.BackgroundImage = _hoverImage;
- }
- private void label1_MouseLeave(object sender, EventArgs e)
- {
- this.BackgroundImage = _normalImage;
- }
- }
- }
设计器产生的代码:
- namespace JiaYiControls
- {
- partial class JButton
- {
- /// <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.label1 = new System.Windows.Forms.Label();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.BackColor = System.Drawing.Color.Transparent;
- this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.label1.Location = new System.Drawing.Point(0, 0);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(78, 30);
- this.label1.TabIndex = 0;
- this.label1.Text = "label1";
- this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- this.label1.MouseLeave += new System.EventHandler(this.label1_MouseLeave);
- this.label1.Click += new System.EventHandler(this.label1_Click);
- this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
- this.label1.MouseHover += new System.EventHandler(this.label1_MouseHover);
- this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
- //
- // JButton
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.Color.Transparent;
- this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
- this.Controls.Add(this.label1);
- this.Name = "JButton";
- this.Size = new System.Drawing.Size(78, 30);
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Label label1;
- }
- }
只是测试用的代码而已。
注意在ps里图片,透明部分,这里颜色是RGB(255,0,255).
测试下控件:
转载于:https://blog.51cto.com/xcf007/432867