自定義按鈕(轉(zhuǎn))
發(fā)表時(shí)間:2024-06-20 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]//這個(gè)用戶自定義控件繼承自Button類(lèi)//實(shí)現(xiàn)了在按紐左側(cè)顯示圖片的任務(wù)//和點(diǎn)擊后顯示不同樣式的功能//下面就是人家的了//本人加如了一些注釋using System;using System.Windows.Forms;using System.Drawing;namespace NETT...
//這個(gè)用戶自定義控件繼承自Button類(lèi)
//實(shí)現(xiàn)了在按紐左側(cè)顯示圖片的任務(wù)
//和點(diǎn)擊后顯示不同樣式的功能
//下面就是人家的了
//本人加如了一些注釋
using System;
using System.Windows.Forms;
using System.Drawing;
namespace NETToggle
{
/// <summary>
/// Summary description for customFlatButton.
/// </summary>
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 };//顯示類(lèi)型
private bool bSelected = false;//是否被選擇
private System.Windows.Forms.ImageList flatBtnImageList;//從工具欄拖進(jìn)來(lái)的了
//他的一些東西有vs.net工具自動(dòng)生成
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;//觸發(fā)bChangeSelection的寫(xiě)屬性--調(diào)用this.Invalidate();--引起對(duì)OnPaint()的調(diào)用
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 );
}
}
}