C# 圆角button
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的更多相关文章
- Android怎样设置圆角button
1. 在res文件夹下的drawable文件夹下新建shape.xml文件 <?xml version="1.0" encoding="utf-8"?&g ...
- Android 自己定义UI圆角button
Android实际开发中我们一般须要圆角的button,普通情况下我们能够让美工做出来对应的button图片.然后放上去就可以,另外我们能够在布局文件里直接设置,也能够达到一样的效果. 以下解说在布局 ...
- Android 圆角Button
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbEAAADrCAYAAADnsqiUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAA
- Android学习之——实现圆角Button
在drawable文件夹下新建btn_shape.xml文件: <?xml version="1.0" encoding="utf-8"?> < ...
- <Android 基础(二十六)> 渐变色圆角Button
简介 总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加.作为一个Android开发者,这 ...
- jQuery Mobile(jqm)button的隐藏和显示,包含a标签,圆角和非圆角button
在移动互联网时代,HTML5开发越来越收到欢迎. 于是各种HTML5的框架都出来了.因为对于jquery的熟悉,jquery mobile 为多数人选择学习的对象.我也是众多追求者之中的一个.近期一直 ...
- 圆角button
方案1: <Window.Resources> <ControlTemplate x:Key="CornerButton" TargetType="{x ...
- Android Demo---如何敲出圆角的Button+圆角头像
经常玩儿App的小伙伴都知道,APP上面有很多按钮都是圆角的,圆形给人感觉饱满,富有张力,不知道设计圆角按钮的小伙伴是不是和小编有着相同的想法`(*∩_∩*)′,听小编公司开发IOS的小伙伴说,他们里 ...
- UWP Button添加圆角阴影(二)
原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...
随机推荐
- linux centos 7 下安装ElasticSearch5.4
一. 把elasticsearch-5.4.0.rpm和kibana-5.4.0-x86_64.rpm上传到centos下/root目录中,如下图:二.进入centos目录/root,并用命令rpm ...
- linux中rc.d目录下的文件
参考 http://blog.sina.com.cn/s/blog_414d78870102vqj5.html http://www.360doc.com/content/12/0820/17/933 ...
- c语言求方阵的行列式、伴随矩阵算法
#include<stdio.h> #include<math.h> #define N 100 //N比输入的阶数大即可 int main() { int n,a[N][ ...
- sort函数详解(史上最完整QAQ)
1.sort 使用:#include <algorithm> using namespace std; 作用:排序 时间复杂度:n*lg(n) 实现原理:sort并不是简单的快速排序, ...
- Spring Security 入门
一.Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配 ...
- 关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题
关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天.最后还是用了spring boot来做.下面是几篇关于打包的有参考价值的文章,以 ...
- Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)
输入: ls -l `which mvn` 如图:
- 开源一个C# Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- DB2 close auto commit
db2 关闭命令行CLP自动提交 --临时关闭自动提交 #db2 "update command options using C off --永久关闭自动提交 ----linux 环境下 # ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...