0

If you take a look at the attached image, is there a way to get the drawing logic for this hover effect from the system renderer of the standard WinForms toolstrip ?

http://imageshack.us/photo/my-images/10/toolstriphovereffect.jpg/

EDIT: Anyway, I've manually implemented this with images, but if anyone comes here with a solution, please post.

Denis Biondic
  • 7,943
  • 5
  • 48
  • 79

1 Answers1

2

Maybe this code helps. It draws red circle with black border around toolstripbutton when mouse is over it.

Set your toolstrip properties:

//Set render mode to professional
myToolStrip.RenderMode = ToolStripRenderMode.Professional;
//Assign new instance of your custom renderer
myToolStrip.Renderer = new MyCustomRenderer();

Custom renderer class:

public class MyCustomRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        if (!e.Item.Selected) 
            base.OnRenderButtonBackground(e);
        else
        {
            Rectangle rectangle = new Rectangle(0, 0, e.Item.Size.Width - 1, e.Item.Size.Height - 1);
            //Draw red circle
            e.Graphics.FillEllipse(Brushes.Red, rectangle);
            //Draw black border
            e.Graphics.DrawEllipse(Pens.Black, rectangle);
        }
    }
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • Thank you for your answer, but I have no problem with basic drawing, I just don't want to recreate the entire effect if there is a way to get it from WinForms. Also, I want to use the effect for button controls, not in the toolstrip – Denis Biondic Jul 19 '11 at 09:26
  • Ok i probably misunderstood question. So you want to use that(as shown in image) effect on System.Windows.Forms.Button control? – Renatas M. Jul 19 '11 at 09:38
  • Yes, and because I don't see a way of "extracting" that effect I'm just creating a button subclass that can apply rollover / push images, but I will have the problem when I have buttons of different size (for each button different images) – Denis Biondic Jul 19 '11 at 11:21
  • Can you show how you do that? Iam getting interested in this :) your buttons FlatStyle=Flat without borders in normal state? – Renatas M. Jul 19 '11 at 11:53
  • @Denis you want to achieve this effect by you manually? Cant you use third party controls who already supports this effect? – Renatas M. Jul 19 '11 at 12:28
  • Well, we are using DevExpress and it doesn't support this either, about the control - I can't really share the source code, but it is actually simple. You inherit the Control and implement IButtonControl, and override drawing. Specify MouseOver and Pushed images, and also add a "generic" Image property as normal buttons have (when you inherit Control, you get the Text property, but there is no Image property). Then in your paint, you do: 1. draw MouseOver / Pushed iamge (according to button state) 2. Draw "generic" small image 3. Draw text – Denis Biondic Jul 19 '11 at 12:50
  • All can be easily done with GDI+, TextRenderer.MeasureText, and such – Denis Biondic Jul 19 '11 at 12:51