📄 nicemenu.cs
字号:
/* ***************************************
* NiceMenu.cs
* --------------------------------------
* This is an improvement by Francesco Natali (fn.varie@libero.it)
* over an improvement by Sajith M
* of the "Visual Studio .NET Menu Style"
* by Carlos H. Perez that appeared on CodeProject
* (http://www.codeproject.com)
* ---------------------------------------
* How to use it
* You have to add:
* using Utility.NiceMenu;
* and after the "InitializeComponent()" code:
* NiceMenu myNiceMenu = new NiceMenu();
* myNiceMenu.UpdateMenu(this.nameofyourmainmenu, new NiceMenuClickEvent(nameofyourclickfunction));
* ---------------------------------------
* With only three lines of code your menu
* will be AUTOMATICALLY updated with
* a new great .Net style menu!
* ****************************************/
namespace MyClock
{
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Text;
using System.Diagnostics;
using System.Collections;
/* ******************************************************
* NiceMenuClickEvent(object sender, System.EventArgs e)
* ------------------------------------------------------
* this is a delegate event that will be initializate with
* the user function to handle the click event on the menu
* ******************************************************/
public delegate void NiceMenuClickEvent(object sender, System.EventArgs e);
/* ******************************************************
* This is a simple example of a function
* to handle the click event:
* -----------------------------------------------------
public void myClickMenuEvent(object sender, System.EventArgs e)
{
if (typeof(NiceMenu).IsInstanceOfType(sender))
{
NiceMenu item = (NiceMenu)sender;
MessageBox.Show(item.Text); //example
}
}
* ******************************************************/
/// <summary>
/// A great and free improvement menu class!
/// </summary>
public class NiceMenu : MenuItem
{
/* If you want to add some incons in your menu
* you have to:
* 1. add an imagelist control in your form
* 2. add your icons in your imagelist control
* 3. add in your menu items the index of the icon
* in the first two characters. For example:
* 00New
* 01Open
* 02Close
* 4. After the "NiceMenu myNiceMenu = new NiceMenu();" code
* you have to add this simple line of code:
* myNiceMenu.imgMenu = nameofyourimagelist;
* */
/// <summary>
/// Set "MenuImages" with the your ImageList to add icons in your menu.
/// </summary>
public ImageList MenuImages;
private static Color window;
// Some useful properties to change the color of your menu
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 static bool doColorUpdate = false;
private string shortcuttext = "";
private Image icon = null;
private static int BITMAP_SIZE = 16;
private static int STRIPE_WIDTH = iconSize + 5;
public NiceMenu() : base()
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(string name) : base(name)
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(string name, EventHandler handler) : base(name, handler)
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(string name, NiceMenu[] items) : base(name, items)
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(string name, EventHandler handler, Shortcut shortcut) : base(name, handler, shortcut)
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string name, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, NiceMenu[] items) : base(mergeType, mergeOrder, shortcut, name, onClick, onPopup, onSelect, items)
{
OwnerDraw = true; UpdateColors();
}
private NiceMenu(string name, Image img) : this(name)
{
icon = img;
}
private NiceMenu(string name, EventHandler handler, Image img) : this(name, handler)
{
icon = img;
}
private NiceMenu(string name, EventHandler handler, Shortcut shortcut, Image img) : this(name, handler, shortcut)
{
icon = img;
}
// *********************************************************
private 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; }
}
//************************************************************************
private void UpdateColors()
{
window = SystemColors.Window;
backcolor = SystemColors.ControlLightLight;
barcolor = SystemColors.Control;
selectioncolor = SystemColors.Highlight;
framecolor = SystemColors.Highlight;
int wa = (int)window.A;
int wr = (int)window.R;
int wg = (int)window.G;
int wb = (int)window.B;
int mna = (int)backcolor.A;
int mnr = (int)backcolor.R;
int mng = (int)backcolor.G;
int mnb = (int)backcolor.B;
int sta = (int)barcolor.A;
int str = (int)barcolor.R;
int stg = (int)barcolor.G;
int stb = (int)barcolor.B;
int sla = (int)selectioncolor.A;
int slr = (int)selectioncolor.R;
int slg = (int)selectioncolor.G;
int slb = (int)selectioncolor.B;
backcolor = Color.FromArgb(wr-(((wr-mnr)*2)/5), wg-(((wg-mng)*2)/5), wb-(((wb-mnb)*2)/5));
barcolor = Color.FromArgb(wr-(((wr-str)*4)/5), wg-(((wg-stg)*4)/5), wb-(((wb-stb)*4)/5));
selectioncolor = Color.FromArgb(wr-(((wr-slr)*2)/5), wg-(((wg-slg)*2)/5), wb-(((wb-slb)*2)/5));
}
private static void UpdateMenuColors()
{
doColorUpdate = true;
}
private void DoUpdateMenuColors()
{
UpdateColors();
doColorUpdate = false;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -