Draw the menu of Office2003 style with C #

xiaoxiao2021-03-06  42

/ ********************* CREATED by chen ********************************* **

* If you feel that I have a good article, please refer to the work of the work, explanation

* Source and original author, Thank you !!! email: Aishen944-sohu.com

*********************************************************** ***************** /

/ *

My method is to rewrite the Menuitem class. I found that the size of the text that is actually measured in the process of writing is not very accurate. There is also a text that I can't align, I can see a lot of plus numbers in the code. I don't want to talk about it. The following code annotation is very clear:

* / using system.drawing; using system.drawing.drawing2d; using system.windows.forms; Namespace OfficeMenu {////

/// Class1 Summary Description. /// public class OfficeMenuItem: MenuItem {public OfficeMenuItem (bool IsApplyOfficeMenu, string ItemText) {if (IsApplyOfficeMenu) {this.Text = ItemText; this.OwnerDraw = true; font = new Font ( "Arial", 9f , GraphicsUnit.point;} else {this.text = itemtext; this.ownerdraw = false;}}

Private font font; private stringformat strformat = new stringFormat (); private string itemtext = "; private string iconpath ="

// define when to draw the various brushes private LinearGradientBrush TopMenuHostLightBrush; private SolidBrush TopMenuDefaultBrush; private SolidBrush FontDefaultBrush; private LinearGradientBrush TopMenuSelectedBrush; private SolidBrush MenuDefaultBrush; private SolidBrush MenuDefaultSelectedBrush; private SolidBrush MenuCheckBrush; private LinearGradientBrush MenuSideBarBrush; private SolidBrush MenuDefaultGrayedBrush; // define when required the various lines drawn private Pen TopMenuPen; private Pen MenuDefaultPen; // define the desired rectangular configuration private rectangle SideBarRect; private rectangle CheckedRect; private rectangle TopMenuRect; private rectangle ShortcutRect;

/ / Define icon Public String Iconpath {get {returnpath;} set {iconpath = value;}}

/ / Decision Whether the menu is a top-level menu private bool checkistop () {g (mainmenu) == this.parent.gettype ()) {return true;} else {return false;}} // Get menu shortcut PRIVATE String getShortcut () {string shortcut = ""; try {ife (this.showshcut == true&& (! this.isparent) && (! (this.shortcut == shortcut.none))) {Shortcut = this.shortcut. Totring (); shortcut = shortcut.replace ("ctrl", "ctrl "); shortcut = shortcut.replace ("alt", "alt "); shortcut = shortcut.replace ("shift", "shift ");} } catch {return "";} return shortcut;} // override measurement function protected override void OnMeasureItem (MeasureItemEventArgs e) {base.OnMeasureItem (e); try {itemText = this.Text; SizeF SizeMenuText; if (this.ItemText == "-") // Set the height of the division line {E.ItemHeight = 5; Return;}

String getshortcut = this.getshortcut (); if (! this.checkistop ()) // If not the top menu {sizeMenutext = E.Graphics.MeasureString (itemtext "". Padright (4, ') Getshortcut, this.font ); e.ItemWidth = Convert.ToInt32 (SizeMenuText.Width) 26;} else {SizeMenuText = e.Graphics.MeasureString (itemText, this.font); e.ItemWidth = Convert.ToInt32 (SizeMenuText.Width) -4; } If (this.visible) {E.ItemHeight = Convert.Toint32 (SIZEMENUTEXT.HEIGHT) 6;} else {E.ItemHeight = 0;}} catch {E.itemHeight = 20; E.Itemwidth = 80;}} // override mapping function protected override void OnDrawItem (DrawItemEventArgs e) {if (this.Visible) {base.OnDrawItem (e); Rectangle MenuRect = e.Bounds; itemText = this.Text; Graphics g = e.Graphics; / / Draw the special rectangular SidebarRect = new rectangle (Menurect.x, Menurect.y, 25, meNurect.x, Menurect.y, 25, Menurect.x, Menurect.y, 25, Menurect.x, Menurect.y, 25, Menurect.x 1, Menurect.x 1, menurect. Y 1, 24, meneurect.height-2); // Draw the rectangular TopMenurect = New Rectangle (Menurect) when drawing the top menu .X 1, menurect.y 3, menurect.width, meneurect.height); // Draw a rectangular shortcutRectRect = New Rectangle (Menurect.x, Menurect.y 4, Menurect.width-10, Menurect.x, Menurect.y 4, Menurect.Width-10, Menurect.width-10, Menurect.width-10, menurect .Height);

