控件聚焦时绘制边框

时间:2015-04-01 02:16:30

标签: c# winforms

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

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

1 个答案:

答案 0 :(得分:1)

试试这个。 (原来比我想象的要糟糕得多。)

   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));
         }
      }
   }