using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace winformTest
{
public partial class XButton : Button
{ /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
} enum model
{
hover,
enter,
press,
enable
} public Color HoverBackColor { get; set; }
public Color EnterBackColor { get; set; }
public Color PressBackColor { get; set; }
public Color HoverForeColor { get; set; }
public Color EnterForeColor { get; set; }
public Color PressForeColor { get; set; } public int Radius { get; set; } model paintmodel = model.hover;
public XButton()
{
InitializeComponent();
//这些得带上,不然会有黑边
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = ;
FlatAppearance.BorderColor = Color.FromArgb(, , , );
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent; SetDefaultColor(); }
public void SetDefaultColor()
{//给个初始值
HoverBackColor = Color.LightBlue;
EnterBackColor = Color.Blue;
PressBackColor = Color.DarkBlue;
HoverForeColor = Color.White;
EnterForeColor = Color.White;
PressForeColor = Color.White;
Radius = ;
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);//这个不能去,而且得放在前面,不然会有黑框之类的莫名其妙的东西
var colorback = HoverBackColor;
var colorfore = HoverForeColor;
switch (paintmodel)
{
case model.hover:
colorback = HoverBackColor;
colorfore = HoverForeColor;
break;
case model.enter:
colorback = EnterBackColor;
colorfore = EnterForeColor;
break;
case model.press:
colorback = PressBackColor;
colorfore = PressForeColor;
break;
case model.enable:
colorback = Color.LightGray;
break;
default:
colorback = HoverBackColor;
colorfore = HoverForeColor;
break;
}
Draw(e.ClipRectangle, e.Graphics, false, colorback);
DrawText(e.ClipRectangle, e.Graphics, colorfore);
}
protected override void OnMouseEnter(EventArgs e)
{
paintmodel = model.enter;
base.OnMouseEnter(e); }
protected override void OnMouseLeave(EventArgs e)
{
paintmodel = model.hover;
base.OnMouseLeave(e); }
protected override void OnMouseDown(MouseEventArgs mevent)
{
paintmodel = model.press;
base.OnMouseDown(mevent);
}
protected override void OnEnabledChanged(EventArgs e)
{
paintmodel = Enabled ? model.hover : model.enable;
Invalidate();//false 转换为true的时候不会刷新 这里强制刷新下
base.OnEnabledChanged(e); }
void Draw(Rectangle rectangle, Graphics g, bool cusp, Color begin_color, Color? end_colorex = null)
{
Color end_color = end_colorex == null ? begin_color : (Color)end_colorex;
int span = ;
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//渐变填充
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
//画尖角
if (cusp)
{
span = ;
PointF p1 = new PointF(rectangle.Width - , rectangle.Y + );
PointF p2 = new PointF(rectangle.Width - , rectangle.Y + );
PointF p3 = new PointF(rectangle.Width, rectangle.Y + );
PointF[] ptsArray = { p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
//填充
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - , Radius)); }
void DrawText(Rectangle rectangle, Graphics g, Color color)
{
SolidBrush sbr = new SolidBrush(color);
var rect = new RectangleF();
switch (TextAlign)
{
case ContentAlignment.MiddleCenter:
rect = getTextRec(rectangle, g);
break;
default:
rect = getTextRec(rectangle, g);
break;
}
g.DrawString(Text, Font, sbr, rect);
}
RectangleF getTextRec(Rectangle rectangle, Graphics g)
{
var rect = new RectangleF();
var size = g.MeasureString(Text, Font);
if (size.Width > rectangle.Width || size.Height > rectangle.Height)
{
rect = rectangle;
}
else
{
rect.Size = size;
rect.Location = new PointF(rectangle.X + (rectangle.Width - size.Width) / , rectangle.Y + (rectangle.Height - size.Height) / );
}
return rect;
}
GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, , );
gp.AddArc(width - radius, y, radius, radius, , );
gp.AddArc(width - radius, height - radius, radius, radius, , );
gp.AddArc(x, height - radius, radius, radius, , );
gp.CloseAllFigures();
return gp;
}
}
}

