クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
System.Windows.Forms 名前空間
DrawToolTipEventArgs クラス
 AssociatedControl プロパティ
すべて縮小/すべて展開 すべて縮小
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
DrawToolTipEventArgs..::.AssociatedControl プロパティ

更新 : 2007 年 11 月

ToolTip を描画する対象のコントロールを取得します。

名前空間 :  System.Windows.Forms
アセンブリ :  System.Windows.Forms (System.Windows.Forms.dll 内)

Visual Basic (宣言)
Public ReadOnly Property AssociatedControl As Control
Visual Basic (使用法)
Dim instance As DrawToolTipEventArgs
Dim value As Control

value = instance.AssociatedControl
C#
public Control AssociatedControl { get; }
Visual C++
public:
property Control^ AssociatedControl {
    Control^ get ();
}
J#
/** @property */
public Control get_AssociatedControl()
JScript
public function get AssociatedControl () : Control

プロパティ値

型 : System.Windows.Forms..::.Control

Draw イベントが発生したときに ToolTip に関連付けられている Control。ツールヒントがコントロールに関連付けられていない場合、戻り値は nullNothingnullptrnull 参照 (Visual Basic では Nothing) です。

ToolTip は、ToolTip..::.SetToolTip メソッドを使用して複数のコントロールに関連付けることができるため、AssociatedControl プロパティを使用することにより、Draw イベントが関連付けられているコントロールを確認できます。これは、関連付けられたコントロールに応じてツールヒントのカスタマイズ方法を変える場合に便利です。

ToolTip をカスタム描画する方法を次のコード例に示します。この例では、ToolTip を作成し、Form 上にある 3 つの Button コントロールに関連付けています。また、OwnerDraw プロパティを true に設定し、Draw イベントを処理しています。Draw イベント ハンドラでは、どのボタンで ToolTip が表示されるかをDrawToolTipEventArgs..::.AssociatedControl プロパティで指定し、それによって ToolTip がカスタムで描画されます。

AssociatedControl プロパティの使用方法を示すコード例の抜粋を次に示します。コード例全体については、DrawToolTipEventArgs クラスの概要を参照してください。

Visual Basic
' Handles drawing the ToolTip.
Private Sub toolTip1_Draw(ByVal sender As System.Object, _
    ByVal e As DrawToolTipEventArgs) Handles toolTip1.Draw
    ' Draw the ToolTip differently depending on which 
    ' control this ToolTip is for.

    ' Draw a custom 3D border if the ToolTip is for button1.
    If (e.AssociatedControl Is button1) Then
        ' Draw the standard background.
        e.DrawBackground()

        ' Draw the custom border to appear 3-dimensional.
        e.Graphics.DrawLines( _
            SystemPens.ControlLightLight, New Point() { _
            New Point(0, e.Bounds.Height - 1), _
            New Point(0, 0), _
            New Point(e.Bounds.Width - 1, 0)})
        e.Graphics.DrawLines( _
            SystemPens.ControlDarkDark, New Point() { _
            New Point(0, e.Bounds.Height - 1), _
            New Point(e.Bounds.Width - 1, e.Bounds.Height - 1), _
            New Point(e.Bounds.Width - 1, 0)})

        ' Specify custom text formatting flags.
        Dim sf As TextFormatFlags = TextFormatFlags.VerticalCenter Or _
                             TextFormatFlags.HorizontalCenter Or _
                             TextFormatFlags.NoFullWidthCharacterBreak

        ' Draw standard text with customized formatting options.
        e.DrawText(sf)
    ElseIf (e.AssociatedControl Is button2) Then
        ' Draw a custom background and text if the ToolTip is for button2.

        ' Draw the custom background.
        e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, e.Bounds)

        ' Draw the standard border.
        e.DrawBorder()

        ' Draw the custom text.
        Dim sf As StringFormat = New StringFormat
        Try
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None
            sf.FormatFlags = StringFormatFlags.NoWrap

            Dim f As Font = New Font("Tahoma", 9)
            Try
                e.Graphics.DrawString(e.ToolTipText, f, _
                    SystemBrushes.ActiveCaptionText, _
                    RectangleF.op_Implicit(e.Bounds), sf)
            Finally
                f.Dispose()
            End Try
        Finally
            sf.Dispose()
        End Try
    ElseIf (e.AssociatedControl Is button3) Then
        ' Draw the ToolTip using default values if the ToolTip is for button3.
        e.DrawBackground()
        e.DrawBorder()
        e.DrawText()
    End If
