Esta documentación está archivada y no tiene mantenimiento.

DrawToolTipEventHandler (Delegado)

Representa el método que controlará el evento Draw de un objeto ToolTip.

Espacio de nombres: System.Windows.Forms
Ensamblado: System.Windows.Forms (en system.windows.forms.dll)

public delegate void DrawToolTipEventHandler (
	Object sender,
	DrawToolTipEventArgs e
)
/** @delegate */
public delegate void DrawToolTipEventHandler (
	Object sender, 
	DrawToolTipEventArgs e
)
No aplicable.

Parámetros

sender

Origen del evento.

e

DrawToolTipEventArgs que contiene los datos del evento.

La clase ToolTip provoca el evento ToolTip.Draw cuando se dibuja ToolTip y el valor de la propiedad ToolTip.OwnerDraw es true. La clase DrawToolTipEventArgs contiene toda la información necesaria para pintar el objeto ToolTip, incluido el texto de la información sobre herramientas, el objeto Rectangle y el objeto Graphics en el que debe realizarse el dibujo. Para personalizar el aspecto de la información sobre herramientas, utilice el objeto Rectangle para determinar los límites de la misma y el objeto Graphics para realizar el dibujo personalizado. Puede aumentar los límites del objeto ToolTip antes de que se muestre controlando el evento Popup.

Cuando se crea un delegado DrawToolTipEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador de eventos, agregue una instancia del delegado al evento. A menos que se quite el delegado, el controlador de eventos recibirá una llamada siempre que se produzca el evento. Para obtener más información sobre los delegados del controlador de eventos, vea Eventos y delegados.

En el ejemplo de código siguiente se muestra cómo se realiza el dibujo personalizado de ToolTip. En el ejemplo se crea un objeto ToolTip y se asocia a tres controles Button situados en Form. En el ejemplo se establece la propiedad OwnerDraw en true y se controla el evento Draw. En el controlador de eventos Draw, ToolTip se dibuja de forma personalizada dependiendo del botón para el que se va a mostrar ToolTip, según indica la propiedad DrawToolTipEventArgs.AssociatedControl.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ToolTipExample
{
    // Form for the ToolTip example.
    public class ToolTipExampleForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ToolTip toolTip1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;

        public ToolTipExampleForm()
        {
            // Create the ToolTip and set initial values.
            this.toolTip1 = new System.Windows.Forms.ToolTip();
            this.toolTip1.AutoPopDelay = 5000;
            this.toolTip1.InitialDelay = 500;
            this.toolTip1.OwnerDraw = true;
            this.toolTip1.ReshowDelay = 10;
            this.toolTip1.Draw += new DrawToolTipEventHandler(this.toolTip1_Draw);
            this.toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);

            // Create button1 and set initial values.
            this.button1 = new System.Windows.Forms.Button();
            this.button1.Location = new 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 = new System.Windows.Forms.Button();
            this.button2.Location = new 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 = new System.Windows.Forms.Button();
            this.button3.Location = new System.Drawing.Point(8, 56);
            this.button3.Text = "Button 3";
            this.toolTip1.SetToolTip(this.button3, "Button3 tip text");

            // Set up the Form.
            this.Controls.AddRange(new Control[] {
                this.button1, this.button2, this.button3
            });
            this.Text = "owner drawn ToolTip example";
        }

        // Clean up any resources being used.        
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                toolTip1.Dispose();
            }

            base.Dispose(disposing);
        }

        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
            Application.Run(new ToolTipExampleForm());
        }

        // Determines the correct size for the button2 ToolTip.
        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            if (e.AssociatedControl == button2)
            {
                using (Font f = new Font("Tahoma", 9))
                {
                    e.ToolTipSize = TextRenderer.MeasureText(
                        toolTip1.GetToolTip(e.AssociatedControl), f);
                }
            }
        }

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

package ToolTipExample;

import System.*;
import System.Drawing.*;
import System.Drawing.GDI.*;
import System.Windows.Forms.*;
import System.Windows.Forms.VisualStyles.*;

// Form for the ToolTip example.
public class ToolTipExampleForm extends System.Windows.Forms.Form
{
    private System.Windows.Forms.ToolTip toolTip1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;

    public ToolTipExampleForm()
    {
        // Create the ToolTip and set initial values.
        this.toolTip1 = new System.Windows.Forms.ToolTip();
        this.toolTip1.set_AutoPopDelay(5000);
        this.toolTip1.set_InitialDelay(500);
        this.toolTip1.set_OwnerDraw(true);
        this.toolTip1.set_ReshowDelay(10);
        this.toolTip1.add_Draw(new DrawToolTipEventHandler(this.toolTip1_Draw));
        // Create button1 and set initial values.
        this.button1 = new System.Windows.Forms.Button();
        this.button1.set_Location(new System.Drawing.Point(8, 8));
        this.button1.set_Text("Button 1");
        this.toolTip1.SetToolTip(this.button1, "Button1 tip text");
        // Create button2 and set initial values.
        this.button2 = new System.Windows.Forms.Button();
        this.button2.set_Location(new System.Drawing.Point(8, 32));
        this.button2.set_Text("Button 2");
        this.toolTip1.SetToolTip(this.button2, "Button2 tip text");
        // Create button3 and set initial values.
        this.button3 = new System.Windows.Forms.Button();
        this.button3.set_Location(new System.Drawing.Point(8, 56));
        this.button3.set_Text("Button 3");
        this.toolTip1.SetToolTip(this.button3, "Button3 tip text");
        // Set up the Form.
        this.get_Controls().AddRange(new Control[]
            { this.button1, this.button2, this.button3 });
        this.set_Text("owner drawn ToolTip example");
    } //ToolTipExampleForm

    // Clean up any resources being used.        
    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            toolTip1.Dispose();
        }
        super.Dispose(disposing);
    } //Dispose

    // The main entry point for the application.
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new ToolTipExampleForm());
    } //main

    // 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
} //ToolTipExampleForm

Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0
Mostrar: