Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# winforms button with solid border, like 3d

How can I create button with solid border(3d), like picture below on C# winforms?

3d-button

Panel BorderStyle can be set as Fixed3D, but buttons BorderStyle cannot be set as Fixed3D.

I also already tried FlatAppearance which is actualy flat style.

like image 901
thecrazyjo Avatar asked May 27 '16 13:05

thecrazyjo


2 Answers

You can customize the Button control this way have thick 3d borders:

  • Set the Button FlatStyle to be Flat
  • In the FlatApperanace set BorderSize to 0
  • In the FlatApperanace set MouseOverBackColor to ControlLight

Then handle Paint event and using ControlPaint.DrawBorder draw a thick 3d border:

private void button1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, button1.ClientRectangle,
        SystemColors.ControlLightLight, 5, ButtonBorderStyle.Outset,
        SystemColors.ControlLightLight, 5, ButtonBorderStyle.Outset,
        SystemColors.ControlLightLight, 5, ButtonBorderStyle.Outset,
        SystemColors.ControlLightLight, 5, ButtonBorderStyle.Outset);
}

And here is the result:

enter image description here

like image 171
Reza Aghaei Avatar answered Sep 20 '22 14:09

Reza Aghaei


If you don't care about how thick the border is, you can use the following code. (I choose my checkbox' appearance as button, and I like it raised when not checked and sunken when checked)

private void checkbox_paint(object sender, PaintEventArgs e)
        {
            CheckBox myCheckbox = (CheckBox)sender;
            Rectangle borderRectangle = myCheckbox.ClientRectangle;
            if (myCheckbox.Checked)
            {
                ControlPaint.DrawBorder3D(e.Graphics, borderRectangle,
                    Border3DStyle.Sunken);
            }
            else
            {
                ControlPaint.DrawBorder3D(e.Graphics, borderRectangle,
                    Border3DStyle.Raised);
            }
        }

Just in case you don't know how to use this code: All you need to do is choose your button/checkbox in your designer, then go to the Properties window and choose the Events tab (click the Thunderbolt). Find the event called Paint and type your event handler's name without the parenthesis in box next to it (such as checkbox_paint). In the code window that pops up afterwards fill in your code. Then you are all set.

like image 35
Girl Spider Avatar answered Sep 21 '22 14:09

Girl Spider