A variety of brushes TopMenuHostLightBrush // define when plotted = new LinearGradientBrush (MenuRect, Color.White, Color.FromArgb (220,134,56), 90f); // color TopMenuDefaultBrush top menu displayed when the occurrence HOTLIGHT = new SolidBrush (SystemColors .Control); // top-level menu default color FontDefaultBrush = new SolidBrush (Color.Black); // default font color TopMenuSelectedBrush = new LinearGradientBrush (MenuRect, Color.White, Color.FromArgb (95,109,126), 90f); // top menu color MenuDefaultBrush when selected = new SolidBrush (Color.White); // default menu color MenuDefaultSelectedBrush = new SolidBrush (Color.FromArgb (247,218,157)); // color MenuCheckBrush when the menu is selected = new SolidBrush (Color.FromArgb (225,143,70)); // CHECKED attribute is true when the menu display color MenuSideBarBrush = new LinearGradientBrush (SideBarRect, Color.White, Color.FromArgb (149,160,174), 0f); // draw sidebar MenuDefaultGrayedBrush = new SolidBrush (Color.Fromargb (105, 105, 105)); // Menu When you are not available // Draw a variety of lines required to draw a variety of lines TopMenupen = new pen (brushes.black, 1f); // Top menu Border Menudefaultpen = New Pen (New " Solidbrush (60, 60, 115)), 1F); // Normal selection border // start to draw ********************************* *********** **************** i (this.checkistop () && this.ItemText! = "-") // If it is a top-level menu {bool isdraw = false; // Define one variables ensure normal display only once to avoid wasting resources drawing strFormat.Alignment = StringAlignment.Center; if (IsDraw == false) // drawn image {g.FillRectangle (TopMenuDefaultBrush, MenuRect) when the normal display; g.DrawString (itemText, THIS.FONT, FONTDEFAULTBRUSH, TOPMENURECT, STRFORMAT;

When the image // if ((e.State & DrawItemState.HotLight) == DrawItemState.HotLight) // highlighted when drawing HOTLIGHT {g.FillRectangle (TopMenuHostLightBrush, MenuRect); g.DrawRectangle (TopMenuPen, MenuRect.X, MenuRect .Y, meNurect.Height-1); g.drawstring (itemtext, this.font, fontdefaultbrush, topmenurect, strformat); isDraw = true;} // Draw an image IF when the menu is selected ((e .State & DrawItemState.Selected) == DrawItemState.Selected) {g.FillRectangle (TopMenuSelectedBrush, MenuRect); g.DrawRectangle (TopMenuPen, MenuRect.X, MenuRect.Y, MenuRect.Width-1, MenuRect.Height-1); g.drawstring (ItemText, THIS.FONT, TOPMENURECT, Strformat); isDraw = true;}}}} Else // Non-top Menu {IF (this.ItemText! = "-") {bool isdraw = false; if ( IsDraw == false) // normal display {g.FillRectangle (MenuDefaultBrush, MenuRect); g.FillRectangle (MenuSideBarBrush, MenuRect.X, MenuRect.Y, 25, MenuRect.Height); g.DrawString (itemText, this.font , Fontdefaultbrush, Menurect .X 26, MenuRect.Y 4); // draw shortcut string shortcut = this.GetShortCut (); strFormat.Alignment = StringAlignment.Far; g.DrawString (shortcut, this.font, FontDefaultBrush, ShortcutRect, strFormat); // Draw icon this.drawicon (g);}} == DrawItemState.selected) // Select {this.drawItem_selected (g, meneurect, itemtext); isdraw = true;

