MeasureItemEventArgsとは? わかりやすく解説

MeasureItemEventArgs クラス

ListBoxComboBox、CheckedListBox、MenuItem の各コントロールMeasureItem イベントデータ提供します

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

Public Class MeasureItemEventArgs
    Inherits EventArgs
Dim instance As MeasureItemEventArgs
public class MeasureItemEventArgs : EventArgs
public ref class MeasureItemEventArgs : public
 EventArgs
public class MeasureItemEventArgs extends EventArgs
public class MeasureItemEventArgs extends
 EventArgs
解説解説
使用例使用例
Public Class Form1
   Inherits System.Windows.Forms.Form
   Private WithEvents listBox1 As
 System.Windows.Forms.ListBox
   Private components As System.ComponentModel.Container
 = Nothing

   Private FontSize As Single
 = 12.0F

   '
   '  This sample displays a ListBox that contains a list of all the
 fonts
   '  installed on the system and draws each item in its respective
 font.
   '
   Public Sub New()
      InitializeComponent()

      ' Populate control with the fonts installed on the system.
      Dim families As FontFamily() = FontFamily.Families

      Dim family As FontFamily
      For Each family In
 families
         Dim style As FontStyle = FontStyle.Regular

         ' Monotype Corsiva is only available in italic
         If family.Name = "Monotype Corsiva"
 Then
            style = style Or FontStyle.Italic
         End If

         listBox1.Items.Add(New ListBoxFontItem(New
 Font(family.Name, FontSize, style, GraphicsUnit.Point)))
      Next family
   End Sub


   Protected Overloads Overrides
 Sub Dispose(ByVal disposing As
 Boolean)
      If disposing Then
         If Not (components Is
 Nothing) Then
            components.Dispose()
         End If

         If Not (foreColorBrush Is
 Nothing) Then
            foreColorBrush.Dispose()
         End If
      End If

      MyBase.Dispose(disposing)
   End Sub

   Private Sub InitializeComponent()
      Me.listBox1 = New System.Windows.Forms.ListBox()
      Me.SuspendLayout()
      ' 
      ' listBox1
      ' 
      Me.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
      Me.listBox1.Location = New System.Drawing.Point(16,
 48)
      Me.listBox1.Name = "listBox1"
      Me.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
      Me.listBox1.Size = New System.Drawing.Size(256,
 134)
      Me.listBox1.TabIndex = 0
      ' 
      ' Form1
      ' 
      Me.ClientSize = New System.Drawing.Size(292,
 273)
      Me.Controls.AddRange(New System.Windows.Forms.Control()
 {Me.listBox1})
      Me.Name = "Form1"
      Me.Text = "Form1"
      Me.ResumeLayout(False)
   End Sub

   <STAThread()> Shared Sub Main()
      Application.Run(New Form1())
   End Sub

   Private Sub listBox1_MeasureItem(ByVal
 sender As Object, ByVal
 e As System.Windows.Forms.MeasureItemEventArgs) Handles listBox1.MeasureItem
      Dim font As Font = CType(listBox1.Items(e.Index),
 ListBoxFontItem).Font
      Dim stringSize As SizeF = e.Graphics.MeasureString(font.Name,
 font)

      ' Set the height and width of the item
      e.ItemHeight = CInt(stringSize.Height)
      e.ItemWidth = CInt(stringSize.Width)
   End Sub

   ' For efficiency, cache the brush used for drawing.
   Private foreColorBrush As SolidBrush

   Private Sub listBox1_DrawItem(ByVal
 sender As Object, ByVal
 e As System.Windows.Forms.DrawItemEventArgs) Handles listBox1.DrawItem
      Dim brush As Brush

      ' Create the brush using the ForeColor specified by the DrawItemEventArgs
      If foreColorBrush Is Nothing
 Then
         foreColorBrush = New SolidBrush(e.ForeColor)
      Else
         If Not foreColorBrush.Color.Equals(e.ForeColor)
 Then
            ' The control's ForeColor has changed, so dispose of the
 cached brush and
            ' create a new one.
            foreColorBrush.Dispose()
            foreColorBrush = New SolidBrush(e.ForeColor)
         End If
      End If

      ' Select the appropriate brush depending on if the item is selected.
      ' Since State can be a combinateion (bit-flag) of enum values,
 you can't use
      ' "==" to compare them.
      If (e.State And DrawItemState.Selected)
 = DrawItemState.Selected Then
         brush = SystemBrushes.HighlightText
      Else
         brush = foreColorBrush
      End If

      ' Perform the painting.
      Dim font As Font = CType(listBox1.Items(e.Index),
 ListBoxFontItem).Font
      e.DrawBackground()
      e.Graphics.DrawString(font.Name, font, brush, e.Bounds.X, e.Bounds.Y)
      e.DrawFocusRectangle()
   End Sub

   '
   '  A wrapper class for use with storing Fonts in a ListBox.  Since
 ListBox uses the
   '  ToString() of its items for the text it displays, this class is
 needed to return
   '  the name of the font, rather than its ToString() value.
   '
   Public Class ListBoxFontItem
      Public Font As Font

      Public Sub New(ByVal
 f As Font)
         Font = f
      End Sub

      Public Overrides Function
 ToString() As String
         Return Font.Name
      End Function
   End Class
