Comment dessiner son propre menu

Contenu du snippet

Ce code vous présente la manière dont vous pouvez manipuiler et modifier les menus : comme la Fonte, la couleur et le fond ...

Source / Exemple :


1. 
//Lastuce ici se base sur la propriété OwnerDraw de l'item

//miItem1.OwnerDraw = true;

2. capturer les deux évenements :  DrawItem  et MeasureItem
private  int iwIcon = 16;

//Fonction pour dessiner les PopUp Items
private void DrawMenuPopLabel(string caption, DrawItemEventArgs ev)
{
      Pen mpen = new Pen(this.BackColor, 1);
      Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular, 
      GraphicsUnit.Pixel);
      SizeF mySizeF = ev.Graphics.MeasureString(caption, myFont);

      StringFormat format = StringFormat.GenericDefault;
       format.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

       this.iwIcon = 10;

       int wit = ev.Bounds.Width - 4;
       int hgt = Convert.ToInt32(mySizeF.Height);
			
       switch(ev.State)
      {
             case (DrawItemState.HotLight|DrawItemState.NoAccelerator):
             case DrawItemState.HotLight:
             case DrawItemState.Selected:
             case (DrawItemState.Selected|DrawItemState.NoAccelerator):
	//Dessin du caret
                   ev.Graphics.FillRectangle(Brushes.MistyRose, new 
                                      Rectangle(ev.Bounds.X, ev.Bounds.Y, wit, hgt));
	   ev.Graphics.DrawRectangle(Pens.Blue, new 
                                        Rectangle(ev.Bounds.X, ev.Bounds.Y, wit, hgt));
	   ev.Graphics.DrawString(caption, myFont, Brushes.Brown, 
                                         ev.Bounds.X + this.iwIcon, ev.Bounds.Y, format);
	   break;
               default:
	   //Effacer le caret
	     System.Drawing.SolidBrush br = new SolidBrush(this.BackColor);
	     ev.Graphics.FillRectangle(br, new Rectangle(ev.Bounds.X, 
                                                           ev.Bounds.Y, wit, hgt));
	     ev.Graphics.DrawRectangle(mpen, new Rectangle(ev.Bounds.X, 
                                                                                ev.Bounds.Y, wit, hgt));
	    ev.Graphics.DrawString(caption, myFont, 
               Brushes.MediumBlue, ev.Bounds.X + this.iwIcon, ev.Bounds.Y, format);
	    break;
               }
}