if ((e.State & DrawItemState.Grayed) == DrawItemState.Grayed) // not available {g.DrawString (ItemText, this.font, MenuDefaultGrayedBrush, MenuRect.X 26, MenuRect.Y 4); IsDraw = true }

if (! this.IsParent) {if ((e.State & DrawItemState.Checked) == DrawItemState.Checked) // selecting {g.FillRectangle (MenuCheckBrush, CheckedRect); g.DrawRectangle (new Pen (Brushes.Black, 1F), MENURECT.X 1, Menurect.Y 1, 23, Menurect.Height-3); g.drawstring ("√", this.font, fontdefaultbrush, menurect.x 3, menurect.y 4) Isdraw = true;} if (this.checked == true && ((e.State & DrawItemState.Selected) == DrawItemState.Selected)) // Select and checkage attributes {G.FillRectangle (New Solidbrush (Color .Fromargb (255, 91, 91)), checkedRect; g.drawRectangle (New Pen (brushes.black, 1f), menurect.x 1, menurect.height-3); g .Drawstring ("√", this.font, fontdefaultbrush, meneticct.x ​​ 3, menurect.y 4); isDraw = true;}}} else // Draw split line {Solidbrush Partitionbrush = New Solidbrush (Color.Fromargb 128, 128, 128)); Pen Partitionpen = New Pen (Partitionlinebrush, 1F); g.FillRectangle (MenudeFaultBrush, meNurect); g.FillRectangle (M EnusideBarbrush, Menurect.x, Menurect.y, 25, meNurect.Height); g.drawline (Partitionpen, New Point (Menurect.x 27, Menurect.y 2), New Point (Menurect.x 27 (Menurect) .Width-26), menurect.y 2));

}}}

{IF (this.checkistop () == false && this.isparent == false && this.checked == false && this.checked == false {bit {bitmap iconimage = new Bitmap (this.IconPath); int imageWidth = iconImage.Width; int imageHeight = iconImage.Height; if (imageWidth <= CheckedRect.Width && imageHeight <= CheckedRect.Height) {int ImageRecWidthMistake = CheckedRect.Width-imageWidth; float Mistake_2 = (float) ImageRecWidthMistake / 2f; g.DrawImage (iconImage, CheckedRect.X (int) Mistake_2, CheckedRect.Y, imageWidth, imageHeight);} else {g.DrawImage (iconImage, CheckedRect);}} catch {throw new IConNotFindException ("You can't create icons, it may be that the path cannot be also not supported");}}} // Due to the selection of the selected action, the same code is required to use a function. private void DrawItem_Selected (Graphics g, Rectangle rec, string itemtext) {SolidBrush tmpBrush; if (this.Enabled) {tmpBrush = FontDefaultBrush;} else {tmpBrush = MenuDefaultGrayedBrush;} // normal display g.FillRectan Gle (MenudefaultSelectedbrush, REC); G.drawRectangle (Menudefaultpen, Rec.x, Rec.y, Rec.width-1, Rec.Height-1); G.DrawString (itemtext, this.font, Tmpbrush, Rec.x 26, REC.Y 4);

// display the shortcut string shortcut = this.GetShortCut (); strFormat.Alignment = StringAlignment.Far; g.DrawString (shortcut, this.font, tmpBrush, ShortcutRect, strFormat); // Videos icon this.DrawIcon (g);} }

// Custom Exception PUBLIC CLASS ICONNOTFINDEXCEPTION: ApplicationException {PUBLIC ICONNOTFINDEXCEPTION (STRING MESSAGE): BASE (MESSAGE) {}}}

转载请注明原文地址:https://www.9cbs.com/read-72457.html

New Post(0)