C#中水晶按钮的程序生成

C#中水晶按钮的程序生成
版权所有 codesky.net 2003-2005
发表时间:2005-1-26    关键字:未知

做了一个控件,这样就可以告别图片按钮了:)

程序源代码:

 

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/jhabb/archive/2010/11/18/1880772.html

vb.net 与c# 窗体绘制水晶按钮图形 水晶按钮       对于用c#编写一个水晶按钮难度并不是很高,已经有很多人写过晶按钮。如果大家通过baidu or  google. 一定找到很多水晶按钮的网页,而且是很早以前写的啦!把我找到的地址贴一下:这个地址写挺详细:http://dev.yesky.com/msdn/32/2626032.shtmlcnblogs的一个链接http://blog.csdn.net/lo 阅读详情

相关推荐

C#之二十三 打印和水晶报表

视频课堂https://edu.csdn.net/course/play/7621 C#程序设计及宿舍管理系统实战 https://edu.csdn.net/course/detail/27107 打印组件介绍 打印是常用的的功能,打印的原理从本质上和Windows窗体画图的原理是一样的,你在窗体里面画了一个圆,那么同样的代码,打印机也会在纸张的同一个位置打印...

张晨光老师的播客 9154

java 水晶按钮_C#中水晶按钮的程序生成

做了一个控件,这样就可以告别图片按钮了:)程序源代码:using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;using System.Drawing.Drawing2D;using Syst...

weixin_33668386的博客 105

创建C# WinForm水晶风格按钮

本文还有配套的精品资源,点击获取 简介:在C# WinForm应用开发中,为了提升用户界面的美观性并提供良好用户体验,本篇文章深入探讨了如何通过继承 Button 类并重写绘图方法来自定义实现水晶按钮控件,以及如何处理鼠标事件以实现动态效果。文章提供了创建透明背景和特殊渲染效果的按钮的详细步骤和代码示例,包括基本的按钮样式和进阶的动态视觉效果。此外,还涉及了如...

weixin_35755562的博客 1877

C#中水晶按钮的程序生成【转载】

做了一个控件,这样就可以告别图片按钮了:) 程序源代码: using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using Sy...

dieshiji0937的博客 120

C#中水晶按钮的设计与实现

Windows Forms(WinForms)是.NET Framework中用于创建Windows桌面应用程序的用户界面框架。它是Microsoft Visual Studio的一部分,允许开发者使用拖放界面和代码来构建GUI应用程序。WinForms基于COM架构,提供了一种简单的方式来封装Win32 API,使开发者能够更快速地创建窗口化的应用程序。在Windows Forms中,开发者可以使用各种控件,如按钮、文本框、标签等,通过属性、方法和事件来控制它们。

weixin_33660045的博客 756

C#自定义水晶按钮控件源码教程2019

在现代软件开发中,自定义控件扮演着至关重要的角色,它们提供了对用户界面(UI)的额外控制和灵活性。自定义控件不仅能够增强应用程序的功能性,还可以提供一致且可定制的用户体验。为了打造自定义控件,开发者需要了解一系列基础概念,比如控件的结构、属性、事件处理以及样式模板的应用。本章将为读者铺垫这些基础知识,从自定义控件的基本概念讲起,涵盖在Windows Forms和WPF环境下的基础知识,并为后续章节关于控件深入开发和优化奠定坚实基础。

weixin_42181686的博客 391

C#中水晶按钮的程序生成.

<!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--><script type="text/javascript"

zgqtxwd的专栏 281

DAP-Link DIY复刻指南

DAP-Link是ARM开源的一个项目,主要用于代码下载以及调试,支持SWD和JTAG调试接口,对于开发嵌入式的同学相信都或多或少有所耳闻。除了DAP-Link之外还有很多同类型的调试器,如ST-LinkJ-Link,但是这些都是不开源的,无法复刻或进行二次开发。但DAP-Link不同,完全开源,不用担心侵权问题,且随着开源社区大家的共同努力,DAP-Link经过了不断的迭代,先如今已取代ARM之前推出的CMSIS-DAP开源项目。

爱出名的狗腿子 1万+

C#中水晶按钮的程序生成. (转)

C#中水晶按钮的程序生成. (转)[@more@]做了一个控件,这样就可以告别图片按钮了:) 效果: 程序源代码: using System;using System.Collections;using System...

cuibeng3549的博客 129

TortoiseSVN的安装与使用

TortoiseSVN的安装与应用1、安装1.1、下载TortoiseSVN    点击下载TortoiseSvn1.2、下载汉化包。上面的页面中,找到Chinese, simplified汉化包,点击32位或者64位的Setup,下载汉化包。1.3、安装TortoiseSVN。在选择安装路径页面时要记得勾选“command line client tools”,默认不是勾选的。这里不勾选的话在I...

qq_28566071的博客 4万+

