【问题标题】:Rule-Lines in Dialog Boxes对话框中的规则线
【发布时间】:2011-02-11 23:57:06
【问题描述】:

示例: 有谁知道这些对话框规则线是如何创建的?它们可以在设计时使用通用控件创建还是自定义控件?

我想简单地在我的表单上拖一个并定位它,但我找不到控件(如果它确实存在的话)。

有人有创建这些的经验吗?

(我使用的是 Visual Studio .NET 2010;C#)

【问题讨论】:

  • 查看我的回答 here 以获得完整的即插即用解决方案。没有人工制品,没有ControlPaint,没有混乱。

标签: .net dialog


【解决方案1】:

好的,伙计们,多亏了从上述所有三个答案(Hans Passant、Reddog 和 itowlson)中收集到的信息,我已经找到了一个可行的解决方案。我在下面提供了完整的详细信息以供将来的询问者使用。

基本上,它是一个子类 GroupBox 控件,其中仅绘制顶部边框(使用 ControlPaint.DrawBorder3D),并且默认情况下 GroupBox.Text 属性设置为 emtpy。

创建一个类文件(例如 Seperator.cs),将其添加到您的项目中并将以下内容粘贴到其中:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace your_namespace
{
    public class Separator : GroupBox
    {
        [DefaultValue("")]
        public override String Text
        {
            get {return String.Empty;}
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            ControlPaint.DrawBorder3D(
                e.Graphics,
                this.ClientRectangle,
                Border3DStyle.Etched,
                Border3DSide.Top
             );
        }
    }
}

编译您的项目。编译项目后,“分隔符”组件将显示在 Visual Studio 设计器工具箱的“your_namespace 组件”部分中。然后,您可以将“分隔符”组件拖到您的表单上,根据您的喜好定位和塑造它。

再次感谢所有花时间回复的人,我希望这对以后希望解决此问题的人有所帮助。

【讨论】:

    【解决方案2】:

    使用 ControlPaint.DrawBorder3D 方法:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
      ControlPaint.DrawBorder3D(
        e.Graphics,
        new Rectangle(10, 10, 300, 10),
        Border3DStyle.Etched,
        Border3DSide.Top);
    }
    

    注意使用 Border3DSide.Top。如果您使用“最小高度的分组框”技巧,这可以避免您在右手端看到的视觉故障。

    【讨论】:

      【解决方案3】:

      根据Hans' answer,GroupBox 通常就足够了,并且可以很好地与操作系统搭配主题。

      我们在控件库中保留以下内容:

      public class Separator : GroupBox
      {
          // Methods
          protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
          {
              base.SetBoundsCore(x, y, width, 3, specified);
          }
      
          // Properties
          [DefaultValue("")]
          public override string Text
          {
              get
              {
                  return string.Empty;
              }
              set
              {
              }
          }
      }
      

      【讨论】:

      • 感谢您的代码。我试过了,但在最右边有一个伪影:oi54.tinypic.com/zklan7.jpg 有没有办法在右边剪掉几个像素使其不显示?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多