·首页 ·asp ·.net ·php ·JSP ·CGI ·数据库 ·网页设计 ·网管专栏 ·XML ·工具软件 ·办公软件 ·操作系统 ·程序设计 ·LINUX 
  当前位置: 普克>>计算机教程>>.net>>Windows开发>>自定义按钮(转)
flash视频教学

photoshop专题

asp.net专题

office专题

自定义按钮(转)


Windows开发 发表时间:2006-4-8 字体:  返回
//这个用户自定义控件继承自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 )
                g.DrawImage( this.flatBtnImageList.Images[0], 8, 5 );
            else if( toggleType == (int)TOGGLE_TYPE.HTML_TOGGLE )
                g.DrawImage( this.flatBtnImageList.Images[1], 7, 5 );
        }
    }
}


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

普克创业投资网刊载此文不代表同意其说法或描述,仅为提供更多信息。
在百度中搜索自定义按钮(转)的相关内容]   [在狗狗中搜索自定义按钮(转)的相关内容]
Copyright @ 2006 PUPK.COM 普克创业投资网 版权所有
 建议使用1024*768以达到最好的浏览效果