C# winforms button with solid border, like 3d

16,551

Solution 1

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

Solution 2

Adding to Reza's post (thanks Reza!) ... you could get a bit fancier and invert the 3D effect when the button is down:

    private bool blnButtonDown = false;

    private void button_Paint(object sender, PaintEventArgs e)
    {
        if (blnButtonDown == false)
        {
            ControlPaint.DrawBorder(e.Graphics, (sender as System.Windows.Forms.Button).ClientRectangle,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Outset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Outset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Outset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Outset);
        }
        else
        {
            ControlPaint.DrawBorder(e.Graphics, (sender as System.Windows.Forms.Button).ClientRectangle,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Inset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Inset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Inset,
                System.Drawing.SystemColors.ControlLightLight, 2, ButtonBorderStyle.Inset);
        }
    }

    private void button_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        blnButtonDown = true;
        (sender as System.Windows.Forms.Button).Invalidate();
    }

    private void button_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        blnButtonDown = false;
        (sender as System.Windows.Forms.Button).Invalidate();

    }

Solution 3

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.

Share:
16,551
thecrazyjo
Author by

thecrazyjo

Updated on July 18, 2022

Comments

  • thecrazyjo
    thecrazyjo almost 2 years

    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.

    • SunilA
      SunilA almost 8 years
      Pls put questions after doing some research. This is a pretty easy question and would be easily available if efforts are put. Anyways the answer is set BorderStyle as Fixed3D.
    • Uthistran Selvaraj
      Uthistran Selvaraj almost 8 years
      FlatAppearance= Popup will help you I think
    • thecrazyjo
      thecrazyjo almost 8 years
      Panel BorderStyle can be set as Fixed3D, but buttons BorderStyle cannot be set as Fixed3D. FlatAppearance is actualy FlatStyle and I already tried this.
  • thecrazyjo
    thecrazyjo almost 8 years
    I tried to override OnPaint, but the result was awful. Your solution is perfect. Thank you very much!
  • Reza Aghaei
    Reza Aghaei almost 8 years
    You are welcome. I also prefer deriving from Button and overriding OnPaint specially if you want to make it a reusable control.
  • Brad
    Brad about 2 years
    When I use this ControlPaint.DrawBorder approach on a button that I have set the Fore/Back colors on, it works well until I move the mouse into the button. Then, the background color changes back to a light color. Is this DrawBorder call redrawing everything inside the rectangle and thus overriding the BackColor with the ControlLightLight color?