繁体   English   中英

控件集中时绘制边框

[英]Draw border when the Control is focused

我想在任何具有焦点的控件上绘制边框,当控件不再具有焦点时,边框必须消失。 我试图在下面的代码中绘制边框,但是我不知道如何在边框消失之前对其进行绘制。

void mButton_Paint(object sender, PaintEventArgs e) {
    ControlPaint.DrawBorder(e.Graphics, ((Control)sender).ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}

尝试这个。 (结果比我预期的要混乱得多。)

   public partial class FormSO29381768 : Form
   {
      // Constructor
      public FormSO29381768()
      {
         InitializeComponent();

         InstallEventHandlers(this);
      }


      /// <summary>
      /// Recursive method to install the paint event handler for all container controls on a form, 
      /// including the form itself, and also to install the "enter" event handler for all controls 
      /// on a form.
      /// </summary>
      private void InstallEventHandlers(Control containerControl)
      {
         containerControl.Paint -= Control_Paint;  // Defensive programming
         containerControl.Paint += Control_Paint;

         foreach (Control nestedControl in containerControl.Controls)
         {
            nestedControl.Enter -= Control_ReceivedFocus;  // Defensive programming
            nestedControl.Enter += Control_ReceivedFocus;

            if (nestedControl is ScrollableControl)
               InstallEventHandlers(nestedControl);
         }
      }


      /// <summary>
      /// Event handler method that gets called when a control receives focus. This just indicates 
      /// that the whole form needs to be redrawn. (This is a bit inefficient, but will presumably 
      /// only be noticeable if there are many, many controls on the form.)
      /// </summary>
      private void Control_ReceivedFocus(object sender, EventArgs e)
      {
         this.Refresh();
      }


      /// <summary>
      /// Event handler method to draw a dark blue rectangle around a control if it has focus, and 
      /// if it is in the container control that is invoking this method.
      /// </summary>
      private void Control_Paint(object sender, PaintEventArgs e)
      {
         Control activeControl = this.ActiveControl;
         if (activeControl != null && activeControl.Parent == sender) 
         {
            e.Graphics.DrawRectangle(Pens.DarkBlue, 
                        new Rectangle(activeControl.Location.X - 2, activeControl.Location.Y - 2, 
                                      activeControl.Size.Width + 4, activeControl.Size.Height + 4));
         }
      }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM