C#でメニューを初めて使ってみた
メインメニューなるものを「C#によるプログラミングWindows上」本を見ながら作ってみました。
ホントにメニューのフォントやサイズを変えられるんだ。
なんかどろくさいけどw
「BigMenuItem」クラス
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace ABCS.UserClass { class BigMenuItem : MenuItem { public BigMenuItem(string strgText, EventHandler ehClick) : base(strgText, ehClick) { this.setInit(); } public BigMenuItem(string strgText) : base(strgText) { this.setInit(); } public BigMenuItem() : base() { this.setInit(); } private void setInit() { this.MeasureItem += this.ehOnMeasureItem; this.DrawItem += this.ehOnDrawItem; this.OwnerDraw = true; } private int iFontPointSuze_ = 16; public int iFontPointSize { get { return this.iFontPointSuze_; } set { this.iFontPointSuze_ = value; } } protected void ehOnMeasureItem(object sender, MeasureItemEventArgs e) { MenuItem mi = sender as MenuItem; if (mi != null) { Font font = new Font(mi.Text, iFontPointSize); StringFormat strgfmt = new StringFormat(); strgfmt.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show; SizeF sizef = e.Graphics.MeasureString(mi.Text, font, 1000, strgfmt); e.ItemWidth = (int)Math.Ceiling(sizef.Width); e.ItemHeight = (int)Math.Ceiling(sizef.Height); e.ItemWidth += SystemInformation.MenuCheckSize.Width * e.ItemHeight / SystemInformation.MenuCheckSize.Height; e.ItemWidth -= SystemInformation.MenuCheckSize.Width; } } protected void ehOnDrawItem(object sender, DrawItemEventArgs e) { MenuItem mi = sender as MenuItem; if (mi != null) { Graphics grfx = e.Graphics; Brush brush; Font font = new Font(mi.Text, iFontPointSize); StringFormat strgfmt = new StringFormat(); strgfmt.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show; Rectangle rectCheck = e.Bounds; rectCheck.Width = SystemInformation.MenuCheckSize.Width * rectCheck.Height / SystemInformation.MenuCheckSize.Height; Rectangle rectText = e.Bounds; rectText.X += rectCheck.Width; e.DrawBackground(); if ((e.State & DrawItemState.Checked) != 0) { ControlPaint.DrawMenuGlyph(grfx, rectCheck, MenuGlyph.Bullet); } if ((e.State & DrawItemState.Selected) != 0) { brush = SystemBrushes.HighlightText; } else { brush = SystemBrushes.ControlText; } grfx.DrawString(mi.Text, font, brush, rectText, strgfmt); } } } }
「BigCheckMenuItem」クラス
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ABCS.UserClass { class BigCheckMenuItem : BigMenuItem { public BigCheckMenuItem(string strgText) : base(strgText) { this.setInit(); } protected void setInit() { this.Click += ehOnClick; } protected void ehOnClick(object sender, EventArgs e) { this.Checked = !this.Checked; } } }
メインメニューを作る、makeMainMenu()関数
private void makeMainMenu() { string ret = ""; Menu = new MainMenu(); //「ファイル」メニュー Menu.MenuItems.Add("ファイル"); Menu.MenuItems[0].MenuItems.Add(new BigMenuItem("印刷")); //「ソフトキーボード」メニュー Menu.MenuItems.Add("ソフトキーボード"); BigMenuItem[] bmi = new BigMenuItem[4]; bmi[0] = new BigCheckMenuItem("ソーシャルIME予測変換"); settingsSingleton.getValue(settingsSingleton.KeyUsePredictSocialIME, out ret); if (settingsSingleton.TRUE.Equals(ret)) bmi[0].Checked = true; bmi[0].Click += this.saveSettingSocialIMEPredict; this.SocialIMEPredictMenuItem = bmi[0]; ret = ""; bmi[1] = new BigCheckMenuItem("ソーシャルIME通常変換"); settingsSingleton.getValue(settingsSingleton.KeyUseNormalSocialIME, out ret); if (settingsSingleton.TRUE.Equals(ret)) bmi[1].Checked = true; bmi[1].Click += this.saveSettingSocialIMENormal; this.SocialIMENormalMenuItem = bmi[1]; ret = ""; bmi[2] = new BigCheckMenuItem("VJE予測変換"); settingsSingleton.getValue(settingsSingleton.KeyUsePredictVJE, out ret); if (settingsSingleton.TRUE.Equals(ret)) bmi[2].Checked = true; bmi[2].Click += this.saveSettingVJEPredict; this.VJEPredictMenuItem = bmi[2]; ret = ""; bmi[3] = new BigCheckMenuItem("VJE通常変換"); settingsSingleton.getValue(settingsSingleton.KeyUseNormalVJE, out ret); if (settingsSingleton.TRUE.Equals(ret)) bmi[3].Checked = true; bmi[3].Click += this.saveSettingVJENormal; this.VJEPredictMenuItem = bmi[3]; Menu.MenuItems[1].MenuItems.AddRange(bmi); }