📄 gradientbutton.cs
字号:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HardwareDistributor.Utilities;
namespace HardwareDistributor.Controls
{
public partial class GradientButton : Control
{
public GradientButton()
{
InitializeComponent();
}
/// <summary>
/// Changes how control is drawn to indicate if it is pressed or not
/// </summary>
virtual public bool Pressed
{
get { return pressed; }
set
{
if ( pressed != value )
{
if ( value )
{
// check to see if the previous button needs to be redrawn
if ( ImageButtonWithFocus != null && ImageButtonWithFocus != this )
{
ImageButtonWithFocus.Pressed = false;
}
ImageButtonWithFocus = this;
}
pressed = value;
Invalidate();
}
}
}
protected bool pressed = false;
static internal GradientButton ImageButtonWithFocus = null;
/// <summary>
/// Image that is drawn behind the button text. null if no image is wanted.
/// </summary>
virtual public Image BackgroundImage
{
get { return backgroundImage; }
set { backgroundImage = value; }
}
protected Image backgroundImage;
/// <summary>
/// Background image to be drawn behind the button text when
/// the button is pressed. If there is no pressed image, then the background image is
/// used except it will be offset by a pixel right and down when pressed.
/// </summary>
virtual public Image PressedImage
{
get { return pressedImage; }
set { pressedImage = value; }
}
private Image pressedImage;
/// <summary>
/// True to draw a one pixel black border around the control.
/// False for no border.
/// </summary>
virtual public bool DrawBorder
{
get { return drawBorder; }
set { drawBorder = value; }
}
private bool drawBorder;
/// <summary>
/// The background color to use when the image button is selected or pressed
/// </summary>
virtual public Color SelectedColor
{
get { return selectedColor; }
set { selectedColor = value; }
}
private Color selectedColor = Color.LightBlue;
protected override void OnMouseDown( MouseEventArgs e )
{
Pressed = true;
base.OnMouseDown( e );
}
protected override void OnMouseUp( MouseEventArgs e )
{
// Pressed = false;
base.OnMouseUp( e );
}
protected override void OnGotFocus( EventArgs e )
{
Pressed = true;
base.OnGotFocus( e );
}
protected override void OnLostFocus( EventArgs e )
{
Pressed = false;
base.OnLostFocus( e );
}
protected override void OnKeyDown( KeyEventArgs e )
{
bool found = false;
try
{
// Move to the next control in the tab order
if ( e.KeyCode == (Keys.Back | Keys.Space) )
{
// find next control by tab stop
Parent.SelectNextControl( this, true, true, false, true );
}
// Move to the previous control in the tab order
else if ( e.KeyCode == (Keys.RButton | Keys.MButton | Keys.Space) )
{
// find previous control by tab stop
Parent.SelectNextControl( this, false, true, false, true );
}
// Handle the 'Enter' event from pressing the center button on a pocket PC
else if ( e.KeyCode == (Keys.MButton | Keys.LButton | Keys.Back) )
{
this.OnClick( e );
}
if ( found )
Pressed = false;
}
catch { } // ignore errors
base.OnKeyDown( e );
}
// This determines where to start drawing text if we're doing it left justified
//private const int TextStartPos = 65; // ppc
private const int TextStartPos = 50; // smartphone
protected override void OnPaint( PaintEventArgs e )
{
Rectangle rect;
try
{
// if pressed, then highlight the button as selected
if ( pressed )
{
DrawGradient( e.Graphics );
//using ( SolidBrush brush = new SolidBrush( selectedColor ) )
//{
// rect = new Rectangle( TextStartPos - 4, 0, Width - TextStartPos + 4, Height );
// e.Graphics.FillRectangle( brush, rect );
//}
}
}
catch { } // ignore error and continue with drawing of button
try
{
Image image;
// Find if there is an image to use
image = backgroundImage;
if ( pressed && pressedImage != null )
image = pressedImage;
// Draw an image, if any
if ( image != null )
{
System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
imageAttributes.SetColorKey( Color.White, Color.White );
// this ensures the image is centered vertically
int y;
y = (Height / 2) - (image.Height / 2);
if ( y < 0 )
y = 0;
// scale the image vertically
if ( image.Height > Height )
{
rect = new Rectangle( 2, y, ((Height * image.Width) / image.Height) + 1, Height );
}
else
{
rect = new Rectangle( 2, y, image.Width + 1, image.Height );
}
// Move the image right and down one pixel to appear pressed unless there
// is a specific pressed image to use.
if ( pressed && pressedImage == null )
{
rect.Offset( 1, 1 );
}
e.Graphics.DrawImage( backgroundImage, rect, 0, 0, backgroundImage.Width + 1,
backgroundImage.Height, GraphicsUnit.Pixel, imageAttributes );
}
}
catch { } // ignore image drawing errors and allow text drawing to continue - should never happen
// Draw text (if any)
if ( this.Text.Length > 0 )
{
SizeF size = e.Graphics.MeasureString( Text, Font );
// Draw the text
using ( SolidBrush brush = new SolidBrush( ForeColor ) )
{
// if pressed, then draw the text down and over 1 pixel just like the image.
int offset = (pressed ? 1 : 0);
// centers text - but no longer wanted, left here in case there is another change
//e.Graphics.DrawString( Text, Font, brush,
// ((ClientSize.Width - size.Width) / 2) + offset,
// ((ClientSize.Height - size.Height) / 2) + offset );
// Left justify text - center vertically
e.Graphics.DrawString( Text, Font, brush,
TextStartPos + offset,
((ClientSize.Height - size.Height) / 2) + offset );
}
}
// Draw a border (may want to remove)
if ( drawBorder )
{
using ( Pen pen = new Pen( Color.Gray ) )
{
if ( Dock == DockStyle.None )
{
e.Graphics.DrawRectangle( pen, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1 );
}
else if ( Dock == DockStyle.Top )
{
e.Graphics.DrawLine( pen, 0, ClientSize.Height - 1, ClientSize.Width, ClientSize.Height - 1 );
}
else
{
e.Graphics.DrawLine( pen, 0, 0, ClientSize.Width, 0 );
e.Graphics.DrawLine( pen, 0, ClientSize.Height - 1, ClientSize.Width, ClientSize.Height - 1 );
}
}
}
base.OnPaint( e );
}
/// <summary>
/// Draws a gradient background on the passed graphics item.
/// </summary>
/// <param name="gr">the object to fill</param>
void DrawGradient( Graphics gr )
{
Rectangle rectangle = this.ClientRectangle;
// first small part of button is kept white to let icons look better
// use half of height so it matches well with the icon size.
rectangle.X = Height >> 1;
rectangle.Width = rectangle.Width;
Native.GradientFill( gr, rectangle, Color.White, Color.SlateBlue, false );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -