当前位置:编程学习 > C#/ASP.NET >>

自定义按钮(转)

答案://这个用户自定义控件继承自Button类
//实现了在按纽左侧显示图片的任务
//和点击后显示不同样式的功能
//下面就是人家的了
//本人加如了一些注释

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 };//显示类型

        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 )
&n

上一个:关于在DataGrid里添加ComboBox(一)
下一个:Dotnet WinForm 建立 FAQ1(转贴)(编程技巧)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,