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 = ...
随机推荐
- 【工具】switchhost
1.前提 主要功能切换host 2.下载路径 https://oldj.github.io/SwitchHosts/ 3.使用略(太简单)
- 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...
- java_免费视频课程汇总
xml使用场景 各种配置文件 用于用户界面的开发 传输数据:ajax 这个可能过时,因为程序员更喜欢将xml用json来代替 web service:这些老式的web serv ...
- 我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore
大家好 , 我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 , 名字叫 HtmlCore . 项目的一个主要目标是可以用 .Net 在 移动设备 上 开发 GUI 程序 (界面程 ...
- VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql 1.参考以前博文,恢复在 Vs2010+linQ for Mysql的环境. 2.建立 wpf工程,参照1,生成 datacontext.cs , ...
- SQL特殊comment语法
SQL 注释的特殊用法: /*!版本号 语句*/ 表示大于等于某个版本是,才执行相应的语句. 在版本为5.7.23的MySQL上做测试如下: 测试1 mysql> select 1 /*!507 ...
- oidc User.Identity.Name 为空解决方法
public override Task TicketReceived(TicketReceivedContext context) { var result = base.TicketReceive ...
- debian删除i386的包
sudo apt-get remove --purge `dpkg --get-selections | grep i386 | awk '{print $1}'`; sudo dpkg --remo ...
- 【Python】hasattr() getattr() setattr() 使用方法详解
本文转自 https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回B ...
- 使用Java监控工具出现 Can't attach to the process
问题重现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ➜ jinfo -flags 3032 Attaching ...