End Sub
C#
// Handles drawing the ToolTip.
private void toolTip1_Draw(System.Object sender, 
    System.Windows.Forms.DrawToolTipEventArgs e)
{
    // Draw the ToolTip differently depending on which 
    // control this ToolTip is for.
    // Draw a custom 3D border if the ToolTip is for button1.
    if (e.AssociatedControl == button1)
    {
        // Draw the standard background.
        e.DrawBackground();

        // Draw the custom border to appear 3-dimensional.
        e.Graphics.DrawLines(SystemPens.ControlLightLight, new Point[] {
            new Point (0, e.Bounds.Height - 1), 
            new Point (0, 0), 
            new Point (e.Bounds.Width - 1, 0)
        });
        e.Graphics.DrawLines(SystemPens.ControlDarkDark, new Point[] {
            new Point (0, e.Bounds.Height - 1), 
            new Point (e.Bounds.Width - 1, e.Bounds.Height - 1), 
            new Point (e.Bounds.Width - 1, 0)
        });

        // Specify custom text formatting flags.
        TextFormatFlags sf = TextFormatFlags.VerticalCenter |
                             TextFormatFlags.HorizontalCenter |
                             TextFormatFlags.NoFullWidthCharacterBreak;

        // Draw the standard text with customized formatting options.
        e.DrawText(sf);
    }
    // Draw a custom background and text if the ToolTip is for button2.
    else if (e.AssociatedControl == button2)
    {
        // Draw the custom background.
        e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, e.Bounds);

        // Draw the standard border.
        e.DrawBorder();

        // Draw the custom text.
        // The using block will dispose the StringFormat automatically.
        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
            sf.FormatFlags = StringFormatFlags.NoWrap;
            using (Font f = new Font("Tahoma", 9))
            {
                e.Graphics.DrawString(e.ToolTipText, f, 
                    SystemBrushes.ActiveCaptionText, e.Bounds, sf);
            }
        }
    }
    // Draw the ToolTip using default values if the ToolTip is for button3.
    else if (e.AssociatedControl == button3)
    {
        e.DrawBackground();
        e.DrawBorder();
        e.DrawText();
    }
}
Visual C++
   // Handles drawing the ToolTip.
private:
   void toolTip1_Draw( System::Object^ /*sender*/, System::Windows::Forms::DrawToolTipEventArgs^ e )
   {
      // Draw the ToolTip differently depending on which 
      // control this ToolTip is for.
      // Draw a custom 3D border if the ToolTip is for button1.
      if ( e->AssociatedControl == button1 )
      {
         // Draw the standard background.
         e->DrawBackground();

         // Draw the custom border to appear 3-dimensional.
         array<Point>^ temp1 = {Point(0,e->Bounds.Height - 1),Point(0,0),Point(e->Bounds.Width - 1,0)};
         e->Graphics->DrawLines( SystemPens::ControlLightLight, temp1 );
         array<Point>^ temp2 = {Point(0,e->Bounds.Height - 1),Point(e->Bounds.Width - 1,e->Bounds.Height - 1),Point(e->Bounds.Width - 1,0)};
         e->Graphics->DrawLines( SystemPens::ControlDarkDark, temp2 );

         // Specify custom text formatting flags.
         TextFormatFlags sf = static_cast<TextFormatFlags>(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter | TextFormatFlags::NoFullWidthCharacterBreak);

         // Draw the standard text with customized formatting options.
         e->DrawText( sf );
      }
      // Draw a custom background and text if the ToolTip is for button2.
      else

      // Draw a custom background and text if the ToolTip is for button2.
      if ( e->AssociatedControl == button2 )
      {
         // Draw the custom background.
         e->Graphics->FillRectangle( SystemBrushes::ActiveCaption, e->Bounds );

         // Draw the standard border.
         e->DrawBorder();

         // Draw the custom text.
         // The using block will dispose the StringFormat automatically.
         StringFormat^ sf = gcnew StringFormat;
         try
         {
            sf->Alignment = StringAlignment::Center;
            sf->LineAlignment = StringAlignment::Center;
            sf->HotkeyPrefix = System::Drawing::Text::HotkeyPrefix::None;
            sf->FormatFlags = StringFormatFlags::NoWrap;
            System::Drawing::Font^ f = gcnew System::Drawing::Font( "Tahoma",9 );
            try
            {
               e->Graphics->DrawString( e->ToolTipText, f, SystemBrushes::ActiveCaptionText, e->Bounds, sf );
            }
            finally
            {
               if ( f )
                  delete safe_cast<IDisposable^>(f);
            }

         }
         finally
         {
            if ( sf )
               delete safe_cast<IDisposable^>(sf);
         }
      }
      // Draw the ToolTip using default values if the ToolTip is for button3.
      else if ( e->AssociatedControl == button3 )
      {
         e->DrawBackground();
         e->DrawBorder();
         e->DrawText();
      }
   }
