c# - 当 UserControl 没有边框样式时,如何在设计 View 中突出显示 UserControl 使用的区域?

标签 c# .net winforms visual-studio user-controls

我开发了一个自定义 UserControl。当我将它添加到设计 View 中的表单时,它周围没有明显的边框(除非我将 BorderStyle 属性更改为 None 以外的其他内容)。

一些控件(例如 PictureBoxes)有一个虚线轮廓来指示它们正在使用的区域。有没有办法为 UserControl 执行此操作?

我正在使用 C#、.NET 3.5、Windows 窗体。

最佳答案

您需要为您的 UserControl 编写自定义设计器,这与 WinForms 为 Panel 控件所做的相同。设计器类中的代码覆盖了 OnPaintAdornments method为了在控件的客户区周围绘制虚线边框。

最简单的入门方法是从 ScrollableControlDesigner 类继承,这将免费为您提供大部分必要的功能。然后将逻辑添加到这些方法中:

public class MyUserControlDesigner : ScrollableControlDesigner
{
   public MyUserControlDesigner()
   {
      base.AutoResizeHandles = true;
   }

   protected override void OnPaintAdornments(PaintEventArgs p)
   {
      // Get the user control that we're designing.
      UserControl component = (UserControl)base.Component;

      // As you mentioned, no reason to draw this border unless the
      // BorderStyle property is set to "None"
      if (component.BorderStyle == BorderStyle.None)
      {
         this.DrawBorder(p.Graphics);
      }

      // Call the base class.
      base.OnPaintAdornments(p);
   }

   protected virtual void DrawBorder(Graphics g)
   {
      // Get the user control that we're designing.
      UserControl component = (UserControl)base.Component;

      // Ensure that the user control we're designing exists and is visible.
      if ((component != null) && component.Visible)
      {
         // Draw the dashed border around the perimeter of its client area.
         using (Pen borderPen = this.BorderPen)
         {
            Rectangle clientRect = this.Control.ClientRectangle;
            clientRect.Width--;
            clientRect.Height--;
            g.DrawRectangle(borderPen, clientRect);
         }
      }
   }

   protected Pen BorderPen
   {
      get
      {
         // Create a Pen object with a color that can be seen on top of
         // the control's background.
         return new Pen((this.Control.BackColor.GetBrightness() < 0.5) ?     
                         ControlPaint.Light(this.Control.BackColor)
                         : ControlPaint.Dark(this.Control.BackColor))
                         { DashStyle = DashStyle.Dash };
      }
   }
}

完成后,您需要指示您的 UserControl 类使用您编写的自定义设计器。这是通过添加 DesignerAttribute 来完成的到它的类定义:

[Designer(typeof(MyUserControlDesigner)), DesignerCategory("UserControl")]
public class MyUserControl : UserControl
{
    // insert your code here
} 

当然,这将要求您将对 System.Design.dll 的引用添加到您的程序集中,迫使您以 完整 版本的 .NET 为目标框架(而不是“客户资料”)。

关于c# - 当 UserControl 没有边框样式时,如何在设计 View 中突出显示 UserControl 使用的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6799519/

相关文章:

c# - 我如何为文本字段生成动态 ID 并从 asp.net 4 mvc3 中的模型数组获取数据

c# - 仅当 XML 发生更改时才从 XML 生成 C# 类

c# - 从 VSTO 项目中的 Excel 工作簿中读取五十万条记录

c# - 在 Cosmos DB 中存储具有动态属性的对象

.net - 如何在 Application.ThreadException 事件处理程序中获取整个异常链?

c# - xpath 返回字符串而不是节点列表

c# - 在 .Net 中使用 IE 设置中的代理自动配置

c# - C#中#if有什么用?

macos - 在Mac上以32位模式运行交互式F#

c# - 我试图从网站上读取 RSS 提要,但希伯来语字体看起来很乱,我该如何修复它?