?? menuitemex.cs
字號:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Resources;
using UtilityLibrary.Win32;
using UtilityLibrary.WinControls;
using UtilityLibrary.General;
using UtilityLibrary.CommandBars;
namespace UtilityLibrary.Menus
{
/// <summary>
/// Summary description for MenuItemEx.
/// </summary>
public class MenuItemEx : MenuItem
{
static ColorGroup group = ColorGroup.GetColorGroup();
static Color bgColor = group.bgColor;
static Color stripeColor = group.stripeColor;
static Color selectionColor = group.selectionColor;
static Color borderColor = group.borderColor;
static Color darkSelectionColor = group.darkSelectionColor;
static int iconSize = SystemInformation.SmallIconSize.Width + 5;
static int itemHeight;
static bool doColorUpdate = false;
string shortcuttext = "";
Bitmap icon = null;
EventHandler clickHandler = null;
// We could use an image list to associate
// the menu items with an bitmap instead of
// assigning a whole Bitmap object to the menu item
ImageList imageList = null;
int imageIndex = -1;
static int BITMAP_SIZE = 16;
static int STRIPE_WIDTH = iconSize + 2;
protected static ImageList menuImages;
static Color transparentColor = Color.FromArgb(192, 192, 192);
static MenuItemEx()
{
// Initialize menu glyphs: checkmark and bullet
menuImages = new ImageList();
menuImages.ImageSize = new Size(BITMAP_SIZE, BITMAP_SIZE);
Assembly thisAssembly = Assembly.GetAssembly(Type.GetType("UtilityLibrary.Menus.MenuItemEx"));
ResourceManager rm = new ResourceManager("UtilityLibrary.Resources.ImagesMenu", thisAssembly);
Bitmap glyphs = (Bitmap)rm.GetObject("Glyphs");
glyphs.MakeTransparent(transparentColor);
menuImages.Images.AddStrip(glyphs);
}
// constructors
// ---------------------------------------------------
public MenuItemEx() : this(null, null)
{
}
public MenuItemEx(string name, EventHandler handler, Shortcut shortcut) : this(name, handler)
{
Initialize(null, shortcut, handler, null, -1);
}
public MenuItemEx(string name, Bitmap icon, Shortcut shortcut, EventHandler handler) : this(name, handler)
{
Initialize(icon, shortcut, handler, null, -1);
}
public MenuItemEx(string name, ImageList imageList, int imageIndex, Shortcut shortcut, EventHandler handler) : this(name, handler)
{
Initialize(icon, shortcut, handler, imageList, imageIndex);
}
public MenuItemEx(string name, EventHandler handler) : base(name, handler)
{
Initialize(null, Shortcut.None, handler, null, -1);
}
private void Initialize(Bitmap bitmap, Shortcut shortcut, EventHandler handler, ImageList list, int imageIndex)
{
OwnerDraw = true;
this.Shortcut = shortcut;
icon = bitmap;
clickHandler = handler;
imageList = list;
this.imageIndex = imageIndex;
}
static public MenuItem CloneMenu(MenuItemEx currentItem)
{
MenuItemEx clonedItem = new MenuItemEx(currentItem.Text, (Bitmap)currentItem.Icon,
(Shortcut)currentItem.Shortcut, currentItem.ClickHandler);
// Preserve the enable and check state
clonedItem.Enabled = currentItem.Enabled;
clonedItem.Checked = currentItem.Checked;
clonedItem.RadioCheck = currentItem.RadioCheck;
foreach (MenuItemEx item in currentItem.MenuItems)
{
clonedItem.MenuItems.Add(CloneMenu(item));
}
return clonedItem;
}
public Bitmap Icon
{
get
{
return icon;
}
set
{
icon = value;
}
}
public string ShortcutText
{
get
{
return shortcuttext;
}
set
{
shortcuttext = value;
}
}
public Color TransparentColor
{
get
{
return transparentColor;
}
set
{
transparentColor = value;
}
}
public ImageList ImageList
{
set
{
imageList = value;
}
get { return imageList; }
}
public int ImageIndex
{
set { imageIndex = value; }
get { return imageIndex; }
}
public EventHandler ClickHandler
{
set { clickHandler = value;}
get { return clickHandler; }
}
protected override void OnSelect(EventArgs e)
{
// This is to support popup menus when using this class
// in conjunction with a toolbar that behaves like a menu
Menu parent = Parent;
while (!(parent is CommandBarMenu) && !(parent == null) )
{
if (parent is MenuItemEx)
parent = (parent as MenuItemEx).Parent;
else if (parent is MenuItem)
parent = (parent as MenuItem).Parent;
else if ( parent == Parent.GetMainMenu() )
parent = null;
else
parent = null;
}
if ( parent is CommandBarMenu )
{
CommandBarMenu cbm = (CommandBarMenu)parent;
cbm.SelectedMenuItem = this;
}
base.OnSelect(e);
}
static public void UpdateMenuColors(object sender, EventArgs e)
{
doColorUpdate = true;
}
static public void UpdateMenuColors()
{
doColorUpdate = true;
}
private void DoUpdateMenuColors()
{
ColorGroup group = ColorGroup.GetColorGroup();
bgColor = group.bgColor;
stripeColor = group.stripeColor;
selectionColor = group.selectionColor;
borderColor = group.borderColor;
darkSelectionColor = group.darkSelectionColor;
doColorUpdate = false;
}
// overrides
// ---------------------------------------------------------
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
// measure shortcut text
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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -