带有实线边框的C#winforms按钮,如3d

时间:2016-05-27 13:18:24

标签: c# winforms button

如何创建带有实线边框(3d)的按钮,如下图C#winforms?

3d-button

小组BorderStyle可以设置为Fixed3D,但按钮BorderStyle不能设置为Fixed3D

我也尝试了FlatAppearance这是一种实际的平面风格。

3 个答案:

答案 0 :(得分:9)

您可以通过这种方式自定义Button控件具有粗三维边框:

  • 将按钮FlatStyle设置为Flat
  • FlatApperanaceBorderSize0
  • FlatApperanaceMouseOverBackColorControlLight

然后处理Paint事件并使用ControlPaint.DrawBorder绘制一个粗三维边框:

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);
}

结果如下:

enter image description here

答案 1 :(得分:1)

添加到Reza的帖子中(感谢Reza!)...当按钮按下时,您可能会更喜欢并反转3D效果:

    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();

    }

答案 2 :(得分:0)

如果您不关心边框的粗细,可以使用以下代码。 (我选择复选框的外观作为按钮,我喜欢它在未选中的情况下凸起而在选中的状态下凹陷)

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);
            }
        }

以防万一您不知道如何使用此代码:只需要做的就是在设计器中选择按钮/复选框,然后转到“属性”窗口并选择“事件”选项卡(单击“ Thunderbolt”)。找到名为Paint的事件,并在事件处理程序的名称旁边不带括号的地方键入事件处理程序的名称(例如checkbox_paint)。在随后弹出的代码窗口中,填写您的代码。然后,您一切就绪。