C#經由過程重寫Panel轉變邊框色彩與寬度的辦法。本站提示廣大學習愛好者:(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#法式設計有所贊助。