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 = ...
随机推荐
- 使用miniconda创建python虚拟环境
安装python指定环境 conda create -n oldboy python=3.6.5 安装环境的同时安装相应的包 conda create -n oldboy python=3.6.5 p ...
- DataTable与DataSet之间的转换Class
using System;using System.Collections.Generic;using System.Data;using System.Linq; namespace Convert ...
- 第一节《Git初始化》
创建版本库以及第一次提交 首先我看查看一下git的版本,本地的git是用的yum安装方式,如果想使用源码安装请参考官方文档. [root@git ~]# git --versiongit versio ...
- H3C交换机限制子网之间的相互访问
acl number 3000 rule 1 permit ip source 10.0.5.0 0.0.0.255 destination 172.16.1.100 0 #允许10.0. ...
- R语言中的字符串处理函数
内容概览 尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串有时候也会在数据分析中占到相当大的份量. R语言是一个擅长处理数据的语言,但是也不可避免的需要处理一些字符串(文本数据).如何高 ...
- MySQL:System.Data.Entity ,MySqlCommand, MySqlParameter and "LIKE" %
Introduction Using GetSqlStringCommand with a text comparative, with LIKE, in ADO.NET and the MySQLP ...
- Xtrabackup的安装与使用
Xtrabackup的安装与使用 1. XtraBackup 简介 XtraBackup(PXB) 工具是 Percona 公司用 perl 语言开发的一个用于 MySQL 数据库物理热备的备份工具, ...
- WIN7 WIN10赋予文件或者文件夹完全访问权限
WIN7 WIN10赋予文件或者文件夹完全访问权限win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:添加everyone ...
- eclipse开发Java web工程时,jsp第一行报错,如何解决?
与myeclipse不同,eclipse开发java web项目时是要下载第三方软件(服务器)的,正是这个原因,很多初学者用eclipse学习java web的时候,总是会遇到一些小问题.其中常见的一 ...
- POJ1569 Myacm Triangles
Description There has been considerable archeological work on the ancient Myacm culture. Many artifa ...