Csharp/C Sharp/GUI Windows Form/MenuItem
Содержание
Add Help text for MenuItem
using System;
using System.Drawing;
using System.Windows.Forms;
class MenuItemHelp: MenuItem
{
StatusBarPanel sbpHelpPanel;
string strHelpText;
public MenuItemHelp(string strText): base(strText)
{
}
public StatusBarPanel HelpPanel
{
get { return sbpHelpPanel; }
set { sbpHelpPanel = value; }
}
public string HelpText
{
get { return strHelpText; }
set { strHelpText = value; }
}
protected override void OnSelect(EventArgs ea)
{
base.OnSelect(ea);
if (HelpPanel != null)
HelpPanel.Text = HelpText;
}
}
class MenuHelpSubclass: Form
{
StatusBarPanel sbpMenuHelp;
string strSavePanelText;
public static void Main()
{
Application.Run(new MenuHelpSubclass());
}
public MenuHelpSubclass()
{
StatusBar sb = new StatusBar();
sb.Parent = this;
sb.ShowPanels = true;
sbpMenuHelp = new StatusBarPanel();
sbpMenuHelp.Text = "Ready";
sbpMenuHelp.AutoSize = StatusBarPanelAutoSize.Spring;
sb.Panels.Add(sbpMenuHelp);
Menu = new MainMenu();
MenuItemHelp mi = new MenuItemHelp("&File");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Commands for working with files";
Menu.MenuItems.Add(mi);
mi = new MenuItemHelp("&Open...");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Opens an existing document";
Menu.MenuItems[0].MenuItems.Add(mi);
mi = new MenuItemHelp("&Close");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Closes the current document";
Menu.MenuItems[0].MenuItems.Add(mi);
mi = new MenuItemHelp("&Save");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Saves the current document";
Menu.MenuItems[0].MenuItems.Add(mi);
mi = new MenuItemHelp("&Edit");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Commands for editing the document";
Menu.MenuItems.Add(mi);
mi = new MenuItemHelp("Cu&t");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Deletes the selection and " +
"copies it to the clipboard";
Menu.MenuItems[1].MenuItems.Add(mi);
mi = new MenuItemHelp("&Copy");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Copies the selection to the clipboard";
Menu.MenuItems[1].MenuItems.Add(mi);
mi = new MenuItemHelp("&Paste");
mi.HelpPanel = sbpMenuHelp;
mi.HelpText = "Replaces the current selection " +
"with the clipboard contents";
Menu.MenuItems[1].MenuItems.Add(mi);
}
protected override void OnMenuStart(EventArgs ea)
{
strSavePanelText = sbpMenuHelp.Text;
}
protected override void OnMenuComplete(EventArgs ea)
{
sbpMenuHelp.Text = strSavePanelText;
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.MainMenu File = new System.Windows.Forms.MainMenu();
private System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem();
private System.Windows.Forms.MenuItem menuItem2 = new System.Windows.Forms.MenuItem();
private System.Windows.Forms.MenuItem menuItem3 = new System.Windows.Forms.MenuItem();
private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
private System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
private System.ruponentModel.Container components = null;
private int nIndex = 0;
public Form1() {
this.SuspendLayout();
this.File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1});
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3});
this.menuItem1.Text = "File";
this.menuItem2.Index = 0;
this.menuItem2.Text = "&Close";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
this.menuItem3.Index = 1;
this.menuItem3.Text = "E&xit";
this.button1.Location = new System.Drawing.Point(40, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "&Add";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(176, 208);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "&Close";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button2, this.button1});
this.Menu = this.File;
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e) {
Close();
}
private void menuItemHandler(object sender, System.EventArgs e) {
MessageBox.Show(this, "Menu Handler Called");
MenuItem mi = (MenuItem)sender;
MessageBox.Show(this, "Menu Item: " + mi.Text);
}
private void button1_Click(object sender, System.EventArgs e) {
MenuItem mi = new MenuItem("File " + (nIndex + 1), new EventHandler(menuItemHandler));
this.menuItem1.MenuItems.Add(mi);
nIndex++;
}
private void menuItem2_Click(object sender, System.EventArgs e) {
Close();
}
}
Font Menu
using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenu: Form
{
const int iPointSize = 24;
string strFacename;
public static void Main()
{
Application.Run(new FontMenu());
}
public FontMenu()
{
strFacename = Font.Name;
Menu = new MainMenu();
MenuItem mi = new MenuItem("&Facename");
mi.Popup += new EventHandler(MenuFacenameOnPopup);
mi.MenuItems.Add(" "); // Necessary for pop-up call
Menu.MenuItems.Add(mi);
}
void MenuFacenameOnPopup(object obj, EventArgs ea)
{
MenuItem miFacename = (MenuItem)obj;
FontFamily[] aff = FontFamily.Families;
EventHandler ehClick = new EventHandler(MenuFacenameOnClick);
MenuItem[] ami = new MenuItem[aff.Length];
for (int i = 0; i < aff.Length; i++)
{
ami[i] = new MenuItem(aff[i].Name);
ami[i].Click += ehClick;
if (aff[i].Name == strFacename)
ami[i].Checked = true;
}
miFacename.MenuItems.Clear();
miFacename.MenuItems.AddRange(ami);
}
void MenuFacenameOnClick(object obj, EventArgs ea)
{
MenuItem mi = (MenuItem)obj;
strFacename = mi.Text;
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Font font = new Font(strFacename, iPointSize);
StringFormat strfmt = new StringFormat();
grfx.DrawString("Sample Text", font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
Hatch Brush Menu
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class HatchBrushMenu: Form
{
HatchStyleMenuItem hsmiChecked;
const int iMin = 0, iMax = 52;
public static void Main()
{
Application.Run(new HatchBrushMenu());
}
public HatchBrushMenu()
{
ResizeRedraw = true;
Menu = new MainMenu();
Menu.MenuItems.Add("&Hatch-Style");
for (HatchStyle hs = (HatchStyle)iMin; hs <= (HatchStyle)iMax;hs++)
{
HatchStyleMenuItem hsmi = new HatchStyleMenuItem();
hsmi.HatchStyle = hs;
hsmi.Click += new EventHandler(MenuHatchStyleOnClick);
if ((int)hs % 8 == 0)
hsmi.BarBreak = true;
Menu.MenuItems[0].MenuItems.Add(hsmi);
}
hsmiChecked = (HatchStyleMenuItem) Menu.MenuItems[0].MenuItems[0];
hsmiChecked.Checked = true;
}
void MenuHatchStyleOnClick(object obj, EventArgs ea)
{
hsmiChecked.Checked = false;
hsmiChecked = (HatchStyleMenuItem) obj;
hsmiChecked.Checked = true;
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
HatchBrush hbrush = new HatchBrush(hsmiChecked.HatchStyle,
Color.White, Color.Black);
grfx.FillEllipse(hbrush, ClientRectangle);
}
}
class HatchStyleMenuItem: MenuItem
{
const int cxImage = 32, cyImage = 32, iMargin = 2;
readonly int cxCheck, cyCheck;
public HatchStyle HatchStyle;
public HatchStyleMenuItem()
{
OwnerDraw = true;
cxCheck = SystemInformation.MenuCheckSize.Width;
cyCheck = SystemInformation.MenuCheckSize.Height;
}
protected override void OnMeasureItem(MeasureItemEventArgs miea)
{
miea.ItemWidth = 2 * cxImage + 3 * iMargin - cxCheck;
miea.ItemHeight = cyImage + 2 * iMargin;
}
protected override void OnDrawItem(DrawItemEventArgs diea)
{
diea.DrawBackground();
if ((diea.State & DrawItemState.Checked) != 0)
{
ControlPaint.DrawMenuGlyph(diea.Graphics,
diea.Bounds.Location.X + iMargin,
diea.Bounds.Location.Y + iMargin,
cxImage, cyImage, MenuGlyph.Checkmark);
}
HatchBrush hbrush = new HatchBrush(HatchStyle,
Color.White, Color.Black);
diea.Graphics.FillRectangle(hbrush,
diea.Bounds.X + 2 * iMargin + cxImage,
diea.Bounds.Y + iMargin, cxImage, cyImage);
}
}
Help Menu
using System;
using System.Drawing;
using System.Windows.Forms;
class HelpMenu: Form
{
Bitmap bmHelp;
public static void Main()
{
Application.Run(new HelpMenu());
}
public HelpMenu()
{
bmHelp = new Bitmap(GetType(), "help.bmp");
Menu = new MainMenu();
Menu.MenuItems.Add("&Help");
MenuItem mi = new MenuItem("&Help");
mi.OwnerDraw = true;
mi.Click += new EventHandler(MenuHelpOnClick);
mi.DrawItem += new DrawItemEventHandler(MenuHelpOnDrawItem);
mi.MeasureItem += new MeasureItemEventHandler(MenuHelpOnMeasureItem);
Menu.MenuItems[0].MenuItems.Add(mi);
}
void MenuHelpOnMeasureItem(object obj, MeasureItemEventArgs miea)
{
miea.ItemWidth = bmHelp.Width;
miea.ItemHeight = bmHelp.Height;
}
void MenuHelpOnDrawItem(object obj, DrawItemEventArgs diea)
{
Rectangle rect = diea.Bounds;
rect.X += diea.Bounds.Width - bmHelp.Width;
rect.Width = bmHelp.Width;
diea.DrawBackground();
diea.Graphics.DrawImage(bmHelp, rect);
}
void MenuHelpOnClick(object obj, EventArgs ea)
{
MessageBox.Show("Help", Text);
}
}
new MenuItem
using System;
using System.Drawing;
using System.Windows.Forms;
class OldFashionedMenu: Form
{
MainMenu mmMain, mmFile, mmEdit;
public static void Main()
{
Application.Run(new OldFashionedMenu());
}
public OldFashionedMenu()
{
EventHandler eh = new EventHandler(MenuOnClick);
mmMain = new MainMenu(new MenuItem[]
{
new MenuItem("MAIN:"),
new MenuItem("&File", new EventHandler(MenuFileOnClick)),
new MenuItem("&Edit", new EventHandler(MenuEditOnClick))
});
mmFile = new MainMenu(new MenuItem[]
{
new MenuItem("FILE:"),
new MenuItem("&New", eh),
new MenuItem("&Open...", eh),
new MenuItem("&Save", eh),
new MenuItem("Save &As...", eh),
new MenuItem("(&Main)", new EventHandler(MenuMainOnClick))
});
mmEdit = new MainMenu(new MenuItem[]
{
new MenuItem("EDIT:"),
new MenuItem("Cu&t", eh),
new MenuItem("&Copy", eh),
new MenuItem("&Paste", eh),
new MenuItem("De&lete", eh),
new MenuItem("(&Main)", new EventHandler(MenuMainOnClick))
});
Menu = mmMain;
}
void MenuMainOnClick(object obj, EventArgs ea)
{
Menu = mmMain;
}
void MenuFileOnClick(object obj, EventArgs ea)
{
Menu = mmFile;
}
void MenuEditOnClick(object obj, EventArgs ea)
{
Menu = mmEdit;
}
void MenuOnClick(object obj, EventArgs ea)
{
MessageBox.Show("Menu item clicked!", Text);
}
}
Owner Drawn Menu
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace OwnerDrawnMenu
{
/// <summary>
/// Summary description for OwnerDrawnMenu.
/// </summary>
public class OwnerDrawnMenu : System.Windows.Forms.Form
{
internal System.Windows.Forms.ImageList imgMenu;
internal System.Windows.Forms.MainMenu mainMenu1;
internal System.Windows.Forms.MenuItem mnuFile;
internal System.Windows.Forms.MenuItem mnuNew;
internal System.Windows.Forms.MenuItem mnuOpen;
internal System.Windows.Forms.MenuItem mnuSave;
private System.ruponentModel.IContainer components;
public OwnerDrawnMenu()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OwnerDrawnMenu));
this.imgMenu = new System.Windows.Forms.ImageList(this.ruponents);
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuNew = new System.Windows.Forms.MenuItem();
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuSave = new System.Windows.Forms.MenuItem();
//
// imgMenu
//
this.imgMenu.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imgMenu.ImageSize = new System.Drawing.Size(16, 16);
this.imgMenu.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgMenu.ImageStream")));
this.imgMenu.TransparentColor = System.Drawing.Color.Transparent;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuNew,
this.mnuOpen,
this.mnuSave});
this.mnuFile.Text = "File";
//
// mnuNew
//
this.mnuNew.Index = 0;
this.mnuNew.OwnerDraw = true;
this.mnuNew.Text = "New";
this.mnuNew.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
this.mnuNew.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
//
// mnuOpen
//
this.mnuOpen.Index = 1;
this.mnuOpen.OwnerDraw = true;
this.mnuOpen.Text = "Open";
this.mnuOpen.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
this.mnuOpen.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
//
// mnuSave
//
this.mnuSave.Index = 2;
this.mnuSave.OwnerDraw = true;
this.mnuSave.Text = "Save";
this.mnuSave.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.mnu_DrawItem);
this.mnuSave.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.mnu_MeasureItem);
//
// OwnerDrawnMenu
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Menu = this.mainMenu1;
this.Name = "OwnerDrawnMenu";
this.Text = "Owner-Drawn Menu";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new OwnerDrawnMenu());
}
private void mnu_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
// Retrieve current item.
MenuItem mnuItem = (MenuItem)sender;
Font menuFont = new Font("Tahoma", 8);
// Measure size needed to display text.
// We add 30 pixels to the width to allow a generous spacing for the image.
e.ItemHeight = (int)e.Graphics.MeasureString(mnuItem.Text, menuFont).Height + 5;
e.ItemWidth = (int)e.Graphics.MeasureString(mnuItem.Text, menuFont).Width + 30;
}
private void mnu_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
// Retrieve current item.
MenuItem mnuItem = (MenuItem)sender;
// This defaults to the highlighted background if selected.
e.DrawBackground();
// Retrieve the image from an ImageList control.
Image menuImage = imgMenu.Images[mnuItem.Index];
// Draw the image.
e.Graphics.DrawImage(menuImage, e.Bounds.Left + 3, e.Bounds.Top + 2);
// Draw the text with the supplied colors and in the set region.
e.Graphics.DrawString(mnuItem.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + 25, e.Bounds.Top + 3);
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/OwnerDrawnMenu.zip">OwnerDrawnMenu.zip( 28 k)</a>
Owner Drawn Menu Control
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System.Drawing;
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Text;
namespace OwnerDrawnMenuControl
{
/// <summary>
/// Summary description for OwnerDrawnMenuControl.
/// </summary>
public class OwnerDrawnMenuControl : System.Windows.Forms.Form
{
internal System.Windows.Forms.MainMenu mainMenu1;
internal System.Windows.Forms.MenuItem mnuFile;
internal System.Windows.Forms.MenuItem mnuFonts;
internal System.Windows.Forms.ImageList imgMenu;
private System.ruponentModel.IContainer components;
public OwnerDrawnMenuControl()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OwnerDrawnMenuControl));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuFonts = new System.Windows.Forms.MenuItem();
this.imgMenu = new System.Windows.Forms.ImageList(this.ruponents);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFonts});
this.mnuFile.Text = "File";
//
// mnuFonts
//
this.mnuFonts.Index = 0;
this.mnuFonts.Text = "Fonts";
//
// imgMenu
//
this.imgMenu.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imgMenu.ImageSize = new System.Drawing.Size(16, 16);
this.imgMenu.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgMenu.ImageStream")));
this.imgMenu.TransparentColor = System.Drawing.Color.Transparent;
//
// OwnerDrawnMenuControl
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Menu = this.mainMenu1;
this.Name = "OwnerDrawnMenuControl";
this.Text = "MenuControlClient";
this.Load += new System.EventHandler(this.OwnerDrawnMenuControl_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new OwnerDrawnMenuControl());
}
private void OwnerDrawnMenuControl_Load(object sender, System.EventArgs e)
{
mnuFile.MenuItems.Add(new ImageMenuItem("New", imgMenu.Images[0]));
mnuFile.MenuItems.Add(new ImageMenuItem("Open", imgMenu.Images[1]));
mnuFile.MenuItems.Add(new ImageMenuItem("Save", imgMenu.Images[2]));
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
try
{
mnuFonts.MenuItems.Add(new ImageMenuItem(family.Name,
new Font(family, 10), null, Color.CornflowerBlue));
}
catch
{
// Catch invalid fonts/styles and ignore them.
}
}
}
}
public class ImageMenuItem : MenuItem
{
private Font font;
private Color foreColor;
private Image image;
public Font Font
{
get
{
return font;
}
set
{
font = value;
}
}
public Image Image
{
get
{
return image;
}
set
{
image = value;
}
}
public Color ForeColor
{
get
{
return foreColor;
}
set
{
foreColor = value;
}
}
public ImageMenuItem(string text, Font font, Image image, Color foreColor) : base(text)
{
this.Font = font;
this.Image = image;
this.ForeColor = foreColor;
this.OwnerDraw = true;
}
public ImageMenuItem(string text, Image image) : base(text)
{
// Choose a suitable default color and font.
this.Font = new Font("Tahoma", 8);
this.Image = image;
this.ForeColor = SystemColors.MenuText;
this.OwnerDraw = true;
}
protected override void OnMeasureItem(System.Windows.Forms.MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
// Measure size needed to display text.
e.ItemHeight = (int)e.Graphics.MeasureString(this.Text, this.Font).Height + 5;
e.ItemWidth = (int)e.Graphics.MeasureString(this.Text, this.Font).Width + 30;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
base.OnDrawItem(e);
// Determine whether disabled text is needed.
Color textColor;
if (this.Enabled == false)
{
textColor = SystemColors.GrayText;
}
else
{
e.DrawBackground();
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
textColor = SystemColors.HighlightText;
}
else
{
textColor = this.ForeColor;
}
}
// Draw the image.
if (Image != null)
{
if (this.Enabled == false)
{
ControlPaint.DrawImageDisabled(e.Graphics, Image,
e.Bounds.Left + 3, e.Bounds.Top + 2, SystemColors.Menu);
}
else
{
e.Graphics.DrawImage(Image, e.Bounds.Left + 3, e.Bounds.Top + 2);
}
}
// Draw the text with the supplied colors and in the set region.
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(textColor),
e.Bounds.Left + 25, e.Bounds.Top + 3);
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/OwnerDrawnMenuControl.zip">OwnerDrawnMenuControl.zip( 35 k)</a>
RadioCheck MenuItem
using System;
using System.Drawing;
using System.Windows.Forms;
class CheckAndRadioCheck: Form
{
MenuItem miColor, miFill;
public static void Main()
{
Application.Run(new CheckAndRadioCheck());
}
public CheckAndRadioCheck()
{
ResizeRedraw = true;
string[] astrColor = {"Black", "Blue", "Green", "Cyan",
"Red", "Magenta", "Yellow", "White"};
MenuItem[] ami = new MenuItem[astrColor.Length + 2];
EventHandler ehColor = new EventHandler(MenuFormatColorOnClick);
for (int i = 0; i < astrColor.Length; i++)
{
ami[i] = new MenuItem(astrColor[i], ehColor);
ami[i].RadioCheck = true;
}
miColor = ami[0];
miColor.Checked = true;
ami[astrColor.Length] = new MenuItem("-");
miFill = new MenuItem("&Fill",new EventHandler(MenuFormatFillOnClick));
ami[astrColor.Length + 1] = miFill;
MenuItem mi = new MenuItem("&Format", ami);
Menu = new MainMenu(new MenuItem[] {mi});
}
void MenuFormatColorOnClick(object obj, EventArgs ea)
{
miColor.Checked = false;
miColor = (MenuItem)obj;
miColor.Checked = true;
Invalidate();
}
void MenuFormatFillOnClick(object obj, EventArgs ea)
{
MenuItem mi = (MenuItem)obj;
mi.Checked ^= true;
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
if (miFill.Checked)
{
Console.WriteLine("fill");
}
else
{
Console.WriteLine("not fill");
}
}
}
Set MenuItem ShortCut
using System;
using System.Drawing;
using System.Windows.Forms;
class StandardMenu: Form
{
MenuItem miFileOpen, miFileSave;
MenuItem miEditCut, miEditCopy, miEditPaste;
bool bDocumentPresent = true;
bool bNonNullSelection = true;
bool bStuffInClipboard = false;
public static void Main()
{
Application.Run(new StandardMenu());
}
public StandardMenu()
{
Menu = new MainMenu();
MenuItem mi = new MenuItem("&File");
mi.Popup += new EventHandler(MenuFileOnPopup);
Menu.MenuItems.Add(mi);
int index = Menu.MenuItems.Count - 1;
miFileOpen = new MenuItem("&Open...");
miFileOpen.Click += new EventHandler(MenuFileOpenOnClick);
miFileOpen.Shortcut = Shortcut.CtrlO;
Menu.MenuItems[index].MenuItems.Add(miFileOpen);
miFileSave = new MenuItem("&Save");
miFileSave.Click += new EventHandler(MenuFileSaveOnClick);
miFileSave.Shortcut = Shortcut.CtrlS;
Menu.MenuItems[index].MenuItems.Add(miFileSave);
mi = new MenuItem("-");
Menu.MenuItems[index].MenuItems.Add(mi);
mi = new MenuItem("E&xit");
mi.Click += new EventHandler(MenuFileExitOnClick);
Menu.MenuItems[index].MenuItems.Add(mi);
mi = new MenuItem("&Edit");
mi.Popup += new EventHandler(MenuEditOnPopup);
Menu.MenuItems.Add(mi);
index = Menu.MenuItems.Count - 1;
miEditCut = new MenuItem("Cu&t");
miEditCut.Click += new EventHandler(MenuEditCutOnClick);
miEditCut.Shortcut = Shortcut.CtrlX;
Menu.MenuItems[index].MenuItems.Add(miEditCut);
miEditCopy = new MenuItem("&Copy");
miEditCopy.Click += new EventHandler(MenuEditCopyOnClick);
miEditCopy.Shortcut = Shortcut.CtrlC;
Menu.MenuItems[index].MenuItems.Add(miEditCopy);
miEditPaste = new MenuItem("&Paste");
miEditPaste.Click += new EventHandler(MenuEditCopyOnClick);
miEditPaste.Shortcut = Shortcut.CtrlV;
Menu.MenuItems[index].MenuItems.Add(miEditPaste);
mi = new MenuItem("&Help");
Menu.MenuItems.Add(mi);
index = Menu.MenuItems.Count - 1;
mi = new MenuItem("&About StandardMenu...");
mi.Click += new EventHandler(MenuHelpAboutOnClick);
Menu.MenuItems[index].MenuItems.Add(mi);
}
void MenuFileOnPopup(object obj, EventArgs ea)
{
miFileSave.Enabled = bDocumentPresent;
}
void MenuEditOnPopup(object obj, EventArgs ea)
{
miEditCut.Enabled = bNonNullSelection;
miEditCopy.Enabled = bNonNullSelection;
miEditPaste.Enabled = bStuffInClipboard;
}
void MenuFileOpenOnClick(object obj, EventArgs ea)
{
MessageBox.Show("File Open dialog box!", Text);
}
void MenuFileSaveOnClick(object obj, EventArgs ea)
{
MessageBox.Show("File Save dialog box!", Text);
}
void MenuFileExitOnClick(object obj, EventArgs ea)
{
Close();
}
void MenuEditCutOnClick(object obj, EventArgs ea)
{
}
void MenuEditCopyOnClick(object obj, EventArgs ea)
{
}
void MenuEditPasteOnClick(object obj, EventArgs ea)
{
}
void MenuHelpAboutOnClick(object obj, EventArgs ea)
{
MessageBox.Show("StandardMenu ?", Text);
}
}
Subclass MenuItem
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterContextMenu: Form
{
MenuItemColor micColor;
public static void Main()
{
Application.Run(new BetterContextMenu());
}
public BetterContextMenu()
{
Text = "Better Context Menu Demo";
EventHandler eh = new EventHandler(MenuColorOnClick);
MenuItemColor[] amic =
{
new MenuItemColor(Color.Black, "&Black", eh),
new MenuItemColor(Color.Blue, "B&lue", eh),
new MenuItemColor(Color.Green, "&Green", eh),
new MenuItemColor(Color.Cyan, "&Cyan", eh),
new MenuItemColor(Color.Red, "&Red", eh),
new MenuItemColor(Color.Magenta, "&Magenta", eh),
new MenuItemColor(Color.Yellow, "&Yellow", eh),
new MenuItemColor(Color.White, "&White", eh)
};
foreach (MenuItemColor mic in amic)
mic.RadioCheck = true;
micColor = amic[3];
micColor.Checked = true;
BackColor = micColor.Color;
ContextMenu = new ContextMenu(amic);
}
void MenuColorOnClick(object obj, EventArgs ea)
{
micColor.Checked = false;
micColor = (MenuItemColor) obj;
micColor.Checked = true;
BackColor = micColor.Color;
}
}
class MenuItemColor: MenuItem
{
Color clr;
public MenuItemColor(Color clr, string str, EventHandler eh):
base(str, eh)
{
Color = clr;
}
public Color Color
{
get { return clr; }
set { clr = value; }
}
}