MenuItem.DrawItem イベントとは? わかりやすく解説

MenuItem.DrawItem イベント

メニュー項目の OwnerDraw プロパティtrue設定されていて、そのメニュー項目の描画要求され場合発生します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event DrawItem As
 DrawItemEventHandler
Dim instance As MenuItem
Dim handler As DrawItemEventHandler

AddHandler instance.DrawItem, handler
public event DrawItemEventHandler DrawItem
public:
event DrawItemEventHandler^ DrawItem {
    void add (DrawItemEventHandler^ value);
    void remove (DrawItemEventHandler^ value);
}
/** @event */
public void add_DrawItem (DrawItemEventHandler
 value)

/** @event */
public void remove_DrawItem (DrawItemEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説
使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からMenuItem.DrawItem イベントを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMenuItem.DrawItem イベントを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMenuItem.DrawItem イベント を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MenuItem.DrawItem イベント」の関連用語

MenuItem.DrawItem イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MenuItem.DrawItem イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS