MenuItem.DrawItem イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public: event DrawItemEventHandler^ DrawItem { void add (DrawItemEventHandler^ value); void remove (DrawItemEventHandler^ value); }

DrawItem イベント ハンドラに渡された DrawItemEventArgs 引数は、Graphics オブジェクトを提供し、メニュー項目の表面で描画操作やその他のグラフィック操作を実行できるようにします。このイベント ハンドラを使用して、アプリケーションの要件を満たすカスタム メニューを作成できます。イベント処理の詳細については、「イベントの利用」を参照してください。

DrawItem イベントを処理する方法を次のコード例に示します。この例では、Brush および Font を使用してメニュー項目を描画し、続いてメニュー項目の周りに Rectangle を描画しています。描画は Graphics オブジェクトを通じて実行されます。このオブジェクトは DrawItemEventArgs パラメータのイベント ハンドラに渡されます。この例では、項目の OwnerDraw プロパティを true に初期化済みであることが必要です。C# の例では、InitializeComponent の後、フォームのコンストラクタで次のコードを追加してイベントをフックします。
this.menuItem1.DrawItem += new DrawItemEventHandler(menuItem1_DrawItem);
' The DrawItem event handler. Private Sub MenuItem1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem1.DrawItem Dim MyCaption As String = "Owner Draw Item1" ' Create a Brush and a Font with which to draw the item. Dim MyBrush As System.Drawing.Brush = System.Drawing.Brushes.AliceBlue Dim MyFont As New Font(FontFamily.GenericSerif, 14, FontStyle.Underline, GraphicsUnit.Pixel) Dim MySizeF As SizeF = e.Graphics.MeasureString(MyCaption, MyFont) ' Draw the item, and then draw a Rectangle around it. e.Graphics.DrawString(MyCaption, MyFont, MyBrush, e.Bounds.X, e.Bounds.Y) e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, MySizeF.Width, MySizeF.Height)) End Sub
// The DrawItem event handler. private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { string myCaption = "Owner Draw Item1"; // Create a Brush and a Font with which to draw the item. Brush myBrush = System.Drawing.Brushes.AliceBlue; Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline, GraphicsUnit.Pixel); SizeF mySizeF = e.Graphics.MeasureString(myCaption, myFont); // Draw the item, and then draw a Rectangle around it. e.Graphics.DrawString(myCaption, myFont, myBrush, e.Bounds.X, e.Bounds.Y); e.Graphics.DrawRectangle(Pens.Black, new Rectangle(e.Bounds.X, e.Bounds.Y, Convert.ToInt32(mySizeF.Width), Convert.ToInt32(mySizeF.Height))); }
// The DrawItem event handler. private: void menuItem1_DrawItem( Object^ /*sender*/, System::Windows::Forms::DrawItemEventArgs^ e ) { String^ myCaption = "Owner Draw Item1"; // Create a Brush and a Font with which to draw the item. Brush^ myBrush = System::Drawing::Brushes::AliceBlue; System::Drawing::Font^ myFont = gcnew System::Drawing::Font( FontFamily::GenericSerif,14,FontStyle::Underline,GraphicsUnit::Pixel ); SizeF mySizeF = e->Graphics->MeasureString( myCaption, myFont ); // Draw the item, and then draw a Rectangle around it. e->Graphics->DrawString( myCaption, myFont, myBrush, (float)e->Bounds.X, (float)e->Bounds.Y ); e->Graphics->DrawRectangle( Pens::Black, Rectangle(e->Bounds.X,e->Bounds.Y,Convert::ToInt32( mySizeF.Width ),Convert::ToInt32( mySizeF.Height )) ); }
// The DrawItem event handler. private void menuItem1_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e) { String myCaption = "Owner Draw Item1"; // Create a Brush and a Font with which to draw the item. Brush myBrush = System.Drawing.Brushes.get_AliceBlue(); Font myFont = new Font(FontFamily.get_GenericSerif(), 14, FontStyle.Underline, GraphicsUnit.Pixel); SizeF mySizeF = e.get_Graphics().MeasureString(myCaption, myFont); // Draw the item, and then draw a Rectangle around it. e.get_Graphics().DrawString(myCaption, myFont, myBrush, e.get_Bounds().get_X(), e.get_Bounds().get_Y()); e.get_Graphics().DrawRectangle(Pens.get_Black(), new Rectangle(e.get_Bounds().get_X(), e.get_Bounds().get_Y(), Convert.ToInt32(mySizeF.get_Width()), Convert.ToInt32(mySizeF.get_Height()))); } //menuItem1_DrawItem

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からMenuItem.DrawItem イベントを検索する場合は、下記のリンクをクリックしてください。

- MenuItem.DrawItem イベントのページへのリンク