C# 圆角button的更多相关文章

  1. Android怎样设置圆角button

    1. 在res文件夹下的drawable文件夹下新建shape.xml文件 <?xml version="1.0" encoding="utf-8"?&g ...

  2. Android 自己定义UI圆角button

    Android实际开发中我们一般须要圆角的button,普通情况下我们能够让美工做出来对应的button图片.然后放上去就可以,另外我们能够在布局文件里直接设置,也能够达到一样的效果. 以下解说在布局 ...

  3. Android 圆角Button

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAADrCAYAAADnsqiUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAA

  4. Android学习之——实现圆角Button

    在drawable文件夹下新建btn_shape.xml文件: <?xml version="1.0" encoding="utf-8"?> < ...

  5. <Android 基础(二十六)> 渐变色圆角Button

    简介 总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加.作为一个Android开发者,这 ...

  6. jQuery Mobile(jqm)button的隐藏和显示,包含a标签,圆角和非圆角button

    在移动互联网时代,HTML5开发越来越收到欢迎. 于是各种HTML5的框架都出来了.因为对于jquery的熟悉,jquery mobile 为多数人选择学习的对象.我也是众多追求者之中的一个.近期一直 ...

  7. 圆角button

    方案1: <Window.Resources> <ControlTemplate x:Key="CornerButton" TargetType="{x ...

  8. Android Demo---如何敲出圆角的Button+圆角头像

    经常玩儿App的小伙伴都知道,APP上面有很多按钮都是圆角的,圆形给人感觉饱满,富有张力,不知道设计圆角按钮的小伙伴是不是和小编有着相同的想法`(*∩_∩*)′,听小编公司开发IOS的小伙伴说,他们里 ...

  9. UWP Button添加圆角阴影(二)

    原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...

随机推荐

  1. 【bug】—— ios scroll 滚动穿透

    BUG描述 在 ios 微信浏览器或原生浏览器中,主内容容器.content在文档流内,并且overflow-y: scroll.在其之上有一个 fixed 定位的弹出层.popUp,滚动.popUp ...

  2. BootStrap Modal 点击空白时自动关闭

    本文为大家讲解的是如何禁用 BootStrap Modal 点击空白时自动关闭的方法,感兴趣的同学参考下. 方法如下 $('#myModal').modal({backdrop: 'static', ...

  3. [转] 两个静态html页面传值方法的总结

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/csdn_ds/article/details/78393564 问题 因最近尝试实现客户端与服务端分 ...

  4. Iviews视频搜索引擎

    随着视频类型的增加和数据量的日益庞大,如何有效地组织和管理这些数据,使人们能够方便地从大量视频数据中找到自己感兴趣的相关视频片段已成为一种迫切的需求,而能够满足这一需求的技术便是目前人们普遍关注的基于 ...

  5. python socket文件传输实现

    简单版 server(服务端) import socket import subprocess import struct import json import os share_dir = r'E: ...

  6. win10开启 linux Bash命令(win10内置了linux系统支持)

    win10开启 Ubuntu linux Bash命令(win10内置了linux系统支持) 第一步: 先在设置→更新和安全→针对开发人员中选择"开发人员模式",点击后会下载&qu ...

  7. JS框架设计之命名空间设计一种子模块

    命名空间 1.种子模块作为一个框架的最开始,除了负责初始化框架的最基础部分. 2.种子模块作为框架的最开始,那么什么是种子框架的最开始呢?答案是IIFE(立即调用函数表达式); IIFE(立即调用函数 ...

  8. js 数组随机排序

    仅用于个人学习记录 javascript 数组随机排序1.最简洁的方法:function randomsort(a, b) {    return Math.random()>.5 ? -1 : ...

  9. Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. 调用函数约定不同

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is ...

  10. 第一个hibernate程序HelloWorldHibernate

    HelloWorldHibernate步骤: HelloWorld 1,新建java项目hibernate_0100_HelloWorld 2,学习User-library-hibernate,并加入 ...