Expandir Minimizar
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original
Este tópico ainda não foi avaliado como - Avalie este tópico

DrawItemEventHandler Representante

Represents the method that will handle the DrawItem event of a ComboBox, ListBox, MenuItem, or TabControl control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms. dll)
public delegate void DrawItemEventHandler(
	Object sender,
	DrawItemEventArgs e
)

Parâmetros

sender
Tipo: System.Object
A origem do evento.
e
Tipo: System.Windows.Forms.DrawItemEventArgs
A DrawItemEventArgs that contains the event data.

When you create a DrawItemEventArgs delegate, you identify the method that will handle the event.Para associar o evento com o manipulador de eventos, adicione uma instância do delegate ao evento.O manipulador de evento é chamado sempre que o evento ocorre, a menos que você remova o delegate.For more information about event handler delegates, see Eventos e representantes.

O exemplo de código a seguir demonstra um menu com um item de menu desenhado pelo proprietário.This example uses the AddHandler statement and the AddressOf operator to designate a delegate to handle the MenuItem.DrawItem event.To the local exemplo executar Colar it in a forma that Imports the Namespaces Sistema, Sistema.Windows.Forms, and Sistema.Desenho.Verifique Tudos os eventos são associado com seu event-Manipulação méTudos.

	// Declare the MainMenu control.
	internal System.Windows.Forms.MainMenu MainMenu1;

	// Declare MenuItem2 as With-Events because it will be user drawn.
	internal System.Windows.Forms.MenuItem MenuItem2;


	private void InitializeMenu()
	{

		// Create MenuItem1, which will be drawn by the operating system.
		MenuItem MenuItem1 = new MenuItem("Regular Menu Item");

		// Create MenuItem2.
		MenuItem2 = new MenuItem("Custom Menu Item");

		// Set OwnerDraw property to true. This requires handling the
		// DrawItem event for this menu item.
		MenuItem2.OwnerDraw = true;

		//Add the event-handler delegate to handle the DrawItem event.
        MenuItem2.DrawItem += new DrawItemEventHandler(DrawCustomMenuItem);
		
      // Add the items to the menu.
		MainMenu1 = new MainMenu(new MenuItem[]{MenuItem1, MenuItem2});																													  

		// Add the menu to the form.
		this.Menu = this.MainMenu1;
	}

	// Draw the custom menu item.
	private void DrawCustomMenuItem(object sender, 
		DrawItemEventArgs e)
	{

		// Cast the sender to MenuItem so you can access text property.
		MenuItem customItem = (MenuItem) sender;

		// Create a Brush and a Font to draw the MenuItem.
		System.Drawing.Brush aBrush = System.Drawing.Brushes.DarkMagenta;
		Font aFont = new Font("Garamond", 10, 
			FontStyle.Italic, GraphicsUnit.Point);

		// Get the size of the text to use later to draw an ellipse
		// around the item.
		SizeF stringSize = e.Graphics.MeasureString(
			customItem.Text, aFont);

		// Draw the item and then draw the ellipse.
		e.Graphics.DrawString(customItem.Text, aFont, 
			aBrush, e.Bounds.X, e.Bounds.Y);
		e.Graphics.DrawEllipse(new Pen(System.Drawing.Color.Black, 2),
			new Rectangle(e.Bounds.X, e.Bounds.Y, 
			(System.Int32)stringSize.Width,
			(System.Int32)stringSize.Height));
	}


// Declare the MainMenu control.
System.Windows.Forms.MainMenu mainMenu1;

// Declare MenuItem2 as With-Events because it will be user drawn.
System.Windows.Forms.MenuItem menuItem2;

private void InitializeMenu()
{
    // Create MenuItem1, which will be drawn by the operating system.
    MenuItem menuItem1 = new MenuItem("Regular Menu Item");
    // Create MenuItem2.
    menuItem2 = new MenuItem("Custom Menu Item");
    // Set OwnerDraw property to true. This requires handling the
    // DrawItem event for this menu item.
    menuItem2.set_OwnerDraw(true);
    //Add the event-handler delegate to handle the DrawItem event.
    menuItem2.add_DrawItem(new DrawItemEventHandler(DrawCustomMenuItem));
    // Add the items to the menu.
    mainMenu1 = new MainMenu(new MenuItem[] { menuItem1, menuItem2 });
    // Add the menu to the form.
    this.set_Menu(this.mainMenu1);
} //InitializeMenu

// Draw the custom menu item.
private void DrawCustomMenuItem(Object sender, DrawItemEventArgs e)
{
    // Cast the sender to MenuItem so you can access text property.
    MenuItem customItem = (MenuItem)sender;
    // Create a Brush and a Font to draw the MenuItem.
    System.Drawing.Brush aBrush = System.Drawing.Brushes.get_DarkMagenta();
    Font aFont =
        new Font("Garamond", 10, FontStyle.Italic, GraphicsUnit.Point);
    // Get the size of the text to use later to draw an ellipse
    // around the item.
    SizeF stringSize =
        e.get_Graphics().MeasureString(customItem.get_Text(), aFont);
    // Draw the item and then draw the ellipse.
    e.get_Graphics().
        DrawString(customItem.get_Text(), aFont, aBrush,
        e.get_Bounds().get_X(), e.get_Bounds().get_Y());
    e.get_Graphics().
        DrawEllipse(new Pen(System.Drawing.Color.get_Black(), 2),
        new Rectangle(e.get_Bounds().get_X(), e.get_Bounds().get_Y(),
        System.Convert.ToInt32((System.Int32)stringSize.get_Width()),
        System.Convert.ToInt32((System.Int32)stringSize.get_Height())));
} //DrawCustomMenuItem


Isso foi útil para você?
(1500 caracteres restantes)

Contribuições da comunidade

ADICIONAR
© 2013 Microsoft. Todos os direitos reservados.