この章では、オーナードローするメニュー項目をMenuItemクラスを継承した自作クラスから作ってみます。
MenuItemクラスを継承することにより、OnMeasureItem,OnDrawItemメソッドをオーバーライドできるようになります。
あとは、前章と全く同じ方法です。
では、サンプルを見てみましょう。
// contextmenu04.cs
using System;
using System.Drawing;
using System.Windows.Forms;
class contextmenu04
{
public static void Main()
{
Application.Run(new MyForm());
}
}
class MyForm : Form
{
public MyForm()
{
Text = "猫でもわかるC#";
BackColor = SystemColors.Window;
ContextMenu contextmenu = new ContextMenu();
ContextMenu = contextmenu;
MenuItem miFile = new MenuItem("ファイル(&F)");
contextmenu.MenuItems.Add(miFile);
MyOwnerDraw mo = new MyOwnerDraw();
miFile.MenuItems.Add(mo);
MenuItem miExit = new MenuItem("終了(&F)");
miExit.Click += new EventHandler(miExit_Click);
miFile.MenuItems.Add(miExit);
}
void miExit_Click(object sender, EventArgs e)
{
Close();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
DialogResult dr;
dr = MessageBox.Show("本当に終了してもよろしいですか",
"猫C#",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Yes)
e.Cancel = false;
else
e.Cancel = true;
}
}
class MyOwnerDraw : MenuItem
{
Bitmap bmpcat;
public MyOwnerDraw()
{
bmpcat = new Bitmap(GetType(), "contextmenu04.cat.gif");
OwnerDraw = true;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("猫をクリックしたね",
"猫C#",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
e.ItemWidth = bmpcat.Width;
e.ItemHeight = bmpcat.Height;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
Rectangle rc = e.Bounds;
Graphics g = e.Graphics;
rc.X = (rc.Width - bmpcat.Width) / 2;
rc.Width = bmpcat.Width;
e.DrawBackground();
g.DrawImage(bmpcat, rc);
}
}
MyFormクラスのコンストラクタで、
MyOwnerDraw mo = new MyOwnerDraw();と、している点に注意してください。自作クラスMyOwnerDrawのオブジェクトを生成しています。そして、
miFile.MenuItems.Add(mo);として、「ファイル」のサブメニュー項目としてAddしています。
自作MyOwnerDrawクラスは、MenuItemクラスから継承してきています。
MyOwnerクラスのコンストラクタで、OwnerDrawプロパティをtrueにしています。
あとは、オーナードローの方法は全く同じです。
実行結果は次のようになります。
Update 10/Nov/2006 By Y.Kumei