C# button重绘
1.添加一个新类
using System;
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace WindowsFormsApplication2
{
class MyButton:System.Windows.Forms.Button
{
private bool mouseDown = false;
private bool mouseHover = false;
public MyButton()
{
SetStyle(ControlStyles.UserPaint, true);
MouseDown += new MouseEventHandler(OnMouseDown);
MouseUp += new MouseEventHandler(OnMouseUp);
MouseEnter += new EventHandler(OnMouseEnter);
MouseLeave +=new EventHandler(OnMouseLeave);
Height = 23;
Width = 72;
BackColor = Color.Red;
ForeColor = Color.Black;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
Font = System.Windows.Forms.Control.DefaultFont;
// UseWaitCursor = true;
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
}
private void OnMouseEnter(object sender, EventArgs e)
{
mouseHover = true;
OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
}
private void OnMouseLeave(object sender, EventArgs e)
{
mouseHover = false;
OnPaint(new PaintEventArgs(CreateGraphics(),ClientRectangle));
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(Parent.BackColor), pevent.ClipRectangle );
if (Enabled == false)
{
DrawDisableButton(pevent.Graphics);
}
else if(mouseDown)
{
DrawMoseDownButton(pevent.Graphics);
}
else if (mouseHover)
{
DrawMosueHoverButton(pevent.Graphics);
}
else if(Focused)
{
DrawContaionFoceuButton(pevent.Graphics);
}
WriteText(pevent.Graphics);
}
private void WriteText(Graphics g)
{
int x = 0, y = 0;
Size s = g.MeasureString(Text, Font).ToSize();
x = (Width - s.Width) / 2;
y = (Height - s.Height) / 2;
if (Enabled)
g.DrawString(Text, Font, Brushes.Black, x, y);
else
g.DrawString(Text, Font, Brushes.Gray, x, y);
}
private void DrawContaionFoceuButton(Graphics g)
{
DrawBorder(g, 5);
PaintBack(g, SystemColors.ControlLightLight);
}
private void DrawMosueHoverButton(Graphics g)
{
DrawBorder(g, 2);
PaintBack(g, SystemColors.ControlLightLight);
}
private void DrawMoseDownButton(Graphics g)
{
DrawBorder(g, 3);
PaintBack(g, SystemColors.ControlLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g, 4);
PaintBack(g, SystemColors.ControlLight);
}
private void PaintBack(Graphics g, Color c)
{
g.FillRectangle(new SolidBrush(c), 3, 3, Width - 6, Height-6);
}
private void DrawBorder(Graphics g, int state)
{
if (state == 1) // draw normal style border
{
Pen p = new Pen(SystemColors.ControlLightLight, 2);
g.DrawLine(p, 1, 1, 1, Height-2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if (state == 2)//draw hover style border
{
Pen p = new Pen(Color.Yellow, 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if (state == 3)//draw pressed style border
{
Pen p = new Pen(SystemColors.ControlDark, 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if (state == 4)//draw disabled style border
{
Pen p = new Pen(SystemColors.ControlLight, 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
else if (state == 5)//draw default style border
{
Pen p = new Pen(Color.SkyBlue, 2);
g.DrawLine(p, 1, 1, 1, Height - 2);
g.DrawLine(p, 1, 1, Width - 2, 1);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
}
if (state==4)
{
Pen p = new Pen(Color.FromArgb(161, 161, 146), 1);
g.DrawLine(p, 0, 2, 0, Height - 3);
g.DrawLine(p, 2, 0, Width - 3, 0);
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 3);
g.DrawLine(p, 2, Height - 1, Width - 3, Height - 1);
g.DrawLine(p, 0, 2, 2, 0);
g.DrawLine(p, 0, Height - 3, 2, Height - 1);
g.DrawLine(p, Width - 3, 0, Width - 1, 2);
g.DrawLine(p, Width - 3, Height - 1, Width - 1, Height - 3);
}
else
{
g.DrawLine(Pens.Black, 0, 2, 0, Height - 3);
g.DrawLine(Pens.Black, 2, 0, Width - 3, 0);
g.DrawLine(Pens.Black, Width - 1, 2, Width - 1, Height - 3);
g.DrawLine(Pens.Black, 2, Height - 1, Width - 3, Height - 1);
g.DrawLine(Pens.Black, 0, 2, 2, 0);
g.DrawLine(Pens.Black, 0, Height - 3, 2, Height - 1);
g.DrawLine(Pens.Black, Width - 3, 0, Width - 1, 2);
g.DrawLine(Pens.Black, Width - 3, Height - 1,
Width - 1, Height - 3);
}
}
}
}
2.在Form中的设计文件中
this.button1 = new MyButton();
private MyButton button1;
转载于:https://www.cnblogs.com/gywei/p/3380766.html
C# button重绘相关推荐
- java画笔覆盖在界面_Java实现画图程序和重绘
上次聊了一下事件监听机制,今天就来聊一下怎么实现一个画图程序并且实现重绘. 一.实现画图程序 1.实现一个画图程序所需的API类? JFrame窗体容器组件类 JPanel 面板元素组件类 JButt ...
- HighChart学习-更新数据data Series与重绘
一:HighChart介绍 基于JQuery的纯JavaScript的图标库,支持各种图表显示,同时还支持Mootools 与Prototype详细版本支持在这里: JQuery 1.3.2 - 1. ...
- ios uiview 如何刷新_UIView的重绘及布局刷新
本文将简要讨论以下几个问题: 1.UIView的drawRect方法的调用机制及注意点 2.UIView的layoutSubviews.layoutIfNeeded.setNeedsLayout等方法 ...
- 【转】【C#】C#重绘windows窗体标题栏和边框
摘要 windows桌面应用程序都有标准的标题栏和边框,大部分程序也默认使用这些样式,一些对视觉效果要求较高的程序,如QQ, MSN,迅雷等聊天工具的样式则与传统的windows程序大不相同,其中迅雷 ...
- java使用重绘实现拖动_原生JS使用Canvas实现拖拽式绘图功能
一.实现的功能 1.基于oop思想构建,支持坐标点.线条(由坐标点组成,包含方向).多边形(由多个坐标点组成).圆形(包含圆心坐标点和半径)等实体 2.原生JavaScript实现,不依赖任何第三方j ...
- Android视图状态及重绘流程分析,带你一步步深入了解View(三)
在前面一篇文章中,我带着大家一起从源码的层面上分析了视图的绘制流程,了解了视图绘制流程中onMeasure.onLayout.onDraw这三个最重要步骤的工作原理,那么今天我们将继续对View进行深 ...
- C# 重绘tabControl,添加关闭按钮(续)
在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码.除此之外,我还添加了 ...
- c#中控件重绘(放大缩小移动隐藏恢复后不消失)实例
//很重要,一定要重写并在在OnPaint()中用传入的pevent.Graphics重绘,并且屏蔽掉父类的OnPaint方法,这样重绘后的图形不论控件怎么操作都不会消失了 using System; ...
- MFC 之 重绘按键Cbutton
上次我们学习了如何美化对话框的界面,这次我们为上次的对话框添加两个按钮,一个是关闭按钮,另一个是最小化按钮,好,现在我们先看一下效果: 是不是很难看,因为我们的对话框美化了,所以我们的按钮也要美化,因 ...
最新文章
- Mondrain支持kylin问题解决
- 第四代Express框架koa简介
- js(小程序或JavaScript)中[]、{}、()区别
- 矩阵形状| 使用Python的线性代数
- consul命令行查看服务_第三章 consul服务注册与服务查询
- PostgreSQL主要优势
- Response.Redirect和Server.Transfer的区别
- 64位java没有javaw.exe,2019-01-01 eclipse无法找到javaw.exe怎么处理
- 斯坦福大学公开课:量子力学_TimelineMax:了解力学
- 服务器销售术语,服务器术语大全
- 判断三维空间中三点是否共线
- 白话 贝叶斯公式_[白话解析] 深入浅出朴素贝叶斯模型原理及应用
- JavaScript 各种事件、方法、参数详解示例及常见问题等(全)
- 142个手机短信笑话
- 浏览器无法访问localhost
- php7模拟,认识PHP7虚拟机()三
- next、nextInt、nextDouble、nextFloat和nextLine的区别。
- ROS_Kinetic_23 ROS流行版本和相关书籍汇总
- project顶行显示项目名称
- Piping OV7670 video to VGA output on ZYBO
热门文章
- linux 查看cpu
- 【若依(ruoyi)】Unknown column ‘create_time‘ in ‘order clause‘
- geth bootnodes
- 【thymeleaf】 Link (URL) expressions: 使用 Link 表达式创建 URL
- 【Nginx】输出/返回 HelloWorld
- linux修改权限掩码,Linux中控制文件权限修改的chgrp和umask命令用法
- u盘文件看得见却打不开_U盘中病毒,文件看得见,但是打不开,文件要怎么修复,求解答?...
- java三级考试题库_JAVA题库:格林模拟试题三(下)
- 查看torch依赖的cuda版本_torch多GPU情况下模型建立遇到的问题
- python网络爬虫文献综述_python网络爬虫综述