- 热门文章:
- · Working with Anchoring and Docking Properties(ZT)
- · 穿针引线:一个不完全的对代码加亮的代码,大家可以对其进行扩展!(原创)
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · Window Hiding with C#(ZT)
- · 调试使用 Visual Basic .NET 或 Visual C# .NET 创建的Windows窗体控件 (zt)
- · Progress bar + status bar
- · 关于使用TreeView 控件的简单介绍
- · vb.net执行外部程序的代码
- · 限定窗口大小范围--windows message subclassing
- · 访问win2000 Active Directory信息的代码
- · 如何用Visual C#来创建、修改注册信息 <zt>
- · Visual C#资源文件编程--使用资源文件 <zt>
上一篇:Globalized Property Grid (一) >>
抛砖引玉——XP风格的按钮源代码
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
namespace EastSpider
{
/// <summary>
/// Summary description for VSNetButton.
/// </summary>
public class XPButton : System.Windows.Forms.Button
{
bool gotFocus = false;
bool mouseDown = false;
bool mouseEnter = false;
bool useDisableImage = true;
public XPButton()
{
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.Opaque, true);
}
public bool UseDisableImage
{
get
{
return useDisableImage;
}
set
{
useDisableImage = value;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = pe.Graphics;
if ( mouseDown )
{
DrawSelectedState(g, ColorUtil.VSNetPressedColor);
return;
}
if (mouseEnter)
{
DrawSelectedState(g, ColorUtil.VSNetSelectionColor);
return;
}
if ( Enabled )
DrawNormalState(pe.Graphics);
else
DrawDisableState(pe.Graphics);
}
protected override void OnMouseEnter(EventArgs e)
{
mouseEnter = true;
base.OnMouseEnter(e);
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
mouseEnter = false;
base.OnMouseLeave(e);
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
// Don@#t call base class
// it makes the painting to screw up
base.OnMouseDown(e);
mouseDown = true;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
// Don@#t call base class
// it makes the painting to screw up
Debug.WriteLine("OnMouseUp callled...");
base.OnMouseUp(e);
mouseDown = false;
Invalidate();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
gotFocus = true;
Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
gotFocus = false;
Invalidate();
}
protected void DrawNormalState(Graphics g)
{
Rectangle rc = ClientRectangle;
// Draw background
g.FillRectangle(new SolidBrush(SystemColors.Control), rc);
if ( Image != null )
{
SizeF sizeF = Image.PhysicalDimension;
int imageWidth = (int)sizeF.Width;
int imageHeight = (int)sizeF.Height;
// We are assuming that the button image is smaller than
// the button itself
if ( imageWidth > rc.Width || imageHeight > rc.Height)
{
Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
return;
}
int x = (Width - imageWidth)/2;
int y = (Height - imageHeight)/2;
g.DrawImage(Image, x, y, imageWidth, imageHeight);
}
}
protected void DrawDisableState(Graphics g)
{
// Draw disable image
Rectangle rc = ClientRectangle;
// Erase whaterver that was there before
g.FillRectangle(new SolidBrush(SystemColors.Control), rc);
// Draw border rectangle
g.DrawRectangle(new Pen(new SolidBrush(SystemColors.ControlDark)),
rc.Left, rc.Top, rc.Width-1, rc.Height-1);
// Draw disable image
if ( Image != null )
{
SizeF sizeF = Image.PhysicalDimension;
int imageWidth = (int)sizeF.Width;
int imageHeight = (int)sizeF.Height;
// We are assuming that the button image is smaller than
// the button itself
if ( imageWidth > rc.Width || imageHeight > rc.Height)
{
Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
return;
}
int x = (Width - imageWidth)/2;
int y = (Height - imageHeight)/2;
ControlPaint.DrawImageDisabled(g, Image, x, y, SystemColors.Control);
}
}
protected void DrawSelectedState(Graphics g, Color selColor)
{
Rectangle rc = ClientRectangle;
// Erase whaterver that was there before
g.FillRectangle(new SolidBrush(selColor), rc);
// Draw border rectangle
g.DrawRectangle(new Pen(new SolidBrush(SystemColors.Highlight)),
rc.Left, rc.Top, rc.Width-1, rc.Height-1);
// Draw image
if ( Image != null )
{
SizeF sizeF = Image.PhysicalDimension;
int imageWidth = (int)sizeF.Width;
int imageHeight = (int)sizeF.Height;
// We are assuming that the button image is smaller than
// the button itself
if ( imageWidth > rc.Width || imageHeight > rc.Height)
{
Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
return;
}
int x = (Width - imageWidth)/2;
int y = (Height - imageHeight)/2;
int gap = 1;
if ( selColor != ColorUtil.VSNetPressedColor )
{
// Draw disable image first to produce the popping image effect
if ( useDisableImage )
{
ControlPaint.DrawImageDisabled(g, Image, x, y, SystemColors.Control);
}
else
gap = 0;
}
else
{
gap = 0;
}
g.DrawImage(Image, x-gap, y-gap, imageWidth, imageHeight);
}
}
}
}
相关文章:
- · Visual C#资源文件编程--创建资源文件 <zt>
- · 用Visual C#来删除注册表中的注册信息 <zt>
- · Windows消息类,续二
- · Windows消息类,续一
- · 用vb.net开发api viewer(1)
- · 用vb.net开发api viewer(2)
- · Windows消息类。非常的有用。国外的,我特意贴出来。
- · 用vb.net开发api viewer(3)
- · 用vb.net开发api viewer(4)
- · 看看这 ( 目录浏览 )
- · vb.net中使用sendkeys方法与shell函数
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · chm文件DIY -- zt sjtubbs
- · C#中键盘钩子的使用(转)
- · 使用 .NET 框架类替代 API 调用 -- Zt
- · NET中对DCOM的调用
- · 用C#在域中增加用户和邮箱
- · 动态加载用户控件(整理)
- · 动态加载服务器控件(整理)
- · Visual C# 对话框全接触(续)
- · .Net平台Windows Forms程序设计简介 -2
- · Visual C# 对话框全接触
- · .Net平台Windows Forms程序设计简介-1(转自:天极网)
- · Retrieving Environmental Details using VB.NET
- · 东方兄,我搞定了,看代码
- · 这个是一个常见错误
- · 让你仔细看MSDN,你不看,现在又来问了:)
- · Displaying ListView items - with class!
- · 应Mr.Cool要求:Using Late Bound COM Objects
- · 原创Nutsbell系列---winform如何做启动页面
- · 一个简单的用户登陆程序……
- · Henry手记— WinForm Datagrid结构剖析(一)
- · 手把手教你在Win2003下配置ASP.NET开发环境
- · winform里的treeview能够跟数据绑定吗?我不知道。里面有treeview的用法。是转来的,看看吧。我已经用了。
- · 用户界面设计风格说明(转)
- · 一段log execption 的代码
- · 从App到Application (CSDN)
- · VB.Net创建不规则窗体 Montaque(原作)
