DrawToolTipEventArgs.ToolTipText 属性

定义

获取正在绘制的 ToolTip 的文本。Gets the text for the ToolTip that is being drawn.

public:
 property System::String ^ ToolTipText { System::String ^ get(); };
public string ToolTipText { get; }
member this.ToolTipText : string
Public ReadOnly Property ToolTipText As String

属性值

String

发生 ToolTip 事件时与 Draw 关联的文本。The text that is associated with the ToolTip when the Draw event occurs.

示例

下面的代码示例演示如何自定义绘制 ToolTipThe following code example demonstrates how to custom draw the ToolTip. 该示例创建一个 ToolTip ,并将其与 Button 位于上的三个控件关联 FormThe example creates a ToolTip and associates it to three Button controls located on the Form. 该示例将 OwnerDraw 属性设置为 true,并处理 Draw 事件。The example sets the OwnerDraw property to true and handles the Draw event. Draw 事件处理程序中, ToolTip 自定义绘制是不同的,具体取决于所显示的按钮, ToolTip 如属性所示 DrawToolTipEventArgs.AssociatedControlIn the Draw event handler, the ToolTip is custom drawn differently depending on what button the ToolTip is being displayed for as indicated by the DrawToolTipEventArgs.AssociatedControl property.

下面的代码摘录演示了如何使用 DrawBorder 方法并使用 BoundsToolTipTextGraphics 属性。The code excerpt below demonstrates using the DrawBorder method and using the Bounds, ToolTipText, and Graphics properties. DrawToolTipEventArgs有关完整的代码示例,请参阅类概述。See the DrawToolTipEventArgs class overview for the complete code example.

// 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 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);
        }
    }
}
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

注解

通常,您可以使用 ToolTipText 属性来确定您在自定义绘图工具提示时的工具提示文本。Typically you would use the ToolTipText property to determine what the ToolTip text is when you are custom drawing your ToolTip. 您可以使用 Graphics.DrawString 方法自定义工具提示文本的绘制。You can use the Graphics.DrawString method to customize the drawing of the ToolTip text. 如果要使用系统指定的样式绘制 ToolTip 文本,请使用 DrawText 方法。If you want to have the ToolTip text drawn using the system specified style, use the DrawText method. 文本值来自传递到类的方法的值 SetToolTip ToolTipThe text value comes from the value passed to the SetToolTip method of the ToolTip class.

适用于

另请参阅