End Class
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.ListBox listBox1;
   private System.ComponentModel.Container components = null;


   protected override void Dispose(bool
 disposing)
   {
      if( disposing )
      {
         if ( components != null ) 
            components.Dispose();

         if ( foreColorBrush != null )
            foreColorBrush.Dispose();
      }
      base.Dispose(disposing);
   }

     #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
      this.listBox1.Location = new System.Drawing.Point(16,
 48);
      this.listBox1.Name = "listBox1";
      this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
      this.listBox1.Size = new System.Drawing.Size(256,
 134);
      this.listBox1.TabIndex = 0;
      this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);
      this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
      // 
      // Form1
      // 
      this.ClientSize = new System.Drawing.Size(292,
 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[]
 {
                                                                   this.listBox1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);

   }
     #endregion

   [STAThread]
   static void Main() 
   {
      Application.Run(new Form1());
   }

   private void listBox1_MeasureItem(object
 sender, System.Windows.Forms.MeasureItemEventArgs e)
   {
      Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
      SizeF stringSize = e.Graphics.MeasureString(font.Name, font);

      // Set the height and width of the item
      e.ItemHeight = (int)stringSize.Height;
      e.ItemWidth = (int)stringSize.Width;
   }

   // For efficiency, cache the brush to use for drawing.
   private SolidBrush foreColorBrush;

   private void listBox1_DrawItem(object sender,
 System.Windows.Forms.DrawItemEventArgs e)
   {
      Brush brush;

      // Create the brush using the ForeColor specified by the DrawItemEventArgs
      if ( foreColorBrush == null )
         foreColorBrush = new SolidBrush(e.ForeColor);
      else if ( foreColorBrush.Color != e.ForeColor
 )
      {
         // The control's ForeColor has changed, so dispose of the cached
 brush and
         // create a new one.
         foreColorBrush.Dispose();
         foreColorBrush = new SolidBrush(e.ForeColor);
      }

      // Select the appropriate brush depending on if the item is selected.
      // Since State can be a combinateion (bit-flag) of enum values,
 you can't use
      // "==" to compare them.
      if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected
 )
         brush = SystemBrushes.HighlightText;
      else
         brush = foreColorBrush;

      // Perform the painting.
      Font font = ((ListBoxFontItem)listBox1.Items[e.Index]).Font;
      e.DrawBackground();
      e.Graphics.DrawString(font.Name, font, brush, e.Bounds);
      e.DrawFocusRectangle();
   }

   /// <summary>
   ///  A wrapper class for use with storing Fonts in a ListBox.  Since
 ListBox uses the
   ///  ToString() of its items for the text it displays, this class
 is needed to return
   ///  the name of the font, rather than its ToString() value.
   /// </summary>
   public class ListBoxFontItem 
   {
      public Font Font;

      public ListBoxFontItem(Font f) 
      {
         Font = f;
      }

      public override string ToString() 
      {
         return Font.Name;
      }
   }
}
public ref class Form1: public
 System::Windows::Forms::Form
{
private:
   System::Windows::Forms::ListBox^ listBox1;
   System::ComponentModel::Container^ components;

public:
   ~Form1()
   {
      if ( components != nullptr )
      {
         delete components;
      }
   }

private:

   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   void InitializeComponent()
   {
      this->listBox1 = gcnew System::Windows::Forms::ListBox;
      this->SuspendLayout();

      // 
      // listBox1
      // 
      this->listBox1->DrawMode = System::Windows::Forms::DrawMode::OwnerDrawVariable;
      this->listBox1->Location = System::Drawing::Point(
 16, 48 );
      this->listBox1->Name = "listBox1";
      this->listBox1->SelectionMode = System::Windows::Forms::SelectionMode::MultiExtended;
      this->listBox1->Size = System::Drawing::Size( 256,
 134 );
      this->listBox1->TabIndex = 0;
      this->listBox1->MeasureItem += gcnew System::Windows::Forms::MeasureItemEventHandler(
 this, &Form1::listBox1_MeasureItem );
      this->listBox1->DrawItem += gcnew System::Windows::Forms::DrawItemEventHandler(
 this, &Form1::listBox1_DrawItem );

      // 
      // Form1
      // 
      this->ClientSize = System::Drawing::Size( 292, 273 );
      array<System::Windows::Forms::Control^>^temp0 = {this->listBox1};
      this->Controls->AddRange( temp0 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );
   }

   void listBox1_MeasureItem( Object^ /*sender*/, MeasureItemEventArgs^
 e )
   {
      System::Drawing::Font^ font = (dynamic_cast<ListBoxFontItem^>(listBox1->Items[
 e->Index ]))->Font;
      SizeF stringSize = e->Graphics->MeasureString( font->Name, font );
      
      // Set the height and width of the item
      e->ItemHeight = (int)stringSize.Height;
      e->ItemWidth = (int)stringSize.Width;
   }

   // For efficiency, cache the brush to use for drawing.
   SolidBrush^ foreColorBrush;
   void listBox1_DrawItem( Object^ /*sender*/, DrawItemEventArgs^
 e )
   {
      Brush^ brush;

      // Create the brush using the ForeColor specified by the DrawItemEventArgs
      if ( foreColorBrush == nullptr )
            foreColorBrush = gcnew SolidBrush( e->ForeColor );
      else
      if ( foreColorBrush->Color != e->ForeColor )
      {
         // The control's ForeColor has changed, so dispose of the cached
 brush and
         // create a new one.
         delete foreColorBrush;
         foreColorBrush = gcnew SolidBrush( e->ForeColor );
      }

      // Select the appropriate brush depending on if the item is selected.
      // Since State can be a combinateion (bit-flag) of enum values,
 you can't use
      // "==" to compare them.
      if ( (e->State & DrawItemState::Selected) == DrawItemState::Selected
 )
            brush = SystemBrushes::HighlightText;
      else
            brush = foreColorBrush;

      // Perform the painting.
      System::Drawing::Font^ font = (dynamic_cast<ListBoxFontItem^>(listBox1->Items[
 e->Index ]))->Font;
      e->DrawBackground();
      e->Graphics->DrawString( font->Name, font, brush, e->Bounds );
      e->DrawFocusRectangle();
   }

public:

   /// <summary>
   ///  A wrapper class for use with storing Fonts in a ListBox.  Since
 ListBox uses the
   ///  ToString() of its items for the text it displays, this class
 is needed to return
   ///  the name of the font, rather than its ToString() value.
   /// </summary>
   ref class ListBoxFontItem
   {
   public:
      System::Drawing::Font^ Font;
      ListBoxFontItem( System::Drawing::Font^ f )
      {
         Font = f;
      }

      virtual String^ ToString() override
      {
         return Font->Name;
      }
   };
};

