SoFunction
Updated on 2025-03-07

C# Method to change border color and width by rewriting Panel

This article describes the method of C# changing the border color and width by rewriting Panel. Share it for your reference. The specific implementation method is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace ImageStudio
{
 public class PanelEx : 
 {
  [DllImport("")]
  private static extern IntPtr GetWindowDC(IntPtr hwnd);
  [DllImport("")]
  private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
  private Color _borderColor = ;
  private int _borderWidth = 1;
  //
  // Summary:  // Get or set the border color of the control.  //
  // Return result:  // The border color of the control.  Default is  // The value of the attribute.  [Description("Border color of component."), Category("Appearance")]
  public Color BorderColor
  {
   get
   {
    return _borderColor;
   }
   set
   {
    _borderColor = value;
    ();
   }
  }
  //
  // Summary:  // Get or set the border width of the control.  //
  // Return result:  // The border width of the control is int.  Default is 1  // The value of the attribute.  [Description("Component's border width."), Category("Appearance")]
  public int BorderWidth
  {
   get
   {
    return _borderWidth;
   }
   set
   {
    _borderWidth = value;
    ();
   }
  }
  public PanelEx()
  {
   SetStyle(, true);
   SetStyle(, false);
   SetStyle(, true);
   SetStyle(, true);
   SetStyle(, true);
   +=new PaintEventHandler(PanelEx_Paint);
  }
  private void PanelEx_Paint(object sender, PaintEventArgs e)
  {
   if ( == )
   {
    IntPtr hDC = GetWindowDC();
    Graphics g = (hDC);
    (
     g,
     new Rectangle(0, 0, , ),
     _borderColor,
     _borderWidth,
     ,
     _borderColor,
     _borderWidth,
     ,
     _borderColor,
     _borderWidth,
     ,
     _borderColor,
     _borderWidth,
     );
    ();
    ReleaseDC(Handle, hDC);
   }
  }
 }
}

I hope this article will be helpful to everyone's C# programming.