///
/// Button.cs
///
/// by Dave Peckham
/// August 2002
/// Irvine, California
///
/// A button that emulates the Apple Aqua user interface guidelines.
/// This button grows horizontally to accomodate its label. Button
/// height is fixed.
/// using System;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace nt.util
{
[Description( "苹果风格菜单" )]
[Designer(typeof (nt.util.NiceButtonDesigner))]
public class NiceButton : System.Windows.Forms.Button
{
#region Class Constants protected static int ButtonDefaultWidth = 80; // Set this to the height of your source bitmaps
protected static int ButtonHeight = 30; // If your source bitmaps have shadows, set this
// to the shadow size so DrawText can position the
// label appears centered on the label
protected static int ButtonShadowOffset = 5; // These settings approximate the pulse effect
// of buttons on Mac OS X
protected static int PulseInterval = 70;
protected static float PulseGammaMax = 1.8f;
protected static float PulseGammaMin = 0.7f;
protected static float PulseGammaShift = 0.2f;
protected static float PulseGammaReductionThreshold = 0.2f;
protected static float PulseGammaShiftReduction = 0.5f; #endregion
#region Member Variables // Timer and gamma to control pulsing
protected Timer timer;
protected float gamma, gammaShift; protected ButtonState buttonState;
protected bool mousePressed;
protected bool pulse = false;
protected bool sizeToLabel = true;
protected bool mouseInsideButton; // Images used to draw the button
protected Image imgLeft, imgFill, imgRight; // Rectangles to position images on the button face
protected Rectangle rcLeft, rcRight; // Matrices for transparency transformation
protected ImageAttributes iaDefault, iaNormal;
protected ColorMatrix cmDefault, cmNormal; #endregion
#region Constructors and Initializers public NiceButton()
{
InitializeComponent();
} private void InitializeComponent()
{
} #endregion
#region Properties [Description( "Determines whether the button pulses. Note that only the default button pulses." )]
[Category( "Appearance" )]
[DefaultValue( false )]
public bool Pulse
{
get { return pulse; }
set { pulse = value; }
} [Description( "Determines whether the button should automatically size to fit the label" )]
[Category( "Layout" )]
[DefaultValue( true )]
public bool SizeToLabel
{
get { return sizeToLabel; }
set
{
sizeToLabel = value;
OnTextChanged( EventArgs.Empty );
}
} #endregion
#region Property overrides /* AquaButton has a fixed height */
protected override Size DefaultSize
{
get
{
return new Size( NiceButton.ButtonDefaultWidth,
NiceButton.ButtonHeight ); }
} /* Shadow Control.Width to make it browsable */
[Description( "See also: SizeToLabel" )]
[Category( "Layout" )]
[Browsable( true )]
public new int Width
{
get { return base.Width; }
set { base.Width = value; }
} /* Shadow Control.Height to make it browsable and read only */
[Description( "Aqua buttons have a fixed height" )]
[Category( "Layout" )]
[Browsable( true )]
[ReadOnly( true )]
public new int Height { get { return base.Height; } } #endregion
#region Method overrides protected override void OnCreateControl()
{
LoadImages();
InitializeGraphics();
StartPulsing();
} protected override void OnTextChanged( EventArgs e )
{
if (sizeToLabel)
{
Graphics g = this.CreateGraphics( );
SizeF sizeF = g.MeasureString( Text, Font );
Width = imgLeft.Width + (int)sizeF.Width + imgRight.Width;
g.Dispose();
}
Invalidate( );
Update( );
base.OnTextChanged( e );
} protected override void OnPaint( PaintEventArgs e )
{
Graphics g = e.Graphics;
g.Clear(Parent.BackColor);
Draw( g );
} protected override void OnMouseDown( MouseEventArgs e )
{
if( e.Button == MouseButtons.Left )
{
Focus( );
Capture = true;
buttonState = ButtonState.Pushed;
mousePressed = true;
mouseInsideButton = true;
StopPulsing( );
Invalidate( );
Update( );
}
else
base.OnMouseDown( e );
} protected override void OnMouseUp( MouseEventArgs e )
{
if( mousePressed && e.Button == MouseButtons.Left )
{
Capture = false;
buttonState = ButtonState.Normal;
StartPulsing( );
Invalidate( );
Update( );
}
else
base.OnMouseUp( e ); mousePressed = false;
} protected override void OnMouseMove( MouseEventArgs e )
{
if( e.Button == MouseButtons.Left )
{
bool bInside = ClientRectangle.Contains( e.X, e.Y );
if (!mouseInsideButton && bInside )
{
mouseInsideButton = true;
Invalidate( );
Update( );
}
else if (mouseInsideButton && !bInside )
{
mouseInsideButton = false;
Invalidate( );
Update( );
}
}
else
base.OnMouseDown( e );
} #endregion
#region Implementation protected virtual void LoadImages ()
{
imgLeft = new Bitmap( GetType(), "left.png" );
imgRight = new Bitmap( GetType(), "right.png" );
imgFill = new Bitmap( GetType(), "fill.png" );
} protected virtual void InitializeGraphics ()
{
// Rectangles for placing images relative to the client rectangle
rcLeft = new Rectangle( 0, 0, imgLeft.Width, imgLeft.Height );
rcRight = new Rectangle( 0, 0, imgRight.Width, imgRight.Height ); // Image attributes used to lighten default buttons cmDefault = new ColorMatrix();
cmDefault.Matrix33 = 0.5f; // reduce opacity by 50% iaDefault = new ImageAttributes();
iaDefault.SetColorMatrix( cmDefault, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap );
// Image attributes that lighten and desaturate normal buttons
cmNormal = new ColorMatrix(); // desaturate the image
cmNormal.Matrix00 = 1/3f;
cmNormal.Matrix01 = 1/3f;
cmNormal.Matrix02 = 1/3f;
cmNormal.Matrix10 = 1/3f;
cmNormal.Matrix11 = 1/3f;
cmNormal.Matrix12 = 1/3f;
cmNormal.Matrix20 = 1/3f;
cmNormal.Matrix21 = 1/3f;
cmNormal.Matrix22 = 1/3f;
cmNormal.Matrix33 = 0.5f; // reduce opacity by 50% iaNormal = new ImageAttributes();
iaNormal.SetColorMatrix( cmNormal, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap );
} protected virtual void StartPulsing ()
{
if ( this.Pulse && !this.DesignModeDetected( ) )
{
timer = new Timer( );
timer.Interval = NiceButton.PulseInterval;
timer.Tick += new EventHandler( TimerOnTick );
gamma = NiceButton.PulseGammaMax;
gammaShift = -NiceButton.PulseGammaShift;
timer.Start();
}
}
protected virtual void StopPulsing ()
{
if ( timer != null )
{
iaDefault.SetGamma( 1.0f, ColorAdjustType.Bitmap );
timer.Stop();
}
} protected virtual void Draw( Graphics g )
{
DrawButton( g );
DrawText( g );
} protected virtual void DrawButton( Graphics g )
{
// Update our destination rectangles
rcRight.X = this.Width - imgRight.Width; if ( buttonState == ButtonState.Pushed )
{
if ( mouseInsideButton )
DrawButtonState( g, iaDefault );
else
DrawButtonState( g, iaNormal );
}
else if (IsDefault)
DrawButtonState( g, iaDefault );
else
DrawButtonState( g, iaNormal );
} protected virtual void DrawButtonState (Graphics g, ImageAttributes ia)
{
TextureBrush tb; // Draw the left and right endcaps
g.DrawImage( imgLeft, rcLeft, 0, 0,
imgLeft.Width, imgLeft.Height, GraphicsUnit.Pixel, ia ); g.DrawImage( imgRight, rcRight, 0, 0,
imgRight.Width, imgRight.Height, GraphicsUnit.Pixel, ia ); // Draw the middle
tb = new TextureBrush( imgFill,
new Rectangle( 0, 0, imgFill.Width, imgFill.Height ), ia );
tb.WrapMode = WrapMode.Tile; g.FillRectangle ( tb, imgLeft.Width, 0,
this.Width - (imgLeft.Width + imgRight.Width),
imgFill.Height); tb.Dispose( );
} protected virtual void DrawText( Graphics g )
{
RectangleF layoutRect =
new RectangleF( 0, 0, this.Width,
this.Height - NiceButton.ButtonShadowOffset ); int LabelShadowOffset = 1; StringFormat fmt = new StringFormat( );
fmt.Alignment = StringAlignment.Center;
fmt.LineAlignment = StringAlignment.Center; // Draw the shadow below the label
layoutRect.Offset( 0, LabelShadowOffset );
SolidBrush textShadowBrush = new SolidBrush( Color.Gray );
g.DrawString( Text, Font, textShadowBrush, layoutRect, fmt );
textShadowBrush.Dispose( ); // and the label itself
layoutRect.Offset( 0, -LabelShadowOffset );
SolidBrush brushFiller = new SolidBrush( Color.Black );
g.DrawString( Text, Font, brushFiller, layoutRect, fmt );
brushFiller.Dispose( );
} protected virtual void TimerOnTick( object obj, EventArgs e)
{
// set the new gamma level
if ((gamma - NiceButton.PulseGammaMin < NiceButton.PulseGammaReductionThreshold ) ||
(NiceButton.PulseGammaMax - gamma < NiceButton.PulseGammaReductionThreshold ))
gamma += gammaShift * NiceButton.PulseGammaShiftReduction;
else
gamma += gammaShift; if ( gamma <= NiceButton.PulseGammaMin || gamma >= NiceButton.PulseGammaMax )
gammaShift = -gammaShift; iaDefault.SetGamma( gamma, ColorAdjustType.Bitmap ); Invalidate( );
Update( );
} protected virtual bool DesignModeDetected()
{
// base.DesignMode always returns false, so try this workaround
IDesignerHost host =
(IDesignerHost) this.GetService( typeof( IDesignerHost ) ); return ( host != null );
} #endregion
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace nt
{
/// <summary>
/// loginForm 的摘要说明。
/// </summary>
public class loginForm : System.Windows.Forms.Form
{
private nt.util.NiceButton button1;
private nt.util.NiceButton niceButton1;
private System.Windows.Forms.TextBox tb1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public loginForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new nt.util.NiceButton();
this.niceButton1 = new nt.util.NiceButton();
this.tb1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.AccessibleRole = System.Windows.Forms.AccessibleRole.ScrollBar;
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.Location = new System.Drawing.Point(56, 160);
this.button1.Name = "button1";
this.button1.Pulse = true;
this.button1.TabIndex = 1;
this.button1.Text = "登陆";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// niceButton1
//
this.niceButton1.AccessibleRole = System.Windows.Forms.AccessibleRole.ScrollBar;
this.niceButton1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.niceButton1.Location = new System.Drawing.Point(176, 160);
this.niceButton1.Name = "niceButton1";
this.niceButton1.Pulse = true;
this.niceButton1.TabIndex = 2;
this.niceButton1.Text = "取消";
//
// tb1
//
this.tb1.Location = new System.Drawing.Point(48, 56);
this.tb1.Name = "tb1";
this.tb1.Size = new System.Drawing.Size(192, 21);
this.tb1.TabIndex = 3;
this.tb1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 128);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button1_Click);
//
// loginForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.tb1);
this.Controls.Add(this.niceButton1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "loginForm";
this.Text = "loginForm";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("---------------");
if(this.tb1.Text==null){
this.tb1.Text = "";
}
this.tb1.Text = NTObj.MD5(this.tb1.Text);
} }
}
干什么代码一摆,就不说明一下出现问题的地方呀。
传递事件没有做过,如果你想调用别的事件可以这样做:
private void button1_Click(object sender, System.EventArgs e)
{
//你要实现的功能
}
private void button2_Click(object sender, System.EventArgs e)
{
button1_Click(sender,e);//调用button1_Click()事件.
}
//还要声明一个事件。
//如下:
public delegate void YourNiceBtnClickedEventHandler(object sender,EventArgs e);
public class YourNiceBtn
{
public event YourNiceBtnClickedEventHandler YourNiceBtnClickOccured;
......
//增加一个事件激发函数
public void EventOccured()
{
if(YourNiceBtnClickOccured!=null)
YourNiceBtnClickOccured(this,new EventArgs());
}
//假设你想通过在调用该类的某个方法时发生该事件实现如下:
public string GetNiceBtnName()
{
EventOccured();
......
}
}
//使用该组件
public AppRun
{
private YourNiceBtn btn;
//构造函数
public AppRun()
{
btn = new YourNiceBtn();
//注册事件
btn.YourNiceBtnClickOccured +=new YourNiceBtnClickedEventHandler(YourClickMethod);
}
//事件发生处理函数,当btn.GetNiceBtnName()方法被调用时就会发生事件;
//楼主也可以直接调用btn.EventOccured()
private void YourClickMethod(object sender, EventArgs e)
{
//处理
}
}
//完成