65.9K
CodeProject 正在变化。 阅读更多。
Home

带设计器选择规则的斜面线控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (10投票s)

2005年6月16日

2分钟阅读

viewsIcon

59065

downloadIcon

1298

一个带有 Visual Studio 设计器 SelectionRule 支持的斜面线控件。

Sample Image - BevelLineScreenshot.gif

引言

当我看到(很久以前)Visual Studio 2002 的工具箱中没有这个控件时,我决定编写这个斜面线控件。 来自 Visual Basic 6 背景,我失望地发现没有一个形状控件(将在另一篇文章中介绍)来生成斜面线。

使用代码

这里是所有能产生差异的属性的示例

   bevelLine1.BevelLineWidth = 1;
   bevelLine1.Blend = false;
   bevelLine1.TopLineColor = SystemColors.ControlDark;
   bevelLine1.BottomLineColor = SystemColors.ControlLightLight;
   bevelLine1.Orientation = Orientation.Horizontal;

在使用表单设计器时,如果控件位于水平位置,您只能拖动控件的宽度。 要更改控件高度,请设置 BevelLineWidth。 使用 Blend 功能将执行渐变填充而不是纯色填充。

关注点

这个控件与其他控件的不同之处在于我实现了一个 ControlDesigner。 当选择 BeveLine 控件时,这会在视觉上指导开发人员,以确定它是可以从左到右调整大小还是取决于方向的上下调整大小。

这在 Visual Studio 2005 Beta 2 中不起作用,因为他们修改了选择绘图例程。 在早期版本的 Visual Studio 中,它将在设计时在控件周围绘制所有选择手柄,但不允许开发人员更改禁用的选项。 但是在 Visual Studio 2005 Beta 2 中,它将不会绘制禁用的 SelectionRules,并且如果控件的高度小于 18,它将不会显示!

使用 Designer 属性允许我们从控件引用设计器。

 [Designer(typeof(BevelLineDesigner))]
 public class BevelLine : System.Windows.Forms.Control
 {
    // code...
 }

这是 BevelLine 的设计器代码。 它继承自 ControlDesigner,您必须添加引用 System.Design 才能在您的项目中使用。

重写 SelectionRules 允许您设置选择手柄,开发人员是否可以在设计时移动它,以及它是否可见。 实际上,您可以在设计时通过使用 base.Control 属性并将其转换为您的控件来引用实际的控件。

public class BevelLineDesigner : System.Windows.Forms.Design.ControlDesigner
{
  public BevelLineDesigner()
  {
   
  }
  public override SelectionRules SelectionRules
  {
   get
   {
    SelectionRules rules; 
    rules = base.SelectionRules;
    
    // If using VS.net 2005 Beta 2 then comment this code to have
    // selective grip handles on the BevelLine control in the Designer
    if (((BevelLine)base.Control).Orientation == 
          System.Windows.Forms.Orientation.Horizontal)
    {
        rules = SelectionRules.Moveable | SelectionRules.Visible
            | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
    }
    else
    {
        rules = SelectionRules.Moveable | SelectionRules.Visible
            | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
    }
   
    return rules;
   }
  }
}

历史

  • 作为 Visual Studio 2005 Beta 2 解决方案上传 - 2005 年 6 月 16 日。
  • 作为 Visual Studio 2003 解决方案上传 - 2005 年 6 月 16 日。
© . All rights reserved.