Change WinForms button highlight color

荒凉一梦 提交于 2019-12-07 11:17:34

问题


I found this page, which outlines how to change the rendering for a MenuStrip and its items.

I want to use this, but the problem is that the highlight color when you hover over a button doesn't match it.

Is there any way to change the highlight color from blue to yellow? I've tried using the MouseHover and MouseLeave events, but for some reason they're really slow, and they change the button to a solid color, which looks bad, but leaves a border on the edge of the button that doesn't change.

In the designer:

this.ButtonName.MouseHover += new System.EventHandler(button_mousehover);

And then in the Code:

private void button_mousehover(object sender, EventArgs e)
{
    Button btn = sender as Button;
    btn.BackColor = Color.Yellow;
}

Is there anything as easy as in the link I posted above to change the highlight color from blue to something else?

Here's the code for changing the rendering of the menu strip:

private void myForm Load(object sender, EventArgs e)
{
    myMenuStrip.Renderer = new MenuRenderer();
{

private class MenuRenderer : ToolStripProfessionalRenderer
{
    public MenuRenderer() : base(new MyColors()) { }
}

private class MyColors : ProfessionalColorTable
{
    public override Color MenuItemSelectedGradientBegin
    {
        get { return Color.Orange; }
    }
    public override Color MenuItemSelectedGradientEnd
    {
        get { return Color.Yellow; }
    }
    public override Color MenuItemPressedGradientBegin
    {
        get{ return Color.Yellow; }
    }
    public override Color MenuItemPressedGradientEnd
    {
        get { return Color.Orange; }
    }
    public override Color MenuItemSelected
    {
        get { return Color.Gold; }
    }
}

So it'll change the background of a hovered-over menu item to an orange-yellow gradient, change it to a yellow-orange gradient on click, and any item in the menu will have a gold highlight on hovering.

What I'm trying to do is do that last part (change the highlight to gold/yellow) for the buttons in my form.


回答1:


In the properties of the button:
under Appearance/FlatStyle, select "Flat".
Then, under Appearance/FlatAppearance/MouseOverBackColor, select the highlight color you want.




回答2:


You can take a look at the Button Renderer.

Why do you want to override the renderer when you can simply subscribe to the MouseHover event like so:

this.someButtonName.MouseHover += (s,e) => 
{
   this.someButtonName.BackColor = Color.Yellow;
};

I recommend you use a mouse leave too in order to reset the button to it's initial color when your mouse isn't on it anymore.



来源:https://stackoverflow.com/questions/27826699/change-winforms-button-highlight-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!