winform 自定义label
来源:互联网 发布:mysql数据库登陆 编辑:程序博客网 时间:2024/06/01 07:44
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TomWinform.CustomerControl
{
public partial class VistaLabel : Control
{
private StringAlignment verticalAlignment = StringAlignment.Center;
private StringAlignment horizontalAlignment = StringAlignment.Center;
private Color lineColor = Color.Black;
private Color leftLineColor = Color.Black;
private Color RightLineColor = Color.Black;
private Color TopLineColor = Color.Black;
private Color BottomLineColor = Color.Black;
public VistaLabel()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
StringFormat format = new StringFormat();
format.Alignment = horizontalAlignment;
format.LineAlignment = verticalAlignment;
SizeF fontSize = pe.Graphics.MeasureString(this.Text, this.Font);
this.Height = (int)fontSize.Height + 20;
this.Width = (int)fontSize.Width + 20;
pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, format);
DrawBorder(pe);
base.OnPaint(pe);
}
//画边框
private void DrawBorder(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
Pen pen = new Pen(LineColor);
Point[] point = new Point[4];
point[0] = new Point(0, 0);
point[1] = new Point(Width - 1, 0);
point[2] = new Point(Width - 1, Height - 1);
point[3] = new Point(0, Height - 1);
g.DrawPolygon(pen, point);
}
//画左边框
private void DrawLeftBorder(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
Pen pen = new Pen(leftLineColor);
Point[] point = new Point[2];
point[0] = new Point(0, 0);
point[1] = new Point(0, this.Height - 1);
g.DrawPolygon(pen, point);
}
//画右边框
private void DrawRightBorder(PaintEventArgs pe)
{
}
[Description("文本垂直对齐方式"), Category("自定义属性")]
public StringAlignment VerticalAlignment
{
get
{
return verticalAlignment;
}
set
{
verticalAlignment = value;
this.Invalidate();
}
}
[Description("文本水平对齐方式"), Category("自定义属性")]
public StringAlignment HorizontalAlignment
{
get
{
return horizontalAlignment;
}
set
{
horizontalAlignment = value;
this.Invalidate();
}
}
[Description("边框颜色"), Category("自定义属性")]
public Color LineColor
{
get
{
return lineColor;
}
set
{
lineColor = value;
this.Invalidate();
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TomWinform.CustomerControl
{
public partial class VistaLabel : Control
{
private StringAlignment verticalAlignment = StringAlignment.Center;
private StringAlignment horizontalAlignment = StringAlignment.Center;
private Color lineColor = Color.Black;
private Color leftLineColor = Color.Black;
private Color RightLineColor = Color.Black;
private Color TopLineColor = Color.Black;
private Color BottomLineColor = Color.Black;
public VistaLabel()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
StringFormat format = new StringFormat();
format.Alignment = horizontalAlignment;
format.LineAlignment = verticalAlignment;
SizeF fontSize = pe.Graphics.MeasureString(this.Text, this.Font);
this.Height = (int)fontSize.Height + 20;
this.Width = (int)fontSize.Width + 20;
pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, format);
DrawBorder(pe);
base.OnPaint(pe);
}
//画边框
private void DrawBorder(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
Pen pen = new Pen(LineColor);
Point[] point = new Point[4];
point[0] = new Point(0, 0);
point[1] = new Point(Width - 1, 0);
point[2] = new Point(Width - 1, Height - 1);
point[3] = new Point(0, Height - 1);
g.DrawPolygon(pen, point);
}
//画左边框
private void DrawLeftBorder(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
Pen pen = new Pen(leftLineColor);
Point[] point = new Point[2];
point[0] = new Point(0, 0);
point[1] = new Point(0, this.Height - 1);
g.DrawPolygon(pen, point);
}
//画右边框
private void DrawRightBorder(PaintEventArgs pe)
{
}
[Description("文本垂直对齐方式"), Category("自定义属性")]
public StringAlignment VerticalAlignment
{
get
{
return verticalAlignment;
}
set
{
verticalAlignment = value;
this.Invalidate();
}
}
[Description("文本水平对齐方式"), Category("自定义属性")]
public StringAlignment HorizontalAlignment
{
get
{
return horizontalAlignment;
}
set
{
horizontalAlignment = value;
this.Invalidate();
}
}
[Description("边框颜色"), Category("自定义属性")]
public Color LineColor
{
get
{
return lineColor;
}
set
{
lineColor = value;
this.Invalidate();
}
}
}
}
阅读全文
0 0
- winform 自定义label
- winform 可拖动的自定义Label控件
- C# winform自定义Label控件使其能设置行距
- C# winform自定义Label控件使其能设置行距
- 自定义Label
- 转 winform label换行问题
- C# Winform 实现透明label
- 自定义控件之:label
- 自定义View--Label + TextField
- coreText自定义文本Label
- 自定义Label跑马灯
- 自定义label高度
- 自定义Label的高度
- Label 的富文本 与 自定义Label
- Label--自定义可调整内边距的Label
- Label--自定义可粘贴复制的Label
- C# WinForm 中Label自动换行 解决方法
- C# WinForm中的Label换行方法
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建
- CSS文本超出2行就隐藏并且显示省略号,单行居中,两行居左
- dataGridView动态加载行号
- 介绍国内外CMS系统
- 如何将mysql命令行的结果输出到文件中
- winform 自定义label
- Leetcode712. Minimum ASCII Delete Sum for Two Strings
- 线程组使用
- 代码笔记
- virtualbox网络配置之NAT,NAT网络,桥接
- ThreadPoolExecutor 线程池Demo
- 融云SDK触达用户数破20亿
- 枚举—案例(完美立方等式poj1543)
- 阿里云CentOS7安装MySQL