日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# button重绘

發布時間:2024/9/5 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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重绘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。