private void InitializeToolStrip() { this.InitializeImageList(this.toolStrip1); this.toolStrip1.Renderer = new RolloverItemRenderer(); RolloverItem item = this.CreateRolloverItem( this.toolStrip1, "RolloverItem on ToolStrip", this.Font, infoIconKey, TextImageRelation.ImageBeforeText, exclIconKey); this.toolStrip1.Items.Add(item); item = this.CreateRolloverItem( this.toolStrip1, "RolloverItem on ToolStrip", this.Font, infoIconKey, TextImageRelation.ImageBeforeText, exclIconKey); this.toolStrip1.Items.Add(item); }
// <snippet21> protected override void OnRenderItemImage( ToolStripItemImageRenderEventArgs e) { base.OnRenderItemImage(e); RolloverItem item = e.Item as RolloverItem; // If the ToolSTripItem is of type RolloverItem, // perform custom rendering for the image. if (item != null) { if (item.Clicked) { // The item is in the clicked state, so // draw the image as usual. e.Graphics.DrawImage( e.Image, e.ImageRectangle.X, e.ImageRectangle.Y); } else { // In the unclicked state, gray out the image. ControlPaint.DrawImageDisabled( e.Graphics, e.Image, e.ImageRectangle.X, e.ImageRectangle.Y, item.BackColor); } } }
// <snippet3> // This method handles the Click event for the button. // it selects the first item in the ToolStrip control // by using the ToolStripITem.Select method. private void button1_Click(object sender, EventArgs e) { RolloverItem item = this.toolStrip1.Items[0] as RolloverItem; if (item != null) { item.Select(); this.Invalidate(); } }
private void InitializeContextMenu() { Font f = new System.Drawing.Font( "Arial", 18f, FontStyle.Bold); ContextMenuStrip cms = new ContextMenuStrip(); this.InitializeImageList(cms); cms.Renderer = new RolloverItemRenderer(); cms.AutoSize = true; cms.ShowCheckMargin = false; cms.ShowImageMargin = false; RolloverItem item = this.CreateRolloverItem( cms, "RolloverItem on ContextMenuStrip", f, handIconKey, TextImageRelation.ImageBeforeText, exclIconKey); cms.Items.Add(item); item = this.CreateRolloverItem( cms, "Another RolloverItem on ContextMenuStrip", f, questionIconKey, TextImageRelation.ImageBeforeText, exclIconKey); cms.Items.Add(item); item = this.CreateRolloverItem( cms, "And another RolloverItem on ContextMenuStrip", f, warningIconKey, TextImageRelation.ImageBeforeText, exclIconKey); cms.Items.Add(item); cms.Closing += new ToolStripDropDownClosingEventHandler(cms_Closing); this.button1.ContextMenuStrip = cms; }
// </snippet3> // <snippet2> // This utility method creates a RolloverItem // and adds it to a ToolStrip control. private RolloverItem CreateRolloverItem( ToolStrip owningToolStrip, string txt, Font f, string imgKey, TextImageRelation tir, string backImgKey) { RolloverItem item = new RolloverItem(); item.Alignment = ToolStripItemAlignment.Left; item.AllowDrop = false; item.AutoSize = true; item.BackgroundImage = owningToolStrip.ImageList.Images[backImgKey]; item.BackgroundImageLayout = ImageLayout.Center; item.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText; item.DoubleClickEnabled = true; item.Enabled = true; item.Font = f; // These assignments are equivalent. Each assigns an // image from the owning toolstrip's image list. item.ImageKey = imgKey; //item.Image = owningToolStrip.ImageList.Images[infoIconKey]; //item.ImageIndex = owningToolStrip.ImageList.Images.IndexOfKey(infoIconKey); item.ImageScaling = ToolStripItemImageScaling.None; item.Owner = owningToolStrip; item.Padding = new Padding(2); item.Text = txt; item.TextAlign = ContentAlignment.MiddleLeft; item.TextDirection = ToolStripTextDirection.Horizontal; item.TextImageRelation = tir; return(item); }
// </snippet21> // <snippet22> // This method defines the behavior for rendering the // background of a ToolStripItem. If the item is a // RolloverItem, it paints the item's BackgroundImage // centered in the client area. If the mouse is in the // item's client area, a border is drawn around it. // If the item is on a drop-down or if it is on the // overflow, a gradient is painted in the background. protected override void OnRenderItemBackground( ToolStripItemRenderEventArgs e) { base.OnRenderItemBackground(e); RolloverItem item = e.Item as RolloverItem; // If the ToolSTripItem is of type RolloverItem, // perform custom rendering for the background. if (item != null) { if (item.Placement == ToolStripItemPlacement.Overflow || item.IsOnDropDown) { using (LinearGradientBrush b = new LinearGradientBrush( item.ContentRectangle, Color.Salmon, Color.DarkRed, 0f, false)) { e.Graphics.FillRectangle(b, item.ContentRectangle); } } // The RolloverItem control only supports // the ImageLayout.Center setting for the // BackgroundImage property. if (item.BackgroundImageLayout == ImageLayout.Center) { // Get references to the item's ContentRectangle // and BackgroundImage, for convenience. Rectangle cr = item.ContentRectangle; Image bgi = item.BackgroundImage; // Compute the center of the item's ContentRectangle. int centerX = (cr.Width - bgi.Width) / 2; int centerY = (cr.Height - bgi.Height) / 2; // If the item is selected, draw the background // image as usual. Otherwise, draw it as disabled. if (item.Selected) { e.Graphics.DrawImage(bgi, centerX, centerY); } else { ControlPaint.DrawImageDisabled( e.Graphics, bgi, centerX, centerY, item.BackColor); } } // If the item is in the rollover state, // draw a border around it. if (item.Rollover) { ControlPaint.DrawFocusRectangle( e.Graphics, item.ContentRectangle); } } }