J#
// Handles drawing the ToolTip.
private void toolTip1_Draw(Object sender,
    System.Windows.Forms.DrawToolTipEventArgs e)
{
    // Draw the ToolTip differently depending on which 
    // control this ToolTip is for.
    // Draw a custom 3D border if the ToolTip is for button1.
    if (e.get_AssociatedControl().Equals(button1)) {
        // Draw the standard background.
        e.DrawBackground();
        // Draw the custom border to appear 3-dimensional.
        e.get_Graphics().DrawLines(SystemPens.get_ControlLightLight(),
            new Point[] { new Point(0, e.get_Bounds().get_Height() - 1),
            new Point(0, 0), new Point(e.get_Bounds().get_Width() - 1, 0)});
        e.get_Graphics().DrawLines(SystemPens.get_ControlDarkDark(),
            new Point[] { new Point(0, e.get_Bounds().get_Height() - 1),
            new Point(e.get_Bounds().get_Width() - 1,
            e.get_Bounds().get_Height() - 1),
            new Point(e.get_Bounds().get_Width() - 1, 0) });
        // Specify custom text formatting flags.
        TextFormatFlags sf = TextFormatFlags.VerticalCenter
            | TextFormatFlags.HorizontalCenter
            | TextFormatFlags.NoFullWidthCharacterBreak;
        // Draw the standard text with customized formatting options.
        e.DrawText(sf);
    }
    // Draw a custom background and text if the ToolTip is for button2.
    else {
        if (e.get_AssociatedControl().Equals(button2)) {
            // Draw the custom background.
            e.get_Graphics().FillRectangle(SystemBrushes.get_ActiveCaption(),
                e.get_Bounds());
            // Draw the standard border.
            e.DrawBorder();

            // Draw the custom text.
            // The using block will dispose the StringFormat automatically.
            StringFormat sf = new StringFormat();
            try {
                sf.set_Alignment(StringAlignment.Center);
                sf.set_LineAlignment(StringAlignment.Center);
                sf.set_HotkeyPrefix(System.Drawing.Text.HotkeyPrefix.None);
                sf.set_FormatFlags(StringFormatFlags.NoWrap);                    
                Font f = new Font("Tahoma", 9);
                try {
                    e.get_Graphics().DrawString(e.get_ToolTipText(), f,
                        SystemBrushes.get_ActiveCaptionText(),
                        RectangleF.op_Implicit(e.get_Bounds()), sf);
                }
                finally {
                    f.Dispose();
                }                    
            }
            finally {
                sf.Dispose();
            }                
        }
        // Draw the ToolTip using default values if the ToolTip is
        // for button3.
        else {
            if (e.get_AssociatedControl().Equals(button3)) {
                e.DrawBackground();
                e.DrawBorder();
                e.DrawText();
            }
        }
    }        
} //toolTip1_Draw

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

.NET Framework

サポート対象 : 3.5、3.0、2.0
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker