PDA上的那个2D的按钮实在太丑了,做了一个图片按钮,与大家共享。参考了 xyocj-- .net cf 兄弟的Blog中的文章,在这里表示感谢。
picButton.cs文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Streamsea.Properties;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Streamsea.Forms
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 图片按钮
/// </summary>
public partial class ucPictureButton : UserControl
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private bool bPressed;
private Image pressImg;
private Image backImg;
private Image disableImg;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Image m_img;
private PictureBoxSizeMode m_sizemode = PictureBoxSizeMode.StretchImage;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造函数
/// </summary>
public ucPictureButton()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Font = new Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Size _GetPreferredSize()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.m_img == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return base.Size;
}
return this.m_img.Size;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Rectangle _ImageRectangle
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Rectangle rectangle1 = new Rectangle(0, 0, 0, 0);
if (this.m_img != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (this.m_sizemode)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case PictureBoxSizeMode.Normal:
rectangle1.Size = this.m_img.Size;
return rectangle1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case PictureBoxSizeMode.StretchImage:
rectangle1.Size = base.ClientSize;
return rectangle1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case ((PictureBoxSizeMode)2):
return rectangle1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case PictureBoxSizeMode.CenterImage:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rectangle1.Size = this.m_img.Size;
Size size1 = base.ClientSize;
rectangle1.X = (size1.Width - rectangle1.Width) / 2;
rectangle1.Y = (size1.Height - rectangle1.Height) / 2;
return rectangle1;
}
}
}
return rectangle1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 指示如何显示图像。
/// </summary>
[DefaultValue(PictureBoxSizeMode.StretchImage)]
public PictureBoxSizeMode SizeMode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_sizemode;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Enum.IsDefined(typeof(PictureBoxSizeMode), value))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException();
}
if (this.m_sizemode != value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_sizemode = value;
base.Invalidate();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 背景图像
/// </summary>
[DefaultValue((string)null)]
public Image BackImage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return backImg; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
backImg = value;
m_img = backImg;
base.Invalidate();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按下时的图像
/// </summary>
[DefaultValue((string)null)]
public Image PressImage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return pressImg; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pressImg = value;
base.Invalidate();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 不可用时的图像
/// </summary>
[DefaultValue((string)null)]
public Image DisableImage
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return disableImg; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
disableImg = value;
m_img = disableImg;
base.Invalidate();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按钮文字
/// </summary>
public string ButtonText
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return base.Text;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Text = value;
this.Invalidate();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。
/// </summary>
public new bool Enabled
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return base.Enabled; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Enabled = value;
this.Invalidate();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 引发 System.Windows.Forms.Control.MouseDown 事件。
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bPressed = true;
this.Invalidate();
base.OnMouseDown(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 引发 System.Windows.Forms.Control.MouseUp 事件。
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bPressed = false;
this.Invalidate();
base.OnMouseUp(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 绘制控件的背景。
/// </summary>
/// <param name="e"></param>
protected override void OnPaintBackground(PaintEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Region region1 = new Region(base.ClientRectangle);
region1.Exclude(this._ImageRectangle);
e.Graphics.FillRegion(new SolidBrush(this.Parent!=null?this.Parent.BackColor:Color.White), region1);
region1.Dispose();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 引发 System.Windows.Forms.Control.Paint 事件。
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.bPressed)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (pressImg == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pressImg = Resources.PressImage;
}
m_img = pressImg;
}
else if (this.Enabled)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (backImg == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
backImg = Resources.BackImage;
}
m_img = backImg;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disableImg == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
disableImg = Resources.DisableImage;
}
m_img = disableImg;
}
e.Graphics.DrawImage(this.m_img, this._ImageRectangle, new Rectangle(0, 0, this.m_img.Width, this.m_img.Height), GraphicsUnit.Pixel);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.ButtonText.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SizeF size = e.Graphics.MeasureString(this.ButtonText, this.Font);
e.Graphics.DrawString(this.ButtonText, this.Font, new SolidBrush(Enabled?this.ForeColor:Color.Gray),
(this.ClientSize.Width - size.Width) / 2, (this.ClientSize.Height - size.Height) / 2);
}
//e.Graphics.DrawRectangle(new Pen(this.Parent != null ? this.Parent.BackColor : Color.White), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
base.OnPaint(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 引发 System.Windows.Forms.Control.Resize 事件。
/// </summary>
/// <param name="eventg"></param>
protected override void OnResize(EventArgs eventg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnResize(eventg);
if ((this.m_sizemode == PictureBoxSizeMode.StretchImage) || (this.m_sizemode == PictureBoxSizeMode.CenterImage))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Invalidate();
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
picButton.Designer.cs
namespace Streamsea.Forms
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
partial class ucPictureButton
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing && (components != null))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
base.Dispose(disposing);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
组件设计器生成的代码#region 组件设计器生成的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在项目属性的资源文件里添加3张图片,名字分别为 BackImage DisableImage PressImage,这是默认的图片,在设计和谐程序的时候也可以换用其他图片的。