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 = ...
随机推荐
- kolla单节点部署openstack
virtualbox环境: 双网卡:enp0s3(桥接) 192.168.102.194 enp0s8(桥接) 无ip 块存储 50G 关闭防火墙,selinux. 配置yum源:wget ...
- Python2和Python3关于reload()用法的区别
Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) >>> from imp >>> imp. ...
- Python的深copy和浅copy
浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象. 浅copy: a = [1, 2, ...
- socat管理haproxy以及haproxy调优
Unix套接字命令(Unix Socket commands) socat是一个多功能的网络工具,名字来由是“Socket CAT”,可以看作是netcat的N倍加强版,socat的官方网站:http ...
- C# 流总结
前言 本篇文章简单总结了在C#编程中经常会用到的一些流.比如说FileStream.MemoryStream. BufferedStream. NetWorkStream. StreamReader/ ...
- 理解OpenShift(4):用户及权限管理
理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...
- 新部署tomcat,An error occurred at line: [1] index_jsp.java
环境: centos6.5 32位 oracle jdk 1.8 tomcat 7 问题: yum install tomcat后,返回如下错误: [root@centos]~# curl -v ht ...
- python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数
函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...
- ---perl 模块安装方法
http://blog.csdn.net/lincy100/article/details/7333794 $ perl -MCPAN -e shell install Log::Log4perlin ...
- lambda group by 的用法
foreach (DynamicObject lstKSItem in lstKSItems) { var entity = lstKSItem["FEntity"] as Dyn ...