C#通过重写Panel改变边框颜色与宽度的方法

 更新时间:2015年08月21日 15:29:48   作者:我心依旧  
这篇文章主要介绍了C#通过重写Panel改变边框颜色与宽度的方法,涉及C#针对Panel控件的重写与属性设置技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#通过重写Panel改变边框颜色与宽度的方法。分享给大家供大家参考。具体实现方法如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace ImageStudio
{
 public class PanelEx : System.Windows.Forms.Panel
 {
  [DllImport("user32.dll")]
  private static extern IntPtr GetWindowDC(IntPtr hwnd);
  [DllImport("user32.dll")]
  private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
  private Color _borderColor = Color.Black;
  private int _borderWidth = 1;
  //
  // 摘要:
  //  获取或设置控件的边框颜色。
  //
  // 返回结果:
  //  控件的边框颜色 System.Drawing.Color。默认为 System.Drawing.Color.Black
  //  属性的值。
  [Description("组件的边框颜色。"), Category("Appearance")]
  public Color BorderColor
  {
   get
   {
    return _borderColor;
   }
   set
   {
    _borderColor = value;
    this.Invalidate();
   }
  }
  //
  // 摘要:
  //  获取或设置控件的边框宽度。
  //
  // 返回结果:
  //  控件的边框宽度 int。默认为 1
  //  属性的值。
  [Description("组件的边框宽度。"), Category("Appearance")]
  public int BorderWidth
  {
   get
   {
    return _borderWidth;
   }
   set
   {
    _borderWidth = value;
    this.Invalidate();
   }
  }
  public PanelEx()
  {
   SetStyle(ControlStyles.DoubleBuffer, true);
   SetStyle(ControlStyles.AllPaintingInWmPaint, false);
   SetStyle(ControlStyles.ResizeRedraw, true);
   SetStyle(ControlStyles.UserPaint, true);
   SetStyle(ControlStyles.SupportsTransparentBackColor, true);
   this.Paint+=new PaintEventHandler(PanelEx_Paint);
  }
  private void PanelEx_Paint(object sender, PaintEventArgs e)
  {
   if (this.BorderStyle == BorderStyle.FixedSingle)
   {
    IntPtr hDC = GetWindowDC(this.Handle);
    Graphics g = Graphics.FromHdc(hDC);
    ControlPaint.DrawBorder(
     g,
     new Rectangle(0, 0, this.Width, this.Height),
     _borderColor,
     _borderWidth,
     ButtonBorderStyle.Solid,
     _borderColor,
     _borderWidth,
     ButtonBorderStyle.Solid,
     _borderColor,
     _borderWidth,
     ButtonBorderStyle.Solid,
     _borderColor,
     _borderWidth,
     ButtonBorderStyle.Solid);
    g.Dispose();
    ReleaseDC(Handle, hDC);
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • c#文件操作示例带详细注释

    c#文件操作示例带详细注释

    System.IO.Directory类和System.DirectoryInfo类主要提供关于目录的各种操作,使用时需要引用System.IO命名空间。下面通过程序实例来介绍其主要属性和方法
    2014-01-01
  • Unity实现坦克模型

    Unity实现坦克模型

    这篇文章主要为大家详细介绍了Unity实现坦克模型,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C# TreeView控件使用代码

    C# TreeView控件使用代码

    TreeView控件的实例代码,需要的朋友可以参考下。
    2009-09-09
  • Unity3D开发教程:愤怒的小鸟

    Unity3D开发教程:愤怒的小鸟

    这篇文章详细的讲解了如何从0开发出一个Unity3D的小游戏愤怒的小鸟,本文包含大量的图片与文字描述,也含有大量的源代码,可以让你快速入手,希望本篇文章对你有所帮助
    2021-06-06
  • c#中抽象类和接口的详细介绍

    c#中抽象类和接口的详细介绍

    这篇文章介绍了c#中抽象类和接口,有需要的朋友可以参考一下
    2013-10-10
  • C#8.0新语法using declaration

    C#8.0新语法using declaration

    这篇文章介绍了C#8.0的新语法using declaration,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • c# RSA非对称加解密及XML&PEM格式互换方案

    c# RSA非对称加解密及XML&PEM格式互换方案

    这篇文章主要介绍了c# RSA非对称加解密及XML&PEM格式互换方案,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • C#多线程系列之工作流实现

    C#多线程系列之工作流实现

    本文详细讲解了C#实现多线程工作流的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • C#中简单的拆箱操作用法实例分析

    C#中简单的拆箱操作用法实例分析

    这篇文章主要介绍了C#中简单的拆箱操作用法.实例分析了C#中拆箱的概念与相关的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#验证给定字符串形式日期是否合法的方法

    C#验证给定字符串形式日期是否合法的方法

    这篇文章主要介绍了C#验证给定字符串形式日期是否合法的方法,实例分析了C#针对字符串及日期的操作技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03

最新评论