C#/WPF 计算字串的真实长度,调整控件的宽度
下面函数是经常用到的计算字串长度的方法:
{
double width = -1;
try
{
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(m_hWnd);
graphics.PageUnit = System.Drawing.GraphicsUnit.Point;
System.Drawing.Font font = new System.Drawing.Font(fontName, (float)fontsize);
System.Drawing.SizeF size = graphics.MeasureString(str, font);
width = size.width;
font.Dispose();
graphics.Dispose();
}
catch (System.Exception e)
{
}
return width;
}
但是事实上,Graphics.MeasureString并不可靠,在不同的SKU下得到的结果会不同,原因不明,可能跟显卡驱动也会有关系。今天尝试新的方法经测试有效,贡献出来:
{
FormattedText formattedText = new FormattedText(
text,
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily.ToString()),
fontSize,
Brushes.Black
);
return formattedText.WidthIncludingTrailingWhitespace;
}
另,附送枚举已安装字体的方法:
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
fontFamilies = installedFontCollection.Families;
int count = fontFamilies.Length;
log("fontFamilies.Length=" + fontFamilies.Length);
for (int i = 0; i < count; i++)
{
fontName = fontFamilies[i].Name;
log("fontName: " + fontName);
}
C#/WPF 计算字串的真实长度,调整控件的宽度的更多相关文章
- python-Levenshtein几个计算字串相似度的函数解析
linux环境下,没有首先安装python_Levenshtein,用法如下: 重点介绍几个该包中的几个计算字串相似度的几个函数实现. 1. Levenshtein.hamming(str1, str ...
- WPF教程十一:简单了解并使用控件模板
WPF教程十一:简单了解并使用控件模板 这一章梳理控件模板,每个WPF控件都设计成无外观的,但是行为设计上是不允许改变的,比如使用Button的控件时,按钮提供了能被点击的内容,那么自由的改变控件外观 ...
- WPF中, 启用添加到RichTextBox中的控件
原文:WPF中, 启用添加到RichTextBox中的控件 WPF中, 启用添加到RichTextBox中的控件 ...
- WPF编程,通过Double Animation动态旋转控件的一种方法。
原文:WPF编程,通过Double Animation动态旋转控件的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/art ...
- WPF编程,通过Double Animation动态更改控件属性的一种方法。
原文:WPF编程,通过Double Animation动态更改控件属性的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/a ...
- WPF编程,通过Double Animation动态缩放控件的一种方法。
原文:WPF编程,通过Double Animation动态缩放控件的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/art ...
- WPF 自定义ComboBox样式,自定义多选控件
原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...
- WPF中动态加载XAML中的控件
原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...
随机推荐
- 【47.95%】【codeforces 554C】Kyoya and Colored Balls
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(5)有趣的指针
一.指针是C语言的灵魂 # include <stdio.h> int main(){ int *p; //p是变量名,int *表示p变量存放的是int类型变量的地址,p是一个指针变量 ...
- Matlab矩阵基础
一.创建矩阵 1.通过直接赋值创建矩阵 将矩阵元素置于[]中.同行元素之间用空格或"."分开.行与行之间用":"隔开. >> a=[1,2,3;4, ...
- PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations
BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention relates generally to h ...
- [Angular] @ContentChild and ngAfterContentInit
@ContentChild normally works with ngAfterContentInit lifecycle. @ContentChild is used for looking in ...
- Watchdog问题实例分析
1.日志获取 Watchdog相关的问题甚至需要以下所有的日志: logcat 通过adb logcat命令输出Android的一些当前运行日志,可以通过logcat的 -b 参数指定要输出的日志缓冲 ...
- 第二十一篇:基于WDM模型的AVStream驱动架构研究
基于WDM模型的AVStream驱动架构研 这篇论文2006年早就发表, 与当时开发这个驱动正好几乎相同的时间. 近期实际项目须要, 又回过头来将AVStre ...
- 【9705】&&【a801】细胞
Time Limit: 10 second Memory Limit: 2 MB 问题描述 一矩形阵列由数字1~9代表细胞,细胞的定义是沿细胞数字上下左右如果还是细胞数字则为同一细胞,求给定矩形阵列的 ...
- RStudio 的使用
0. 基本 路径查看和设置: getwd() ⇒ 查看路径 setwd() ⇒ 设置路径: 1. 快捷键 ctrl+shift+c:多行注释:选中待注释的代码段,按下 ctrl+shift+c(c 表 ...
- vue webpack添加jQuery
---恢复内容开始--- 在webpack.prod.conf.js文件中,找到plugins new webpack.ProvidePlugin({ $: "jquery", j ...