繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Windows开发 >> 自定义按钮(转)

自定义按钮(转)

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:29  文字大小:【】【】【
简介://这个用户自定义控件继承自Button类 //实现了在按纽左侧显示图片的任务 //和点击后显示不同样式的功能 //下面就是人家的了 //本人加如了一些注释 using System; using System.Windows.Forms; using System.Dra...
关键字:按钮

//这个用户自定义控件继承自Button类

//实现了在按纽左侧显示图片的任务

//和点击后显示不同样式的功能

//下面就是人家的了

//本人加如了一些注释

using System;

using System.Windows.Forms;

using System.Drawing;

namespace NETToggle

{

///

/// Summary description for customFlatButton.

///

public class customFlatButton : System.Windows.Forms.Button

{

//used to set the type of image that is displayed with the button

public enum TOGGLE_TYPE{ none, XML_TOGGLE, HTML_TOGGLE };//显示类型

private bool bSelected = false;//是否被选择

private System.Windows.Forms.ImageList flatBtnImageList;//从工具栏拖进来的了

//他的一些东西有vs.net工具自动生成

private System.ComponentModel.IContainer components;

private int toggleType = 0;

public int ToggleType

{

get{ return toggleType; }

set{ toggleType = value; }

}

public bool bChangeSelection

{

get{ return bSelected; }

set{ bSelected = value; Invalidate(); }

}

public customFlatButton()

{

InitializeComponent();

}

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(customFlatButton));

this.flatBtnImageList = new System.Windows.Forms.ImageList(this.components);

//

// flatBtnImageList

//

this.flatBtnImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;

this.flatBtnImageList.ImageSize = new System.Drawing.Size(9, 9);

this.flatBtnImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("flatBtnImageList.ImageStream")));

this.flatBtnImageList.TransparentColor = System.Drawing.Color.Transparent;

//

// customFlatButton

//

this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.Size = new System.Drawing.Size(57, 19);

}

protected override void OnMouseEnter( System.EventArgs e )

{

return;

}

protected override void OnClick( System.EventArgs e)

{

bSelected = true;//触发bChangeSelection的写属性--调用this.Invalidate();--引起对OnPaint()的调用

base.OnClick(e);

}

protected override void OnMouseDown( System.Windows.Forms.MouseEventArgs e)

{

OnClick(e);

}

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )

{

Rectangle rect = e.ClipRectangle;

Graphics g = e.Graphics;

if( bSelected )

{

g.FillRectangle( new SolidBrush(Color.White), rect );

g.DrawString( this.Text, this.Font, new SolidBrush(this.ForeColor), 20, (float)2.5 );

g.DrawRectangle( new Pen(System.Drawing.SystemColors.Highlight, 2), rect );

}

else

{

g.FillRectangle( new SolidBrush(System.Drawing.SystemColors.Control), rect );

g.DrawString( this.Text, this.Font, new SolidBrush(this.ForeColor), 20, (float)2.5 );

}

//now draw the correct image if one is specified

if( toggleType == (int)TOGGLE_TYPE.XML_TOGGLE )

g.DrawImage( this.flatBtnImageList.Images[0], 8, 5 );

else if( toggleType == (int)TOGGLE_TYPE.HTML_TOGGLE )

g.DrawImage( this.flatBtnImageList.Images[1], 7, 5 );

}

}

}

责任编辑:admin
相关文章