Button有没有按下的效果 能不能让Button点击一下就处于按下的状态,我印象中Delphi中SpeedButton好像有这种效果,C#中能不能实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 做两个Button, 一个按下状态, 一个正常状态, 初始正常状态, 按下状态不可见, 按下后,正常状态不可见, 按下状态可见 Button的样式设置成Popup有点像是按下得效果,好像不是很明显 估计要自己制作这样的效果。如果是webform就可以使用css了。 To:Jamestan(情商蛋蛋) 什么控件有这种效果? using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;using System.Diagnostics;using System.Drawing.Imaging;using System.ComponentModel;namespace AquaButtonTester{ /// <summary> /// Summary description for AquaButton. /// </summary> [Serializable] public class AquaButton : System.Windows.Forms.Button { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private bool pulseOnFocus; /// ///Additional variables to handle pulsing /// private ImageAttributes imgAttr = new ImageAttributes(); private float gamma; private float minGamma; private float maxGamma; private float gammaStep; private Timer pulseTimer = new Timer(); private Bitmap buttonBitmap; private Rectangle buttonBitmapRectangle; public AquaButton() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); mouseAction = MouseActionType.None; this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); //The following defaults are better suited to draw the text outline this.Font = new Font("Arial Black", 12, FontStyle.Bold); this.BackColor = Color.DarkTurquoise; this.Size = new Size(112, 48); //Initialize variables to Pulse button gamma = 1.0f; minGamma = 1.0f; maxGamma = 2.2f; gammaStep = .2f; pulseTimer.Interval = 90; pulseTimer.Tick +=new EventHandler(pulseTimer_Tick); } [DefaultValue(false)] public bool PulseOnFocus { get { return pulseOnFocus; } set { pulseOnFocus = value; Invalidate(); } } /// <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() { components = new System.ComponentModel.Container(); } #endregion private enum MouseActionType { None, Hover, Click } private MouseActionType mouseAction; protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(Color.White); Color clr = this.BackColor; int shadowOffset = 8; int btnOffset = 0; switch (mouseAction) { case MouseActionType.Click: shadowOffset = 4; clr = Color.Gold; btnOffset = 2; break; case MouseActionType.Hover: clr = Color.Gold; break; } g.SmoothingMode = SmoothingMode.AntiAlias; /// /// Create main colored shape /// Rectangle rc = new Rectangle(btnOffset,btnOffset,this.ClientSize.Width-8-btnOffset, this.ClientSize.Height-8-btnOffset); GraphicsPath path1 = this.GetPath(rc, 20); LinearGradientBrush br1 = new LinearGradientBrush(new Point(0, 0),new Point(0, rc.Height+6), clr, Color.White); /// /// Create shadow /// Rectangle rc2 = rc; rc2.Offset(shadowOffset, shadowOffset); GraphicsPath path2 = this.GetPath(rc2, 20); PathGradientBrush br2 = new PathGradientBrush(path2); br2.CenterColor = ControlPaint.DarkDark(Color.Silver); br2.SurroundColors = new Color[]{Color.White}; /// /// Create top water color to give "aqua" effect /// Rectangle rc3 = rc; rc3.Inflate(-5, -5); rc3.Height = 15; GraphicsPath path3 = GetPath(rc3, 20); LinearGradientBrush br3 = new LinearGradientBrush(rc3, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical); /// ///draw shapes /// g.FillPath(br2, path2); //draw shadow g.FillPath(br1, path1); //draw main g.FillPath(br3, path3); //draw top bubble /// ///Create a backup of the button image to a bitmap so we can manipulate it's pulsing action /// buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size); buttonBitmap = new Bitmap(buttonBitmapRectangle.Width, buttonBitmapRectangle.Height); Graphics g_bmp = Graphics.FromImage(buttonBitmap); g_bmp.SmoothingMode = SmoothingMode.AntiAlias; g_bmp.FillPath(br1, path1); g_bmp.FillPath(br3, path3); /// ///Set the region for the button Region rgn = new Region(path1); rgn.Union(path2); this.Region = rgn; /// /// Create a Path to draw the text to give the button a nice outline /// GraphicsPath path4 = new GraphicsPath(); RectangleF path1bounds = path1.GetBounds(); Rectangle rcText = new Rectangle((int)path1bounds.X, (int)path1bounds.Y, (int)path1bounds.Width, (int)path1bounds.Height); StringFormat strformat = new StringFormat(); strformat.Alignment = StringAlignment.Center; strformat.LineAlignment = StringAlignment.Center; path4.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size, rcText, strformat); Pen txtPen = new Pen(this.ForeColor, 1); g.DrawPath(txtPen, path4); g_bmp.DrawPath(txtPen, path4); } private GraphicsPath GetPath(Rectangle rc, int r) { int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height; GraphicsPath path = new GraphicsPath(); path.AddArc(x, y, r, r, 180, 90); //Upper left corner path.AddArc(x+w - r, y, r, r, 270, 90); //Upper right corner path.AddArc(x+w - r, y+h - r, r, r, 0, 90); //Lower right corner path.AddArc(x, y+h - r, r, r, 90, 90); //Lower left corner path.CloseFigure(); return path; } protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.mouseAction = MouseActionType.Click; this.Invalidate(); } base.OnMouseDown (e); } protected override void OnMouseUp(MouseEventArgs e) { if (this.Bounds.Contains(e.X, e.Y)) this.mouseAction = MouseActionType.Hover; else this.mouseAction = MouseActionType.None; this.Invalidate(); base.OnMouseUp (e); } protected override void OnMouseEnter(EventArgs e) { this.mouseAction = MouseActionType.Hover; this.Invalidate(); base.OnMouseEnter (e); } protected override void OnMouseLeave(EventArgs e) { this.mouseAction = MouseActionType.None; this.Invalidate(); base.OnMouseLeave (e); } //----------------------------------------------------------------- // METHODS TO HANDLE PULSING //----------------------------------------------------------------- protected override void OnGotFocus(EventArgs e) { base.OnGotFocus (e); if (this.pulseOnFocus) pulseTimer.Start(); } protected override void OnLostFocus(EventArgs e) { base.OnLostFocus (e); pulseTimer.Stop(); this.Invalidate(); //redraw to get back it's original picture } private void pulseTimer_Tick(object sender, EventArgs e) { if (this.Focused && pulseOnFocus && buttonBitmap != null) { gamma += gammaStep; if (gamma > this.maxGamma) gammaStep = - gammaStep; if (gamma < this.minGamma) gammaStep = Math.Abs(gammaStep); imgAttr.SetGamma(gamma); this.CreateGraphics().DrawImage(buttonBitmap, buttonBitmapRectangle, 0, 0, buttonBitmap.Width, buttonBitmap.Height, GraphicsUnit.Pixel, imgAttr); } } }}这个是个自定义控件的代码。你看看吧 可以用CheckBox代替CheckBox.Appearance=Button C#连续整数问题修改 如何在panel上画一个矩形 怎么声名数组? 问一个C移植到C#时位操作的问题 如何将字符转成日期类型 .net连接Access的问题 哪位老兄帮帮忙呀 C#调用DLL 这里有很多C#的初级教材 传奇类游戏的视野计算有做过的大神咩 JSON转SortedList 求教:關於Timers在C#.NET的WEB開發中應該怎樣應用 一个小问题!
正常状态不可见, 按下状态可见
有点像是按下得效果,好像不是很明显
什么控件有这种效果?
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.ComponentModel;namespace AquaButtonTester
{
/// <summary>
/// Summary description for AquaButton.
/// </summary>
[Serializable]
public class AquaButton : System.Windows.Forms.Button
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool pulseOnFocus; ///
///Additional variables to handle pulsing
///
private ImageAttributes imgAttr = new ImageAttributes();
private float gamma;
private float minGamma;
private float maxGamma;
private float gammaStep;
private Timer pulseTimer = new Timer();
private Bitmap buttonBitmap;
private Rectangle buttonBitmapRectangle; public AquaButton()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent(); mouseAction = MouseActionType.None; this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.UserPaint, true); //The following defaults are better suited to draw the text outline
this.Font = new Font("Arial Black", 12, FontStyle.Bold);
this.BackColor = Color.DarkTurquoise;
this.Size = new Size(112, 48); //Initialize variables to Pulse button
gamma = 1.0f;
minGamma = 1.0f;
maxGamma = 2.2f;
gammaStep = .2f;
pulseTimer.Interval = 90;
pulseTimer.Tick +=new EventHandler(pulseTimer_Tick);
} [DefaultValue(false)]
public bool PulseOnFocus
{
get { return pulseOnFocus; }
set { pulseOnFocus = value; Invalidate(); }
}
/// <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()
{
components = new System.ComponentModel.Container();
}
#endregion private enum MouseActionType
{
None,
Hover,
Click
} private MouseActionType mouseAction; protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
Color clr = this.BackColor;
int shadowOffset = 8;
int btnOffset = 0;
switch (mouseAction)
{
case MouseActionType.Click:
shadowOffset = 4;
clr = Color.Gold;
btnOffset = 2;
break;
case MouseActionType.Hover:
clr = Color.Gold;
break;
}
g.SmoothingMode = SmoothingMode.AntiAlias; ///
/// Create main colored shape
///
Rectangle rc = new Rectangle(btnOffset,btnOffset,this.ClientSize.Width-8-btnOffset, this.ClientSize.Height-8-btnOffset);
GraphicsPath path1 = this.GetPath(rc, 20);
LinearGradientBrush br1 = new LinearGradientBrush(new Point(0, 0),new Point(0, rc.Height+6), clr, Color.White); ///
/// Create shadow
///
Rectangle rc2 = rc;
rc2.Offset(shadowOffset, shadowOffset);
GraphicsPath path2 = this.GetPath(rc2, 20);
PathGradientBrush br2 = new PathGradientBrush(path2);
br2.CenterColor = ControlPaint.DarkDark(Color.Silver);
br2.SurroundColors = new Color[]{Color.White}; ///
/// Create top water color to give "aqua" effect
///
Rectangle rc3 = rc;
rc3.Inflate(-5, -5);
rc3.Height = 15;
GraphicsPath path3 = GetPath(rc3, 20);
LinearGradientBrush br3 = new LinearGradientBrush(rc3, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical); ///
///draw shapes
///
g.FillPath(br2, path2); //draw shadow
g.FillPath(br1, path1); //draw main
g.FillPath(br3, path3); //draw top bubble ///
///Create a backup of the button image to a bitmap so we can manipulate it's pulsing action
///
buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size);
buttonBitmap = new Bitmap(buttonBitmapRectangle.Width, buttonBitmapRectangle.Height);
Graphics g_bmp = Graphics.FromImage(buttonBitmap);
g_bmp.SmoothingMode = SmoothingMode.AntiAlias;
g_bmp.FillPath(br1, path1);
g_bmp.FillPath(br3, path3); ///
///Set the region for the button
Region rgn = new Region(path1);
rgn.Union(path2);
this.Region = rgn; ///
/// Create a Path to draw the text to give the button a nice outline
///
GraphicsPath path4 = new GraphicsPath();
RectangleF path1bounds = path1.GetBounds();
Rectangle rcText = new Rectangle((int)path1bounds.X, (int)path1bounds.Y, (int)path1bounds.Width, (int)path1bounds.Height); StringFormat strformat = new StringFormat();
strformat.Alignment = StringAlignment.Center;
strformat.LineAlignment = StringAlignment.Center;
path4.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size,
rcText, strformat); Pen txtPen = new Pen(this.ForeColor, 1);
g.DrawPath(txtPen, path4);
g_bmp.DrawPath(txtPen, path4);
}
private GraphicsPath GetPath(Rectangle rc, int r)
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
GraphicsPath path = new GraphicsPath();
path.AddArc(x, y, r, r, 180, 90); //Upper left corner
path.AddArc(x+w - r, y, r, r, 270, 90); //Upper right corner
path.AddArc(x+w - r, y+h - r, r, r, 0, 90); //Lower right corner
path.AddArc(x, y+h - r, r, r, 90, 90); //Lower left corner
path.CloseFigure();
return path;
} protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.mouseAction = MouseActionType.Click;
this.Invalidate();
}
base.OnMouseDown (e);
} protected override void OnMouseUp(MouseEventArgs e)
{
if (this.Bounds.Contains(e.X, e.Y))
this.mouseAction = MouseActionType.Hover;
else
this.mouseAction = MouseActionType.None;
this.Invalidate();
base.OnMouseUp (e);
}
protected override void OnMouseEnter(EventArgs e)
{
this.mouseAction = MouseActionType.Hover;
this.Invalidate();
base.OnMouseEnter (e);
} protected override void OnMouseLeave(EventArgs e)
{
this.mouseAction = MouseActionType.None;
this.Invalidate();
base.OnMouseLeave (e);
} //-----------------------------------------------------------------
// METHODS TO HANDLE PULSING
//----------------------------------------------------------------- protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus (e);
if (this.pulseOnFocus)
pulseTimer.Start();
} protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus (e);
pulseTimer.Stop();
this.Invalidate(); //redraw to get back it's original picture
} private void pulseTimer_Tick(object sender, EventArgs e)
{
if (this.Focused && pulseOnFocus && buttonBitmap != null)
{
gamma += gammaStep;
if (gamma > this.maxGamma) gammaStep = - gammaStep;
if (gamma < this.minGamma) gammaStep = Math.Abs(gammaStep); imgAttr.SetGamma(gamma);
this.CreateGraphics().DrawImage(buttonBitmap, buttonBitmapRectangle, 0, 0,
buttonBitmap.Width, buttonBitmap.Height, GraphicsUnit.Pixel, imgAttr);
}
}
}
}这个是个自定义控件的代码。你看看吧
CheckBox.Appearance=Button