提供一个测试用的“平面按纽”控件代码!如想看结果,请自行编译

发表于:2007-06-30来源:作者:点击数: 标签:
环境:win2000 server 、DOTNET beta2 本程序旨在练习,请朋友们多指教 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System. Windows .Forms; namespace SohoContral { /// summa
环境:win2000 server 、DOTNET beta2

本程序旨在练习,请朋友们多指教


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace SohoContral
{
    /// <summary>
    /// 定义平面格式的按扭
    /// </summary>
    public class FlatButton : System.Windows.Forms.UserControl
    {
        private string _text="";                //定义按纽文字

        public string Text{
            set{_text=value;}
            get{return _text;}
        }

        
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public FlatButton()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if( components != null )
                    components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.label1.Location = new System.Drawing.Point(120, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(48, 24);
            this.label1.TabIndex = 1;
            this.label1.Text = "张成";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
            this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(40, 8);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(56, 24);
            this.button1.TabIndex = 0;
            this.button1.Text = "测试";
            this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
            this.button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
            this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
            //
            // FlatButton
            //
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label1,
                                                                          this.button1});
            this.Name = "FlatButton";
            this.Size = new System.Drawing.Size(280, 104);
            this.Resize += new System.EventHandler(this.UserControl1_Resize);
            this.Load += new System.EventHandler(this.UserControl1_Load);
            this.ResumeLayout(false);

        }
        #endregion


        private void UserControl1_Resize(object sender,System.EventArgs e)
        {
            this.label1.Width= this.button1.Width   =this.Width ;
            this.label1.Height = this.button1.Height  =this.Height ;
            this.label1.Location =this.button1.Location =new System.Drawing.Point(0);
        }
        private void UserControl1_Load(object sender, System.EventArgs e)
        {
            
            if(this._text!="")
            {
                this.button1.Text   =this.label1.Text =this._text   ;
            }
                
            this.button1.FlatStyle=System.Windows.Forms.FlatStyle.Popup  ;
            this.label1.BorderStyle=System.Windows.Forms.BorderStyle.None ;
            this.button1.Visible=false;
        }

        
        private void label1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            this.label1.Visible=false;
            this.button1.Visible=true;
        }
        private void button1_MouseLeave(object sender,System.EventArgs e)
        {
            this.label1.BorderStyle=System.Windows.Forms.BorderStyle.None  ;
            this.button1.Visible=false;
            this.label1.Visible=true;
        }

        private void button1_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            this.button1.Visible=false;
            this.label1.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D ;
            this.label1.Visible=true;
        }

        private void button1_MouseUp(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            this.button1.Visible=false;
            this.label1.BorderStyle=System.Windows.Forms.BorderStyle.None  ;
            this.label1.Visible=true;
            
        }

        private void label1_MouseUp(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            this.label1.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D ;
            this.button1.Visible=false;
            this.label1.Visible=true;
        }
    }
}

原文转自:http://www.ltesting.net