C#中水晶按钮的程序生成【转载】
做了一个控件,这样就可以告别图片按钮了:)
程序源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace WindowsControlLibrary1
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : Button
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool XiaCen=false;
private bool mouseMove=false;
private Color backColor;
public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
backColor=this.backColor;
//this.Text=this.ShowFocusCues.ToString();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// UserControl1
//
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.UserControl1_Paint);
this.MouseEnter += new System.EventHandler(this.UserControl1_MouseEnter);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.UserControl1_KeyUp);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.UserControl1_KeyDown);
this.BackColorChanged += new System.EventHandler(this.UserControl1_BackColorChanged);
this.MouseLeave += new System.EventHandler(this.UserControl1_MouseLeave);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseDown);
}
#endregion
protected GraphicsPath GetGraphicsPath(Rectangle rect)
{
GraphicsPath ClientPath = new System.Drawing.Drawing2D.GraphicsPath();
if(rect.Width<=0)
{
rect.Width=1;
}
if(rect.Height<=0)
{
rect.Height=1;
}
ClientPath.AddArc(rect.Left,rect.Top,rect.Height,rect.Height,90f,180f);
ClientPath.AddArc(rect.Right-rect.Height,rect.Top,rect.Height,rect.Height,270f,180f);
ClientPath.CloseFigure();
return ClientPath;
}
protected GraphicsPath GetGraphicsPath1(Rectangle rect)
{
GraphicsPath ClientPath = new System.Drawing.Drawing2D.GraphicsPath();
if(rect.Width<=0)
{
rect.Width=1;
}
if(rect.Height<=0)
{
rect.Height=1;
}
ClientPath.AddArc(rect.Left,rect.Top,rect.Height,rect.Height,190f,80f);
ClientPath.AddArc(rect.Right-rect.Height,rect.Top,rect.Height,rect.Height,270f,80f);
ClientPath.CloseFigure();
return ClientPath;
}
private void DrawYinYing(Graphics gr,bool xiacen)
{
Rectangle rect= this.ClientRectangle;
rect.Inflate(-(rect.Width/10),-(rect.Height)/4);
float bf1=rect.Width/100f;
float bf2=rect.Height/100f;
rect.Y=rect.Y+this.ClientRectangle.Height/4;
if(xiacen)
{
rect.Y=rect.Y+4;
}
GraphicsPath path;
for(int a=1;a<33;a++)
{
float bf3=bf1*a;
float bf4=bf2*a;
Rectangle rect1=rect;
rect1.Inflate(-(int)bf3,-(int)bf4);
path=GetGraphicsPath(rect1);
int r=backColor.R;
int g=backColor.G;
int b=backColor.B;
r=r+3*a;
g=g+3*a;
b=b+3*a;
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
gr.FillPath(new SolidBrush(Color.FromArgb(r,g,b)),path);
}
}
private void DrawGaoLiang(Graphics g,bool xiacen)
{
Rectangle rect= this.ClientRectangle;
rect.Inflate(-4,-4);
if(xiacen)
{
rect.Y=rect.Y+4;
}
GraphicsPath path=GetGraphicsPath1(rect);
RectangleF rect1=path.GetBounds();
rect1.Height=rect1.Height+1;
g.FillPath(new LinearGradientBrush(rect1,
Color.FromArgb(0xff,0xff,0xff,0xff),
Color.FromArgb(0xff,backColor),LinearGradientMode.Vertical),path);
}
private void DrawText(Graphics g,bool xiacen)
{
Rectangle rect= this.ClientRectangle;
Rectangle rect1= this.ClientRectangle;
StringFormat stringFormat=new StringFormat();
stringFormat.Alignment=StringAlignment.Center;
stringFormat.LineAlignment=StringAlignment.Center;
rect.Y=this.ClientRectangle.Height/5;
if(xiacen)
{
rect.Y=rect.Y+4;
rect1.Y=rect1.Y+4;
}
Font font=this.Font;
if(mouseMove)
{
font=new Font(this.Font,FontStyle.Underline);
}
g.DrawString(this.Text,font,
new SolidBrush(Color.FromArgb(0x66,backColor)),rect,stringFormat);
g.DrawString(this.Text,font,new SolidBrush(this.ForeColor),rect1,stringFormat);
}
private void UserControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(XiaCen==false)
{
XiaCen=true;
this.Refresh();
}
}
private void UserControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(XiaCen==true)
{
XiaCen=false;
this.Refresh();
}
}
private void UserControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(backColor),0,0,this.Width,this.Height);
e.Graphics.SmoothingMode=SmoothingMode.HighQuality;
e.Graphics.TextRenderingHint=TextRenderingHint.AntiAliasGridFit;
Rectangle rect=new Rectangle(0,0,this.Width,this.Height);
GraphicsPath ClientPath=GetGraphicsPath(rect);
e.Graphics.FillPath(new SolidBrush(backColor),ClientPath);
this.Region=new System.Drawing.Region(ClientPath);
DrawYinYing(e.Graphics,XiaCen);
DrawGaoLiang(e.Graphics,XiaCen);
DrawText(e.Graphics,XiaCen);
if(this.Focused)
{
e.Graphics.DrawPath(new Pen(Color.FromArgb(0x22,0xff,0xff,0xff), 3), ClientPath);
}
}
private void UserControl1_BackColorChanged(object sender, System.EventArgs e)
{
int r=BackColor.R;
int g=BackColor.G;
int b=BackColor.B;
r=r+0x22;
g=g+0x22;
b=b+0x22;
if(r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
backColor=Color.FromArgb(r,g,b);
}
private void UserControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(XiaCen==false && e.KeyCode==Keys.Space)
{
XiaCen=true;
this.Refresh();
}
}
private void UserControl1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(XiaCen==true && e.KeyCode==Keys.Space)
{
XiaCen=false;
this.Refresh();
}
}
private void UserControl1_MouseEnter(object sender, System.EventArgs e)
{
if(mouseMove==false)
{
mouseMove=true;
this.Refresh();
}
}
private void UserControl1_MouseLeave(object sender, System.EventArgs e)
{
if(mouseMove==true)
{
mouseMove=false;
this.Refresh();
}
}
}
}
转载于:https://www.cnblogs.com/ProgRamMer-cSdn/archive/2008/11/12/1332264.html
C#中水晶按钮的程序生成【转载】相关推荐
- Winform中实现自定义水晶按钮控件(附代码下载)
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个用户 ...
- Photoshop五步制作水晶按钮
用Photoshop五步制作简单实用水晶按钮 类水晶的按钮,在很多地方都能用道,那么有没有快速的方法制作出逼真的水晶按钮呢?呵呵,我在这里介绍一下我的制作方法,(是在观察了很多类水晶按钮之后)发现如下 ...
- 利用.NET绘图技术制作水晶按钮控件(转)
UI(User Interface)编程在整个项目开发过程中是个颇为重要的环节,任何好的解决方案若没有良好的用户界面呈现给最终用户,那么就算包含了最先进的技术也 不能算是好程序.UI编程体现在两个方面 ...
- 利用.NET绘图技术制作水晶按钮控件[转]
UI(User Interface)编程在整个项目开发过程中是个颇为重要的环节,任何好的解决方案若没有良好的用户界面呈现给最终用户,那么就算包含了最先进的技术也不能算是好程序.UI编程体现在两个方面, ...
- C#制作高仿360安全卫士窗体(四)- 水晶按钮
项目越来越紧,我也乐此不疲.自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西.我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用.所以只 ...
- iOS 9应用开发教程之ios9中实现按钮的响应
iOS 9应用开发教程之ios9中实现按钮的响应 IOS9实现按钮的响应 按钮主要是实现用户交互的,即实现响应.按钮实现响应的方式可以根据添加按钮的不同分为两种:一种是编辑界面添加按钮实现的响应:另一 ...
- (android控件)ListView的Item中设置按钮实现
前言 在项目的开发过程中,需要Listview的Item中包含按钮的效果.在实际开发过,Item中放置按钮后,ListView 单行选择背景颜色变化的效果没有了. 问题原因 ListView 和 其它 ...
- Android Listview中Button按钮点击事件冲突解决办法
今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
- 点击Cell中的按钮时,如何取所在的Cell
点击Cell中的按钮时,如何取所在的Cell: -(void)OnTouchBtnInCell:(UIButton *)btn { CGPoint point = btn.center; poi ...
- 从系统中取得指定资源图像(转载)
从系统中取得指定资源图像(转载) 在Winuser.h中定义了一些系统常用的资源,比如对话框的图标,某些按钮上的图像等. 以下是部分OEM资源序号和一些标准ICON的ID: //----------- ...
最新文章
- [LeetCode] Sum of Two Integers 两数之和
- JavaScript中的三种常用继承方法
- python 组合数库函数_Python数据分析之Numpy库(笔记)
- 零距离泛目录站群开源版源码
- 周三晚八点直播丨如何通过APEX 实现自动化运维
- **踩坑之通用Mapper中 Mapper.selectByExample(example)**的返回
- Iroha and Haiku II
- Cainteoir Text-to-Speech 0.8 发布
- qemu前后端features协商过程分析(vhost_user后端)
- python3调用arcpy地理加权回归_地理加权回归( GWR)
- Git diffmerge 工具的配置及问题解决
- 一口气说出 OAuth2.0 的四种授权方式,面试官会高看一眼
- win7 最常用的快捷键 ( 完全可以使用键盘来操作)
- Windows CMD常用命令大全
- 游戏王-黑暗大魔法师发动教程
- 软考(软件设计师)考点总结 --法律法规与知识产权
- ubuntu 禁用笔记本触摸板
- C盘不够了怎么办!将其他盘空间怎么分给C盘!
- java 10000以内的质数_【10000以内有多少质数】作业帮
- 大连新计划科技曝光网站建设中的“潜规则”