按钮、菜单的重绘代码
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
namespace ControlSet
{
/**//// <summary>
/// 自绘按钮
/// </summary>
public 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=75;
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Parent.BackColor),
pe.ClipRectangle);
if (Enabled == false)
{
DrawDisableButton(pe.Graphics);
}
else if (mouseDown)
{
DrawMouseDownButton(pe.Graphics);
}
else if (mouseHover)
{
DrawMouseHoverButton(pe.Graphics);
}
else if (Focused)
{
DrawContainFocusButton(pe.Graphics);
}
else
{
DrawNormalButton(pe.Graphics);
}
WriteText(pe.Graphics);
}
private void OnMouseDown(object sender,MouseEventArgs e)
{
mouseDown=true;
}
private void OnMouseUp(object sender,MouseEventArgs e)
{
mouseDown=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseEnter(object sender,EventArgs e)
{
mouseHover=true;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void OnMouseLeave(object sender,EventArgs e)
{
mouseHover=false;
PaintEventArgs pe =
new PaintEventArgs(CreateGraphics(),ClientRectangle);
OnPaint(pe);
}
private void DrawBorder(Graphics g,int state)
{
if (state==1)
{
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)
{
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)
{
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)
{
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)
{
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);
}
}
private void DrawNormalButton(Graphics g)
{
DrawBorder(g,1);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawMouseHoverButton(Graphics g)
{
DrawBorder(g,2);
PaintBack(g,SystemColors.ControlLightLight);
}
private void DrawMouseDownButton(Graphics g)
{
DrawBorder(g,3);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawDisableButton(Graphics g)
{
DrawBorder(g,4);
PaintBack(g,SystemColors.ControlLight);
}
private void DrawContainFocusButton(Graphics g)
{
DrawBorder(g,5);
PaintBack(g,SystemColors.ControlLightLight);
}
private void PaintBack(Graphics g,Color c)
{
g.FillRectangle(new SolidBrush(c),3,3,
Width-6,Height-6);
}
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);
}
}
}
※※※※※※※※※※※※※※※※
菜单:
using System;
using System.Windows;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Text;
using System.Diagnostics;
using System.Collections;
namespace ControlSet
{
/**//// <summary>
/// 自绘菜单
/// </summary>
public class OwnerDrawMenu : MenuItem
{
private static Color backcolor;
private static Color barcolor;
private static Color selectioncolor;
private static Color framecolor;
private static int iconSize = SystemInformation.SmallIconSize.Width + 5;
private static int itemHeight;
private string shortcuttext = "";
private Image icon = null;
private static int BITMAP_SIZE = 16;
private static int STRIPE_WIDTH = iconSize + 5;
public OwnerDrawMenu() : base()
{
OwnerDraw = true;
}
private OwnerDrawMenu(string name) : base(name)
{
OwnerDraw = true;
}
public OwnerDrawMenu(string name, EventHandler handler) : base(name, handler)
{
OwnerDraw = true;
}
private OwnerDrawMenu(string name, EventHandler handler, Shortcut shortcut) : base(name, handler, shortcut)
{
OwnerDraw = true;
}
private OwnerDrawMenu(MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string name, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, OwnerDrawMenu[] items) : base(mergeType, mergeOrder, shortcut, name, onClick, onPopup, onSelect, items)
{
OwnerDraw = true;
}
private OwnerDrawMenu(string name, Image img) : this(name)
{
OwnerDraw = true;
icon = img;
}
private OwnerDrawMenu(string name, EventHandler handler, Image img) : this(name, handler)
{
OwnerDraw = true;
icon = img;
}
private OwnerDrawMenu(string name, EventHandler handler, Shortcut shortcut, Image img) : this(name, handler, shortcut)
{
OwnerDraw = true;
icon = img;
}
public Image Icon
{
get
{
return icon;
}
set
{
icon = value;
}
}
private string ShortcutText
{
get
{ return shortcuttext; }
set
{ shortcuttext = value; }
}
public Color SelectionColor
{
get
{ return selectioncolor; }
set
{ selectioncolor = value; }
}
public Color BackColor
{
get
{ return backcolor; }
set
{ backcolor = value; }
}
public Color BarColor
{
get
{ return barcolor; }
set
{ barcolor = value; }
}
public Color FrameColor
{
get
{ return framecolor; }
set
{ framecolor = value; }
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
if (Shortcut != Shortcut.None)
{
string text = "";
int key = (int)Shortcut;
int ch = key & 0xFF;
if (((int)Keys.Control & key) > 0) text += "Ctrl+";
if (((int)Keys.Shift & key) > 0) text += "Shift+";
if (((int)Keys.Alt & key) > 0) text += "Alt+";
if (ch >= (int)Shortcut.F1 && ch <= (int)Shortcut.F12)
text += "F" + (ch - (int)Shortcut.F1 + 1);
else
{
if ( Shortcut == Shortcut.Del)
{
text += "Del";
}
else
{
text += (char)ch;
}
}
shortcuttext = text;
}
if (Text == "-")
{
e.ItemHeight = 8;
e.ItemWidth = 4;
return;
}
bool topLevel = Parent == Parent.GetMainMenu();
string tempShortcutText = shortcuttext;
if ( topLevel )
{
tempShortcutText = "";
}
int textwidth = (int)(e.Graphics.MeasureString(Text + tempShortcutText, SystemInformation.MenuFont).Width);
int extraHeight = 4;
e.ItemHeight = SystemInformation.MenuHeight + extraHeight;
if ( topLevel )
e.ItemWidth = textwidth - 5;
else
e.ItemWidth = Math.Max(160, textwidth + 50);
itemHeight = e.ItemHeight;
base.OnMeasureItem(e);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = e.Bounds;
bool selected = (e.State & DrawItemState.Selected) > 0;
bool toplevel = (Parent == Parent.GetMainMenu());
bool hasicon = Icon != null;
bool enabled = Enabled;
DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
if (hasicon)
DrawIcon(g, Icon, bounds, selected, Enabled, Checked);
else
{
if (Checked)
DrawCheckmark(g, bounds, selected);
if (RadioCheck)
DrawRadioCheckmark(g, bounds, selected);
}
if (Text == "-")
{ DrawSeparator(g, bounds); }
else
{
DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);
}
}
private void DrawRadioCheckmark(Graphics g, Rectangle bounds, bool selected)
{
int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Bullet);
g.DrawRectangle(new Pen(framecolor), checkLeft-1, checkTop-1, BITMAP_SIZE+1, BITMAP_SIZE+1);
}
private void DrawCheckmark(Graphics g, Rectangle bounds, bool selected)
{
int checkTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
int checkLeft = bounds.Left + ( STRIPE_WIDTH - BITMAP_SIZE)/2;
ControlPaint.DrawMenuGlyph(g, new Rectangle(checkLeft, checkTop, BITMAP_SIZE, BITMAP_SIZE), MenuGlyph.Checkmark);
g.DrawRectangle(new Pen(framecolor), checkLeft-1, checkTop-1, BITMAP_SIZE+1, BITMAP_SIZE+1);
}
private void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool ischecked)
{
int iconTop = bounds.Top + (itemHeight - BITMAP_SIZE)/2;
int iconLeft = bounds.Left + (STRIPE_WIDTH - BITMAP_SIZE)/2;
if (enabled)
{
if (selected)
{
ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, Color.Black);
g.DrawImage(icon, iconLeft, iconTop-1);
}
else
{
g.DrawImage(icon, iconLeft + 1, iconTop);
}
}
else
{
ControlPaint.DrawImageDisabled(g, icon, iconLeft + 1, iconTop, SystemColors.HighlightText);
}
}
private void DrawSeparator(Graphics g, Rectangle bounds)
{
int y = bounds.Y + bounds.Height / 2;
g.DrawLine(new Pen(SystemColors.ControlDark), bounds.X + iconSize + 7, y, bounds.X + bounds.Width - 2, y);
}
private void DrawBackground(Graphics g, Rectangle bounds, DrawItemState state, bool toplevel, bool hasicon, bool enabled)
{
bool selected = (state & DrawItemState.Selected) > 0;
if (selected || ((state & DrawItemState.HotLight) > 0))
{
if (toplevel && selected)
{
bounds.Inflate(-1, 0);
g.FillRectangle(new SolidBrush(barcolor), bounds);
ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.SunkenOuter, Border3DSide.Top | Border3DSide.Left | Border3DSide.Right | Border3DSide.Bottom);
}
else
{
if(toplevel)
{
bounds.Inflate(-1, 0);
g.FillRectangle(new SolidBrush(barcolor), bounds);
ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.RaisedInner, Border3DSide.Top | Border3DSide.Left | Border3DSide.Right | Border3DSide.Bottom);
}
else
if (enabled )
{
g.FillRectangle(new SolidBrush(selectioncolor), bounds);
g.DrawRectangle(new Pen(framecolor), bounds.X, bounds.Y, bounds.Width-1, bounds.Height-1);
}
else
{
g.FillRectangle(new SolidBrush(barcolor), bounds);
bounds.X += STRIPE_WIDTH;
bounds.Width -= STRIPE_WIDTH;
g.FillRectangle(new SolidBrush(backcolor), bounds);
}
}
}
else
{
if (!toplevel)
{
g.FillRectangle(new SolidBrush(barcolor), bounds);
bounds.X += STRIPE_WIDTH;
bounds.Width -= STRIPE_WIDTH;
g.FillRectangle(new SolidBrush(backcolor), bounds);
}
else
{
g.FillRectangle(SystemBrushes.Control, bounds);
}
}
}
private void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state )
{
StringFormat stringformat = new StringFormat();
stringformat.HotkeyPrefix = ((state & DrawItemState.NoAccelerator) > 0) ? HotkeyPrefix.Hide : HotkeyPrefix.Show;
if ( toplevel )
{
int index = text.IndexOf("&");
if ( index != -1 )
{
text = text.Remove(index,1);
}
}
int textwidth = (int)(g.MeasureString(text, SystemInformation.MenuFont).Width);
int x = toplevel ? bounds.Left + (bounds.Width - textwidth) / 2: bounds.Left + iconSize + 10;
int topGap = 7;
if ( toplevel ) topGap = 2;
int y = bounds.Top + topGap;
Brush brush = null;
if (!enabled)
brush = new SolidBrush(SystemColors.GrayText);
else
brush = new SolidBrush(SystemColors.MenuText);
g.DrawString(text, SystemInformation.MenuFont, brush, x, y, stringformat);
if ( !toplevel )
{
stringformat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
g.DrawString(shortcut, SystemInformation.MenuFont, brush, bounds.Width - 10 , bounds.Top + topGap, stringformat);
}
}
}
}
※※※※※※※※※※※※※※※※※※
用友单据的面板(Panel)的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ControlSet
{
/**//// <summary>
/// 单据Panel
/// </summary>
public class SheetPanel : System.Windows.Forms.Panel
{
public SheetPanel()
{
this.BorderStyle=BorderStyle.None;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g=e.Graphics;
SolidBrush backbrush = new SolidBrush(this.Parent.BackColor);
g.FillRectangle(backbrush,0,0,this.Width,this.Height);
Pen pen = new Pen(Color.Black,1);
g.DrawLine(pen,this.Width-4,4,this.Width-2,4);
g.DrawLine(pen,4,this.Height-2,4,this.Height-4);
g.DrawLine(pen,this.Width-2,4,this.Width-2,this.Height-2);
g.DrawLine(pen,4,this.Height-2,this.Width-2,this.Height-2);
g.DrawRectangle(pen,2,2,this.Width-6,this.Height-6);
backbrush.Color=this.BackColor;
g.FillRectangle(backbrush,0,0,this.Width-6,this.Height-6);
g.DrawRectangle(pen,0,0,this.Width-6,this.Height-6);
}
}
}
转载于:https://www.cnblogs.com/Dragonpro/archive/2005/03/31/129402.html
按钮、菜单的重绘代码相关推荐
- C#中创建圆形/按钮(使用重绘)
创建圆形按钮挺简单的. public class EllipseButton : Button { protected override void OnPaint(PaintEventArgs pev ...
- 孙鑫-MFC笔记六--绘图,重绘
Windows颜色对话框功能的添加: MFC提供了CColorDialog类,方便创建颜色对话框. CColorDialog dlg: dlg.DoModal(); 默认为黑色.即首参为值为0. 保存 ...
- 解决MFC 的 CButton改变样式问题,使用重绘方式。(转载)
要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制.这可以通过定义一个以CButton为基类的新按钮类来实现.以下为具体的实现方法: 方法一: 加入一个新类,类名:CM ...
- MFC-CListCtrl重绘,添加按钮到单元格
原文链接 MFC Listctrl 不支持单元格颜色设置,以及单击其中某一单元格时高亮显示,要想达成自己的目的,就只能对其重绘. 关于单元格中按钮的添加,说一下思路,首先要重写CButton类,将其单 ...
- 用ASP.NET 重绘TabControl代码
在www.codeproject.com 看到一个关于重绘tabControl的例了,觉得挺有意思的.照着修改一下,有一些东西自己并没有去改,使得代码很短,同时也有一些功能并没实现的.具休可到http ...
- 【VB界面】子类化重绘仿QQ2009渐变按钮(模块)
'************************************************************************* ''----------------------- ...
- java都界面相对布局_浏览器的重排和重绘
浏览器的高层结构 1.用户界面 - 包括地址栏.前进/后退按钮.书签菜单等. 2.浏览器引擎 - 在用户界面和呈现引擎之间传送指令. 3.渲染引擎 - 负责显示请求的内容. 4.网络 - 用于网络调用 ...
- MFC 之 重绘按键Cbutton
上次我们学习了如何美化对话框的界面,这次我们为上次的对话框添加两个按钮,一个是关闭按钮,另一个是最小化按钮,好,现在我们先看一下效果: 是不是很难看,因为我们的对话框美化了,所以我们的按钮也要美化,因 ...
- Windows核心编程_重绘ListBox样式(仿QQListBox)
今天教大家如何重绘ListBox,其中涉及到的相关知识点也一一解释了 效果图: 是不是和QQ的列表框非常相似?那么这里就一步一步教大家如何使用Win32SDK来完成这项工作! 创建Win32窗口 #i ...
- Stable Diffusion系列课程上:安装、提示词入门、常用模型(checkpoint、embedding、LORA)、放大算法、局部重绘、常用插件
文章目录 一.Stable Diffusion简介与安装 二.文生图(提示词解析) 2.1 提示词入门 2.2 权重 2.3 负面提示词( Negative prompt) 2.4 出图参数设置 2. ...
最新文章
- Java 14 :NullPointerException的处理新方式
- inode linux x64,又一次在64位ubuntu上安装iNode
- 本地存储cookie和localStorage区别特点
- MySQL学习——标识符语法和命名规则
- 基础知识 | 对目标检测认识及理解
- 自学python能找到工作吗-自学Python好找工作吗?
- RFC2544优化步长测试——信而泰网络测试仪实操
- 编程c语言黑与白,C语言黑与白问题
- JAVA中interface接口的使用
- 游戏服务器的常用架构
- 【HCIE-BigData-Data Mining课程笔记(三)】预备知识-Python基础
- Matlab 查阅、读取nc数据
- hbase实战 与mysql_HBase实战系列1—压缩与编码技术
- 浏览器功能大比拼:谁更出彩?
- 壹度婚礼邀请函请帖小程序免费制作
- PDF文档无损去签名去口令
- response.setHeader各种用法
- from . import *
- 【JAVA基础】重新系统学习Java(五)案例一
- 南京邮电大学网络攻防平台WriteUP——WEB(上)
热门文章
- Togu Audio Line推出最新版本的TAL-BassLine-101插件 支持M1芯片
- 总是找不到电脑文件?学会这 3 个 Mac 搜索技巧,一秒就能找到
- Windows Server 2016 安装OpenSSH Server,并设置密钥
- Ububtu 18.04 安装 mysql 和 phpmyadmin 过程记录
- 如何快速制作短视频?拥有这个神器,轻松搞定!
- Security6:查看授予的权限
- 你真的了解Android ViewGroup的draw和onDraw的调用时机吗
- 使用MHA对mysql主从架构中的主节点做高可用
- HTML5- Canvas入门(五)
- SPI子系统分析之一:框架