//Fonction pour dessiner les autres Items
private void DrawMenuItemLabel(string caption, DrawItemEventArgs ev)
{
            Pen mpen = new Pen(ev.BackColor, 1);
            Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular, 
                                                    GraphicsUnit.Pixel);
            SizeF mySizeF = ev.Graphics.MeasureString(caption, myFont);
            int witd = ev.Bounds.Width - 2;
            int hgt = Convert.ToInt32(mySizeF.Height+2);
            this.iwIcon = 20;

            StringFormat format = StringFormat.GenericTypographic;
            format.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

            ev.DrawBackground();
             if(caption == "-")
              {
	ev.Graphics.DrawLine(new Pen(Color.Brown),ev.Bounds.X, ev.Bounds.Y, 
                                                         ev.Bounds.Width, ev.Bounds.Y);
	return;
	}
	switch(ev.State)
	{
	   case (DrawItemState.HotLight|DrawItemState.NoAccelerator):
	   case DrawItemState.HotLight:
	   case DrawItemState.Selected:
	   case (DrawItemState.Selected|DrawItemState.NoAccelerator):
	        //Dessin du caret
	        ev.Graphics.FillRectangle(Brushes.LemonChiffon, new Rectangle
                                              (ev.Bounds.X, ev.Bounds.Y,  witd, hgt));
	        ev.Graphics.DrawRectangle(Pens.Black, new Rectangle
                                                (ev.Bounds.X, ev.Bounds.Y,  witd, hgt));
	        ev.Graphics.DrawString(caption, myFont, Brushes.Brown, 
                                                ev.Bounds.X + this.iwIcon, ev.Bounds.Y, format);
	        break;
	     case   
              (DrawItemState.Selected|DrawItemState.Grayed|DrawItemState.Disabled):
	      case (DrawItemState.Selected|DrawItemState.Grayed|DrawItemState.Disabled|DrawItemState.NoAccelerator):

	     //Item disbled
	           ev.Graphics.FillRectangle(Brushes.LemonChiffon, new Rectangle
                                                 (ev.Bounds.X, ev.Bounds.Y,  witd, hgt));
	            ev.Graphics.DrawRectangle(Pens.Black, new Rectangle
                                                 (ev.Bounds.X, ev.Bounds.Y,  witd, hgt));
	            ev.Graphics.DrawString(caption, myFont, 
               Brushes.LemonChiffon, ev.Bounds.X + this.iwIcon, ev.Bounds.Y, format);
	             break;
	           default:
		//Effacer le caret
		System.Drawing.SolidBrush br = new SolidBrus(ev.BackColor);
		ev.Graphics.FillRectangle(br, new Rectangle(ev.Bounds.X, 
                                                                     ev.Bounds.Y,  witd, hgt));
		ev.Graphics.DrawRectangle(mpen, new Rectangle
                                                            (ev.Bounds.X, ev.Bounds.Y,  witd, hgt));
		ev.Graphics.DrawString(caption, myFont, Brushes.Black, 
                                                ev.Bounds.X + this.iwIcon, ev.Bounds.Y, format);
		    break;
	}
}
private void MesureItemLabel(string caption, MeasureItemEventArgs ev)
{
    Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular, 
                                          GraphicsUnit.Pixel);
     SizeF mySizeF = ev.Graphics.MeasureString(caption, myFont);

     this.iwIcon = 16;
     if(caption == "-")
     {
            ev.ItemHeight = 4; 
      }
      else
     {
          ev.ItemHeight = Convert.ToInt32(mySizeF.Height + 4);
      }
      ev.ItemWidth = Convert.ToInt32(mySizeF.Width + this.iwIcon);
}
private void MesureItemPopLabel(string caption, MeasureItemEventArgs ev)
{
    Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular, 
                                             GraphicsUnit.Pixel);
     SizeF mySizeF = ev.Graphics.MeasureString(caption, myFont);

      this.iwIcon = 16;
      ev.ItemHeight = Convert.ToInt32(mySizeF.Height + 4);
      ev.ItemWidth = Convert.ToInt32(mySizeF.Width);
}
		

//Fichier Item
private void miFichier_DrawItem(object sender,
                                             System.Windows.Forms.DrawItemEventArgs e)
{
     MenuItem itm = (MenuItem) sender;
    string sCaption = "";
    sCaption = itm.Text;
    this.DrawMenuPopLabel(sCaption, e);
}

private void miFichier_MeasureItem(object sender, 
                                           System.Windows.Forms.MeasureItemEventArgs e)
{
     MenuItem itm = (MenuItem) sender;
     string sCaption = "";
     sCaption = itm.Text;
     this.MesureItemPopLabel(sCaption, e);
}
//Open Item
private void miOpen_DrawItem(object sender, 
                                               System.Windows.Forms.DrawItemEventArgs e)
{
    MenuItem itm = (MenuItem) sender;
    string sCaption = "";
    sCaption = itm.Text;
    this.DrawMenuItemLabel(sCaption, e);
}

private void miOpen_MeasureItem(object sender, 
                                            System.Windows.Forms.MeasureItemEventArgs e)
{
    MenuItem itm = (MenuItem) sender;
    string sCaption = "";
    sCaption = itm.Text;
    this.MesureItemLabel(sCaption, e);
}

Conclusion :


Cette astuce vous permet de dessiner et de changer les menus à votre guise
Il se peut aussi de dessiner des icons et de changer le fonds avec une autre image

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.