【问题标题】:How can I force the focus outline to be drawn around the checkbox instead of the text on a checkbox control?如何强制围绕复选框而不是复选框控件上的文本绘制焦点轮廓?
【发布时间】:2009-05-29 00:44:22
【问题描述】:

通常,在窗体上的控件之间切换时,当焦点设置为 CheckBox 控件时,文本会被勾勒出来以显示焦点。

我正在使用不带文本的 CheckBox(因此只显示该框)。如何强制围绕框而不是文本绘制焦点轮廓?

【问题讨论】:

    标签: .net winforms


    【解决方案1】:

    看看 - 只是给你一个想法:

    public class MyCheckBox : CheckBox
    {
      public MyCheckBox()
      {
        // AutoSize is virtual - so you should not call it here, just demo
        AutoSize = false;
        // You need padding to make the base.OnPaint() method leaving you some space
        Padding = new Padding( 2, 2, 0, 0 );
        Size = new Size( 17, 16 );
      }
    
      protected override void OnPaint( PaintEventArgs pevent )
      {
        base.OnPaint( pevent );
        if( !Focused )
        {
          return;
        }
        using( var pen = new Pen( Color.Black ) )
        {
          pen.DashStyle = DashStyle.Dot;
          pevent.Graphics.DrawRectangle( pen, new Rectangle( 0, 0, 16, 15 ) );
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您必须派生自己的“无文本复选框”才能正确识别控件的边界。

      我认为可能听起来很复杂,但应该不会太难。我在学习 .NET 1.x 时创建了一些花哨的复选框,但我不记得担心焦点矩形。

      【讨论】:

        猜你喜欢
        • 2012-04-18
        • 2011-03-24
        • 1970-01-01
        • 2013-03-30
        • 2014-06-17
        • 1970-01-01
        • 2018-08-30
        • 2015-06-21
        • 1970-01-01
        相关资源
        最近更新 更多