[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.ListBox listBox1;
    private System.ComponentModel.Container components = null;

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }

            if (foreColorBrush != null) {
                foreColorBrush.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose
      
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.set_DrawMode(
            System.Windows.Forms.DrawMode.OwnerDrawVariable);
        this.listBox1.set_Location(new System.Drawing.Point(16,
 48));
        this.listBox1.set_Name("listBox1");
        this.listBox1.set_SelectionMode(
            System.Windows.Forms.SelectionMode.MultiExtended);
        this.listBox1.set_Size(new System.Drawing.Size(256,
 134));
        this.listBox1.set_TabIndex(0);
        this.listBox1.add_MeasureItem(
            new System.Windows.Forms.MeasureItemEventHandler(
            this.listBox1_MeasureItem));
        this.listBox1.add_DrawItem(
            new System.Windows.Forms.DrawItemEventHandler(
            this.listBox1_DrawItem));
        // 
        // Form1
        // 
        this.set_ClientSize(new System.Drawing.Size(292,
 273));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[]
 
            { this.listBox1 });
        this.set_Name("Form1");
        this.set_Text("Form1");
        this.ResumeLayout(false);
    } //InitializeComponent
    #endregion
      
    /** @attribute STAThread()
     */
    public static void main(String[]
 args)
    {
        Application.Run(new Form1());
    } //main

    private void listBox1_MeasureItem(Object
 sender, 
        System.Windows.Forms.MeasureItemEventArgs e)
    {
        Font font = ((ListBoxFontItem)listBox1.get_Items().
             get_Item(e.get_Index())).font;
        SizeF stringSize = 
            e.get_Graphics().MeasureString(font.get_Name(), font);

        // Set the height and width of the item
        e.set_ItemHeight((int)stringSize.get_Height());
        e.set_ItemWidth((int)stringSize.get_Width());
    } //listBox1_MeasureItem

    // For efficiency, cache the brush to use for drawing.
    private SolidBrush foreColorBrush;

    private void listBox1_DrawItem(Object sender,
 
        System.Windows.Forms.DrawItemEventArgs e)
    {
        Brush brush;

        // Create the brush using the ForeColor specified by the 
        // DrawItemEventArgs
        if (foreColorBrush == null) {
            foreColorBrush = new SolidBrush(e.get_ForeColor());
        }
        else {
            if (!foreColorBrush.get_Color().Equals(e.get_ForeColor()))
 {
                // The control's ForeColor has changed, 
                // so dispose of the cached brush and
                // create a new one.
                foreColorBrush.Dispose();
                foreColorBrush = new SolidBrush(e.get_ForeColor());
            }
        }
        // Select the appropriate brush depending on if the item is
 selected.
        // Since State can be a combinateion (bit-flag) of enum values
,
        // you can't use "==" to compare them.
        if ((e.get_State() & DrawItemState.Selected).
            Equals(DrawItemState.Selected)) {
            brush = SystemBrushes.get_HighlightText();
        }
        else {
            brush = foreColorBrush;
        }

        // Perform the painting.
        Font font = ((ListBoxFontItem)listBox1.get_Items().
            get_Item(e.get_Index())).font;
        e.DrawBackground();
        e.get_Graphics().DrawString(font.get_Name(), font, brush, 
            new PointF((float)e.get_Bounds().get_X(),
 
            (float)e.get_Bounds().get_Y()));
        e.DrawFocusRectangle();
    } //listBox1_DrawItem

    /// <summary>
    ///  A wrapper class for use with storing Fonts in a ListBox.  
    ///  Since ListBox uses the ToString() of its items 
    ///  for the text it displays, this class is needed to return
    ///  the name of the font, rather than its ToString() value.
    /// </summary>
    public class ListBoxFontItem
    {
        public Font font;

        public ListBoxFontItem(Font f)
        {
            set_Font(f);
        } //ListBoxFontItem

        public String ToString()
        {
            return get_Font().get_Name();
        } //ToString
    } //ListBoxFontItem
} //Form1
継承階層継承階層
System.Object
   System.EventArgs
    System.Windows.Forms.MeasureItemEventArgs
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MeasureItemEventArgs メンバ
System.Windows.Forms 名前空間
ListBox クラス
ComboBox クラス
CheckedListBox クラス
MenuItem
MeasureItemEventHandler

