Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DrawToolTipEventHandler Delegate
Collapse the table of content
Expand the table of content

DrawToolTipEventHandler Delegate

Represents the method that will handle the Draw event of a ToolTip.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public delegate void DrawToolTipEventHandler(
	Object^ sender, 
	DrawToolTipEventArgs^ e


Type: System::Object

The source of the event.

Type: System.Windows.Forms::DrawToolTipEventArgs

A DrawToolTipEventArgs that contains the event data.

The ToolTip::Draw event is raised by the ToolTip class when the ToolTip is drawn and the ToolTip::OwnerDraw property value is true. The DrawToolTipEventArgs class contains all the information needed to paint the ToolTip, including the ToolTip text, the Rectangle, and the Graphics object on which the drawing should be done. To customize the look of the ToolTip, use the Rectangle to determine the bounds of the ToolTip, and the Graphics object to perform your customized drawing. You can increase the bounds of the ToolTip before it is shown by handling the Popup event.

When you create a DrawToolTipEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Handling and Raising Events.

The following code example demonstrates how to custom draw the ToolTip. The example creates a ToolTip and associates it to three Button controls located on the Form. The example sets the OwnerDraw property to true and handles the Draw event. In the Draw event handler, the ToolTip is custom drawn differently depending upon what button the ToolTip is being displayed for as indicated by the DrawToolTipEventArgs::AssociatedControl property.

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::VisualStyles;

// Form for the ToolTip example. 
public ref class ToolTipExampleForm: public System::Windows::Forms::Form
   System::Windows::Forms::ToolTip^ toolTip1;
   System::Windows::Forms::Button^ button1;
   System::Windows::Forms::Button^ button2;
   System::Windows::Forms::Button^ button3;

      // Create the ToolTip and set initial values. 
      this->toolTip1 = gcnew System::Windows::Forms::ToolTip;
      this->toolTip1->AutoPopDelay = 5000;
      this->toolTip1->InitialDelay = 500;
      this->toolTip1->OwnerDraw = true;
      this->toolTip1->ReshowDelay = 10;
      this->toolTip1->Draw += gcnew DrawToolTipEventHandler( this, &ToolTipExampleForm::toolTip1_Draw );

      // Create button1 and set initial values. 
      this->button1 = gcnew System::Windows::Forms::Button;
      this->button1->Location = System::Drawing::Point( 8, 8 );
      this->button1->Text = "Button 1";
      this->toolTip1->SetToolTip( this->button1, "Button1 tip text" );

      // Create button2 and set initial values. 
      this->button2 = gcnew System::Windows::Forms::Button;
      this->button2->Location = System::Drawing::Point( 8, 32 );
      this->button2->Text = "Button 2";
      this->toolTip1->SetToolTip( this->button2, "Button2 tip text" );

      // Create button3 and set initial values. 
      this->button3 = gcnew System::Windows::Forms::Button;
      this->button3->Location = System::Drawing::Point( 8, 56 );
      this->button3->Text = "Button 3";
      this->toolTip1->SetToolTip( this->button3, "Button3 tip text" );

      // Set up the Form. 
      array<Control^>^temp0 = {this->button1,this->button2,this->button3};
      this->Controls->AddRange( temp0 );
      this->Text = "owner drawn ToolTip example";


      if ( toolTip1 != nullptr )
         delete toolTip1;

   // Handles drawing the ToolTip. 
   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.

         // 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. 

      // 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.

         // Draw the custom text. 
         // The using block will dispose the StringFormat automatically.
         StringFormat^ sf = gcnew StringFormat;
            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 );
               e->Graphics->DrawString( e->ToolTipText, f, SystemBrushes::ActiveCaptionText, e->Bounds, sf );
               if ( f )
                  delete safe_cast<IDisposable^>(f);

            if ( sf )
               delete safe_cast<IDisposable^>(sf);
      // Draw the ToolTip using default values if the ToolTip is for button3. 
      else if ( e->AssociatedControl == button3 )

// The main entry point for the application.

int main()
   Application::Run( gcnew ToolTipExampleForm );

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft