c#: Label控件加入AutoHeight属性
2024-09-10 16:14:21
此功能在界面布局中颇为实用,录代码以记之:
public class LabelEx : Label
{
private bool autoHeight = true; [DefaultValue(true)]
public bool AutoHeight
{
get { return this.autoHeight; }
set
{
this.autoHeight = value;
RecalcHeight();
}
} protected override void OnAutoSizeChanged(EventArgs e)
{
base.OnAutoSizeChanged(e);
RecalcHeight();
} protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
RecalcHeight();
} protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
RecalcHeight();
} protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
RecalcHeight();
} private void RecalcHeight()
{
if (this.AutoSize || !this.autoHeight)
return; var size = TextRenderer.MeasureText(this.Text, this.Font);
int lc = (int)Math.Ceiling(size.Width * 1.0 / this.ClientSize.Width);
this.Height = lc * size.Height;
}
}
顺手,给TextBox加个水印文字(PlaceHolderText)功能:
public class TextBoxEx : TextBox
{
private string placeHolderText; [DefaultValue("")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string PlaceHolderText
{
get { return this.placeHolderText; }
set
{
this.placeHolderText = value;
PaintPlaceHolderText();
}
} protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//若使OnPaint()起作用,须设置this.SetStyle(ControlStyles.UserPaint,true);,但这样控件显示都得重绘,所以直截WM_PAINT消息
//WM_PAINT消息:0xf WM_CTLCOLOREDIT:0x133
if (m.Msg == 0xf || m.Msg == 0x133)
PaintPlaceHolderText();
} private void PaintPlaceHolderText()
{
if (this.Focused || string.IsNullOrEmpty(this.placeHolderText) || !string.IsNullOrEmpty(this.Text))
return; var g = Graphics.FromHwnd(this.Handle);
var bounds = this.ClientRectangle;
g.FillRectangle(new SolidBrush(this.BackColor), bounds);
var sf = new StringFormat();
switch (this.TextAlign)
{
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
bounds.Offset(-, );
g.DrawString(this.placeHolderText, this.Font, new SolidBrush(Color.DarkGray), bounds, sf);
}
}
参考资料:
Winform-TextBox实现 placeholder - zhishiheng的专栏 - CSDN博客
最新文章
- using语法糖详解 2015-01-06 17:45 50人阅读 评论(0) 收藏
- ldconfig deferred processing now taking place
- 二模 (1) day2
- c# winform快捷键设置
- EasyBCD 2.2中文版安装变色龙wowpc.iso详细教程(适用各个版本)
- aix 扩展文件系统
- 水晶報表中小寫變大寫的函數-VB
- log4j日志重定向
- java 集合框架(十)List
- 配置Tomcat线程参数maxThreads、acceptCount
- 推荐一款好用的任务定时器:Quartz
- UICollectionView使用相关博文链接
- CentOS系统/tmp目录里面的文件默认保留多久
- vue加elementui开发的分页显示
- Go语言学习笔记(五) [函数]
- 在SQL中查询某列具有相同值的数据
- 用kettle从mysql中使用存储过程读取数据写入到sqlserver数据库
- 请求&;注解
- stm32f103串口实现映射功能
- [源码] 定义String s=";abcd";, 求长度
热门文章
- 如何避免提交页面,信息未填写完善 就出现注册成功提示 基于js
- Linux服务安装配置总结
- 【js字符串当做数组来使用】浪费一晚【想出了3个解决方案】
- MySQL Execution Plan--NOT IN查询
- np金融量化分析
- c语言fork 多进程
- AttributeError: 'module' object has no attribute 'main'
- jsp中forward与redirect
- 数据库SQL语言学习----左外连接,右外连接,外连接,自然连接的形象对比
- [转]Java事件处理机制- 事件监听器的四种实现方式