MeasureItemEventArgs コンストラクタ

MeasureItemEventArgs クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
MeasureItemEventArgs (Graphics, Int32) MeasureItemEventArgs クラス新しインスタンス初期化します。
MeasureItemEventArgs (Graphics, Int32, Int32) 項目の高さに対すパラメータ指定してMeasureItemEventArgs クラス新しインスタンス初期化します。
参照参照

関連項目

MeasureItemEventArgs クラス
MeasureItemEventArgs メンバ
System.Windows.Forms 名前空間

MeasureItemEventArgs コンストラクタ (Graphics, Int32)

MeasureItemEventArgs クラス新しインスタンス初期化します。

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MeasureItemEventArgs クラス
MeasureItemEventArgs メンバ
System.Windows.Forms 名前空間

MeasureItemEventArgs コンストラクタ (Graphics, Int32, Int32)

項目の高さに対すパラメータ指定して、MeasureItemEventArgs クラス新しインスタンス初期化します。

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

Public Sub New ( _
    graphics As Graphics, _
    index As Integer, _
    itemHeight As Integer _
)
Dim graphics As Graphics
Dim index As Integer
Dim itemHeight As Integer

Dim instance As New MeasureItemEventArgs(graphics,
 index, itemHeight)
public MeasureItemEventArgs (
    Graphics graphics,
    int index,
    int itemHeight
)
public:
MeasureItemEventArgs (
    Graphics^ graphics, 
    int index, 
    int itemHeight
)
public MeasureItemEventArgs (
    Graphics graphics, 
    int index, 
    int itemHeight
)
public function MeasureItemEventArgs (
    graphics : Graphics, 
    index : int, 
    itemHeight : int
)