Codex 卡在 Reconnecting 5/5?一行代理配置解决长连接握手失败

长连接(WebSocket)在实时交互场景中比普通 HTTP 请求更脆弱,握手阶段一旦被代理规则误伤,就会触发指数退避重试并卡死在“Reconnecting 5/5”状态。其原理在于代理工具对 WebSocket 升级请求的兼容性不足,导致连接无法建立。通过为 Codex 相关域名单独配置一条高优先级直连规则,可绕过代理层的不确定性,快速恢复稳定会话。这一思路同样适用于远程开发、流式日志推送等依赖长连接的工程场景。

weixin_34005090的博客 275

Reeds-Shepp曲线公式推导与代码实现:从几何原理到路径求解

路径规划是自动驾驶和机器人运动控制中的核心问题,尤其在车辆低速、狭窄场景下,如何高效生成符合运动学约束的最短路径成为工程落地的关键。Reeds-Shepp曲线作为一种经典的几何路径生成算法,通过组合圆弧与直线段,能够在车辆可前进、可倒车的条件下给出最短路径解。本文从坐标系归一化、角度规范化等基础概念出发,系统推导LSL、RSR、LSR、LRL等核心路径类型的几何原理与可行条件,并给出可直接运行的Python代码骨架。同时结合实际调试中的数值陷阱,如角度区间选择、边界容差处理,以及往返自检方法,帮助开发者避开

weixin_33860553的博客 359

设计智能化的企业并购目标筛选多维度评估模型

企业并购是企业扩张和战略调整的重要手段,其目的在于实现资源的整合、优势互补和效率提升。评估指标单一:传统评估方法主要依赖财务指标,如净利润、市场份额等,忽视了企业其他重要的非财务指标,如品牌价值、客户满意度等。主观性较强:评估过程中,往往依赖于评估人员的经验和直觉,缺乏客观性,导致评估结果可能存在偏差。评估效率低:传统的评估方法通常需要大量的人力和时间,效率较低,难以应对大规模并购项目的需求。因此,设计一种智能化的企业并购目标筛选多维度评估模型,显得尤为必要。

SuperAGI2025 1035

[老blog] C#中水晶按钮的程序生成(老版) [2005年8月18日]

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

芽芽民工广场 1172

基于Python实现人脸识别相似度对比

随着人工智能技术的不断进步,人脸识别成为了一种广泛应用的技术。人脸相似度对比是其中的一个重要应用场景,可以用于人脸搜索、身份验证等方面。本文将介绍如何使用Python实现人脸相似度对比。

商务合作 / 项目定制 / 学习交流。个人vx:lovely_wml 9816

无需SLAM建图,直接生成Gazebo仿真环境的二维代价地图的功能包pgm_map_creator适配ROS Noetic编译报错及解决方法

在ROS Noetic中,使用`pgm_map_creator`功能包直接生成Gazebo仿真环境的二维代价地图时,可能会遇到各种编译和运行错误。本篇博客详细解析了在适配ROS Noetic过程中常见的报错及其解决方法,包括Boost文件结构更新、C++版本兼容性、Gazebo函数名变更、链接库缺失以及protobuf文件加载错误。通过逐步排查和修改,如更换头文件路径、调整C++标准、更新CMakeLists.txt文件等,成功解决了这些问题,使得`pgm_map_creator`功能包能够顺利编译和运行。

慕羽★的博客 2266

用C#写水晶按钮

       UI(User Interface)编程在整个项目开发过程中是个颇为重要的环节,任何好的解决方案若没有良好的用户界面呈现给最终用户,那么就算包含了最先进的技术也不能算是好程序。UI编程体现在两个方面,一是设计精美的用户界面,再有就是符合大多数用户习惯和易于使用的操作流程,而制作出精美的、绚丽多彩的用户界面是博得最终用户喜爱的第一步。我们就以制作一个水晶样式的三维按钮为例来丰富.Net

牛牛工作室 2284

RK3568 linux-5.10 rk809 声卡实现spk hp自动切换

RK3568 linux-5.10 rk809 声卡实现spk hp自动切换

目前从事linux驱动开发,多交流多学习吧 1101

WinForm自制水晶按钮

1.代码结构 2.实现代码 using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace WinFormBeautyButtonDemo { /// <summary> ...

weixin_30840573的博客 315

华为OD机试真题 新系统 2026-04-19 Java&Go 实现【分辨率排序】

华为OD机试真题 新系统 2026-04-19 Python&JS 实现【分辨率排序】

MISAYAONE的博客 316

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

C#水晶按钮

做了一个控件,这样就可以告别图片按钮了:) 效果: 程序源代码: using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.Drawing.Dra...

aome1470的博客 217

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

上一篇: 80后十三种最深沉的寂寞
下一篇: 几个程序员也难已回答的问题
weixin_30929011
博客等级 码龄11年 118粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值