知道了字体、字号、文字,C#有没有可能精确知道这串字符的高度和宽度?

Ki1381 2008-11-18 09:40:11
如题,谢谢!
...全文
271 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ki1381 2008-11-18
  • 打赏
  • 举报
回复
谢谢,果然都是夜猫子,哈哈

其实这个问题的背景是这样的:

想做个自动生成按钮图片的小工具,其中有个选项是根据输入的文字经过一个简单算法后自动产生相应宽度的按钮图片,所以感觉最好还是交给GDI+解决。

各位的代码我研究下......
RexZheng 2008-11-18
  • 打赏
  • 举报
回复

使用 Graphics.MeasureString 方法

以下示例在winform中的用法:


using (Graphics g = this.CreateGraphics())
{
SizeF size = g.MeasureString("测试文本", new Font("宋体", 12));
}
郭军 2008-11-18
  • 打赏
  • 举报
回复
使用下面的方法

private void Form1_Paint(object sender, PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);

// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);

// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));

}

hornbills 2008-11-18
  • 打赏
  • 举报
回复
通过RichTextBox控件的特性来实现!!
hornbills 2008-11-18
  • 打赏
  • 举报
回复
研究下面的程序就能解决您的问题

下面例程是在richtextbox前面加入行号

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NumberedTextBox
{
public partial class NumberedTextBoxUC : UserControl
{

public NumberedTextBoxUC()
{
InitializeComponent();
numberLabel.Font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size + 1.019f);
}

private void updateNumberLabel()
{
//得到第一个字符和第一行
Point pos = new Point(0, 0);
int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);

//得到最后一个字符和最后一行
pos.X = ClientRectangle.Width;
pos.Y = ClientRectangle.Height;
int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);

//这是最后一个字符的位置,我们通过Y值计算字符长度
pos = richTextBox1.GetPositionFromCharIndex(lastIndex);


//在每一行前面加行号
numberLabel.Text = "";
for (int i = firstLine; i <= lastLine + 1; i++)
{
numberLabel.Text += i + 1 + "\n";
}

}


private void richTextBox1_TextChanged(object sender, EventArgs e)
{
updateNumberLabel();
}

private void richTextBox1_VScroll(object sender, EventArgs e)
{
int d = richTextBox1.GetPositionFromCharIndex(0).Y % (richTextBox1.Font.Height + 1);
numberLabel.Location = new Point(0, d);

updateNumberLabel();
}

private void richTextBox1_Resize(object sender, EventArgs e)
{
richTextBox1_VScroll(null, null);
}

private void richTextBox1_FontChanged(object sender, EventArgs e)
{
updateNumberLabel();
richTextBox1_VScroll(null, null);
}
}
}

pp_shy 2008-11-18
  • 打赏
  • 举报
回复
可以通过Graphics.MeasureString来获取
Ki1381 2008-11-18
  • 打赏
  • 举报
回复
谢谢楼上各位,我试下
huoxudong125 2008-11-18
  • 打赏
  • 举报
回复
可以的,
字母的话就是字体的大小变为像素即可,而汉字的大小则为字体的1.5倍,这个我做过实验,现在在一个画图软件里使用了。
这个问题当时我也比较困惑,希望对你有帮助
duping9626 2008-11-18
  • 打赏
  • 举报
回复

//是静态方法
static Size DataGridViewCell.MeasureTextSize(Graphics graphics,
string text,
Font font,
TextFormatFlags flags
)
happychou 2008-11-18
  • 打赏
  • 举报
回复
感觉应该可以吧
是个好问题
没法下载,到这里折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Ro

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