📄 panelpro.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Custom
{
public partial class PanelPro : Panel
{
#region "构造函数"
/// <summary>
/// 构造函数
/// </summary>
public PanelPro()
{
InitializeComponent();
this.Image = null;
this.Caption = this.Name;
this.TextAlign = ContentAlignment.TopLeft;
this.SizeMode = PictureBoxSizeMode.Normal;
this.TextFont = new Font(FontFamily.GenericSerif, 9, FontStyle.Regular);
this.TextColor = Color.Black;
_p = new Pen(Color.Black);
}
#endregion
#region "属性"
#region "受保护的属性"
/// <summary>
/// 文字
/// </summary>
private string _text;
/// <summary>
/// 文字对齐方式
/// </summary>
private ContentAlignment _textalign;
/// <summary>
/// 图像
/// </summary>
private Image _image;
/// <summary>
/// 图像显示方式
/// </summary>
private PictureBoxSizeMode _sizemode;
/// <summary>
/// 字体
/// </summary>
private Font _font;
/// <summary>
/// 前景色
/// </summary>
private Color _forecolor;
/// <summary>
/// 边框笔
/// </summary>
private Pen _p;
/// <summary>
/// 文字刷
/// </summary>
private Brush _b;
#endregion
#region "公共属性"
#region "文字"
/// <summary>
/// 获取或设置显示的文字
/// </summary>
public string Caption
{
get { return _text; }
set
{
_text = value;
this.Invalidate();
}
}
#endregion
#region "字体"
/// <summary>
/// 获取或设置字体
/// </summary>
public Font TextFont
{
get { return _font; }
set
{
_font = value;
this.Invalidate();
}
}
#endregion
#region "前景色"
/// <summary>
/// 获取或设置前景色
/// </summary>
public Color TextColor
{
get { return _forecolor; }
set
{
_forecolor = value;
if (_b != null)
{
_b.Dispose();
}
_b = new SolidBrush(_forecolor);
this.Invalidate();
}
}
#endregion
#region "文字对齐方式"
/// <summary>
/// 获取或设置文字对齐方式
/// </summary>
public ContentAlignment TextAlign
{
get { return _textalign; }
set
{
_textalign = value;
this.Invalidate();
}
}
#endregion
#region "图像"
/// <summary>
/// 获取或设置显示的图像
/// </summary>
public Image Image
{
get { return _image; }
set
{
_image = value;
this.Invalidate();
}
}
#endregion
#region "图像显示方式"
/// <summary>
/// 获取或设置图像显示方式
/// </summary>
public PictureBoxSizeMode SizeMode
{
get { return _sizemode; }
set
{
_sizemode = value;
this.Invalidate();
}
}
#endregion
#endregion
#endregion
#region "方法"
#region "受保护的方法"
#region "绘图"
/// <summary>
/// 重载绘图
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
//画边框
using (Pen p = new Pen(Color.Black))
{
pe.Graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
}
//画图像
if (_image != null)
{
Rectangle r;
switch (_sizemode)
{
case PictureBoxSizeMode.CenterImage:
r = new Rectangle(1 + (Width - 2 - _image.Width) / 2, 1 + (Height - 2 - _image.Height) / 2, _image.Width, _image.Height);
break;
case PictureBoxSizeMode.Normal:
r = new Rectangle(1, 1, _image.Width, _image.Height);
break;
case PictureBoxSizeMode.StretchImage:
r = new Rectangle(1, 1, Width - 2, Height - 2);
break;
default:
r = new Rectangle(1, 1, _image.Width, _image.Height);
break;
}
pe.Graphics.DrawImage(_image, r, new Rectangle(0, 0, _image.Width, _image.Height), GraphicsUnit.Pixel);
}
//画文字
if (_text.Length > 0)
{
switch (_textalign)
{
case ContentAlignment.TopCenter:
pe.Graphics.DrawString(_text, _font, _b, (Width - 2 - pe.Graphics.MeasureString(_text, _font).Width) / 2, (Height - 2 - pe.Graphics.MeasureString(_text, _font).Height) / 2);
break;
case ContentAlignment.TopLeft:
pe.Graphics.DrawString(_text, _font, _b, 1, (Height - 2 - pe.Graphics.MeasureString(_text, _font).Height) / 2);
break;
case ContentAlignment.TopRight:
pe.Graphics.DrawString(_text, _font, _b, Width - 2 - pe.Graphics.MeasureString(_text, _font).Width, (Height - 2 - pe.Graphics.MeasureString(_text, _font).Height) / 2);
break;
default:
pe.Graphics.DrawString(_text, _font, _b, 1, (Height - 2 - pe.Graphics.MeasureString(_text, _font).Height) / 2);
break;
}
}
// 调用基类 OnPaint
base.OnPaint(pe);
}
#endregion
#endregion
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -