c#: Label控件加入AutoHeight属性
此功能在界面布局中颇为实用,录代码以记之:
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博客
c#: Label控件加入AutoHeight属性的更多相关文章
- C# 如何复制(拷贝)Label控件上的文本【新方法】
Label控件在目前是无法直接调用成员函数来复制其文本内容.其实网络上有很多热心程序员网民解答过这个问题,百度上也可以搜索到,不过大多数人建议使用 TextBox 并把边框调整为不可见(运行时文本框看 ...
- WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果 本以为Label也有TextChanged 事件,但在使 ...
- IOS - 控件的AutoresizingMask属性
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum { UIViewAutoresi ...
- C#winform中使用控件的Dock属性进行布局
想要实现如下布局,可以通过设置控件的Dock属性达到效果 1.拖放一个panel控件一个label控件(放在panel中)和一个treeView控件到TestForm中 2.设置panel的Dock属 ...
- duilib进阶教程 -- Label控件的bug (8)
上个教程说到了TreeView的文字不能垂直居中的问题,而我们用LabelUI其实是可以垂直居中的,为什么不说是TreeView的bug,而说是Label控件的bug呢?因为影响TreeView垂直居 ...
- TextBox控件的DataBindings属性
DataBindings属性是很多控件都有的属性,作用有2方面.一方面是用于与数据库的数据进行绑定,进行数据显示.另一方面用于与控件或类的对象进行数据绑定.这里主要关注后者.主要用法是将某个对象的某个 ...
- C#控件方法及属性大全,望补充
C#控件及常用设计整理 1.窗体 常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体. (2) WindowState属性: 用来获取或设置窗体的窗口状态 ...
- Label控件
文本控件包含标签控件(label).按钮控件(button).文本框控件(textBox)和有格式文本控件(richtextBox) Label控件可以说是最简单的控件,是System.windo ...
- asp.net中label控件设置字体大小
//后台修改label控件字体 protected void Button1_Click(object sender, EventArgs e) { this.Label1.Font.Size = ...
随机推荐
- 学习笔记TF047:PlayGround、TensorBoard
PlayGround.http://playground.tensorflow.org .教学目的简单神经网络在线演示.实验图形化平台.可视化神经网络训练过程.在浏览器训练神经网络.界面,数据(DAT ...
- linux服务器架设--学习笔记
PS: Centos是属于红帽子的操作系统
- HttpWebRequest post 请求超时问题
在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步, 发送一个请求, 包含一个Expect:100-continue, ...
- Linux之ls
命令功能: ls是list的简写,列出目录下的内容 命令格式: ls [OPTION]... [FILE]... 命令参数: -a,--all 不忽略以“.”开头的隐藏文件 -A, --almo ...
- 彻底卸载Windows 10中OneDrive
批处理来源网上. @rem OneDrive Complete uninstaller batch process for Windows 10. @rem Run as administrator ...
- 自己动手开发智能聊天机器人完全指南(附python完整源码)
一.前言 人工智能时代,开发一款自己的智能问答机器人,一方面提升自己的AI能力,另一方面作为转型AI的实战练习.在此把学习过程记录下来,算是自己的笔记. 二.正文 2.1 下载pyaiml 下载pya ...
- Lepus监控之SQLServer配置(后续整理)
1.安装pymssql模块 1-1.环境准备: 1-1-1.unixODBC安装 yum install unixODBC unixODBC-devel -y 1-1-2.freetds安装下载 fr ...
- atop 分析小记
atop分析小记 atop这个工具相当NB 项目中需要用到它的磁盘使用率统计值,为了一探究竟,挖了下它的代码 atopsar atopsar实际就是atop的一个链接指向. 从atop.c的main源 ...
- 统计uint64的数对应二进制数的1的个数
// pc[i] is the populatio count of ivar pc [256]byte //统计出o~255每个数对应二进制上1的个数func init() { for i ...
- 使用fdisk进行分区
fdisk进行分区 1.首先使用fdisk -l 发现待分区磁盘/dev/vdb 大小为1TB 2.fdisk /dev/vdb 对该磁盘进行分区,输入m并回车 3.输入n并回车,n是“new”新建 ...