パラメータ

graphics

書き込まれGraphics オブジェクト

index

高さまたは幅が必要な項目のインデックス

itemHeight

Graphics オブジェクトに対して相対的に計測する、項目の高さ。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MeasureItemEventArgs クラス
MeasureItemEventArgs メンバ
System.Windows.Forms 名前空間

MeasureItemEventArgs プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Graphics 計測対象となる Graphics オブジェクト取得します
パブリック プロパティ Index 高さと幅が必要な項目のインデックス取得または設定します
パブリック プロパティ ItemHeight Index指定した項目の高さを取得または設定します
パブリック プロパティ ItemWidth Index指定した項目の幅を取得または設定します
参照参照

関連項目

MeasureItemEventArgs クラス
System.Windows.Forms 名前空間
ListBox クラス
ComboBox クラス
CheckedListBox クラス
MenuItem
MeasureItemEventHandler

MeasureItemEventArgs メソッド


MeasureItemEventArgs メンバ

ListBoxComboBox、CheckedListBox、MenuItem の各コントロールMeasureItem イベントデータ提供します

MeasureItemEventArgs データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド MeasureItemEventArgs オーバーロードされます。 MeasureItemEventArgs クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Graphics 計測対象となる Graphics オブジェクト取得します
パブリック プロパティ Index 高さと幅が必要な項目のインデックス取得または設定します
パブリック プロパティ ItemHeight Index指定した項目の高さを取得または設定します
パブリック プロパティ ItemWidth Index指定した項目の幅を取得または設定します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

MeasureItemEventArgs クラス
System.Windows.Forms 名前空間
ListBox クラス
ComboBox クラス
CheckedListBox クラス
MenuItem
MeasureItemEventHandler



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

辞書ショートカット

すべての辞書の索引

「MeasureItemEventArgs」の関連用語

MeasureItemEventArgsのお隣キーワード
検索ランキング

   

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



MeasureItemEventArgsのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS