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; } }
GUI Windows Form
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.ComponentModel; 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.ComponentModel.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.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OwnerDrawnMenu)); this.imgMenu = new System.Windows.Forms.ImageList(this.components); 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); } } } OwnerDrawnMenu.zip( 28 k)
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.ComponentModel; 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.ComponentModel.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.components = new System.ComponentModel.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.components); // // 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); } } } OwnerDrawnMenuControl.zip( 35 k)
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); } } [/csharp]
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"); } } } [/csharp]
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; } } }
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); } } [/csharp]