【问题标题】:C# winforms button with solid border, like 3d带有实线边框的 C# winforms 按钮,如 3d
【发布时间】:2016-09-25 20:34:03
【问题描述】:

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

面板BorderStyle可以设置为Fixed3D,但按钮BorderStyle不能设置为Fixed3D

我也已经尝试过FlatAppearance,实际上是扁平风格。

【问题讨论】:

  • 请在做一些研究后提出问题。这是一个非常简单的问题,如果付出努力,将很容易获得。无论如何,答案是将 BorderStyle 设置为 Fixed3D。
  • FlatAppearance= 我认为弹出窗口会帮助你
  • 面板BorderStyle可以设置为Fixed3D,但按钮BorderStyle不能设置为Fixed3D。 FlatAppearance 实际上是 FlatStyle,我已经尝试过了。

标签: c# winforms button


【解决方案1】:

您可以通过这种方式自定义Button 控件,使其具有厚的 3d 边框:

  • 将按钮FlatStyle设置为Flat
  • FlatApperanace 中将BorderSize 设置为0
  • FlatApperanace 中将MouseOverBackColor 设置为ControlLight

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

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

结果如下:

【讨论】:

  • 我试图覆盖 OnPaint,但结果很糟糕。你的解决方案是完美的。非常感谢!
  • 不客气。我也更喜欢从Button 派生并覆盖OnPaint,特别是如果你想让它成为可重用的控件。
【解决方案2】:

添加到 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();

    }

【讨论】:

    【解决方案3】:

    如果不关心边框有多粗,可以使用下面的代码。 (我选择我的复选框的外观为按钮,我喜欢它在未选中时升起,在选中时下沉)

    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)。在之后弹出的代码窗口中填写您的代码。那么你就准备好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2015-04-13
      相关资源
      最近更新 更多