c#中如何得到字符的象素宽度?知道的麻烦指点一下!

freeman102414 2006-10-19 09:09:01
比如 ,一个20号的汉字 ,我想知道他的宽度,就是象素宽度,是多少乘多少的 ?

以A4纸为例,纸的宽度可以得到,如果向纸上打印字,我要他到一定宽度的时候换行,要怎么做?
...全文
287 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
freeman102414 2006-10-19
  • 打赏
  • 举报
回复
谢谢各位,方法很好用,问题以解决,不知道问题解决后要怎么做,我第一次在这里发问题,不太清楚流程!
rickjelly2004 2006-10-19
  • 打赏
  • 举报
回复
<SCRIPT language="javascript">
function test(obj)
{var range = obj.createTextRange();
alert("内容区宽度: " + range.boundingWidth
+ "px\r\n内容区高度: " + range.boundingHeight + "px");

}
</SCRIPT>
<BODY>
<Textarea id="txt" height="150">sdf</textarea><INPUT type="button" value="计算内容宽度" onClick="test(txt)">
</BODY>
xiehuanxie 2006-10-19
  • 打赏
  • 举报
回复
Graphic 的 MeasureString 方法
或者用API

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

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private IntPtr hdc;

public Form1()
{
InitializeComponent();
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public int lfHeight = 0;
public int lfWidth = 0;
public int lfEscapement = 0;
public int lfOrientation = 0;
public int lfWeight = 0;
public byte lfItalic = 0;
public byte lfUnderline = 0;
public byte lfStrikeOut = 0;
public byte lfCharSet = 0;
public byte lfOutPrecision = 0;
public byte lfClipPrecision = 0;
public byte lfQuality = 0;
public byte lfPitchAndFamily = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName = string.Empty;
}

[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr CreateFontIndirect(
[In, MarshalAs(UnmanagedType.LPStruct)]
LOGFONT lplf // characteristics
);

[DllImport("gdi32.dll")]
public static extern bool DeleteObject(
IntPtr handle
);

[DllImport("gdi32.dll")]
static extern bool GetTextExtentPoint(IntPtr hdc, string lpString,
int cbString, ref Size lpSize);

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}

[DllImport("gdi32.dll", EntryPoint = "SelectObject", SetLastError = true)]
private static extern IntPtr SelectObjectCE(IntPtr hdc, IntPtr hgdiobj);

private void button1_Click(object sender, EventArgs e)
{
LOGFONT lf = new LOGFONT();
//Font f = Font.FromLogFont(lf);

Graphics g = CreateGraphics();

this.Font.ToLogFont(lf, g);
hdc = g.GetHdc();


IntPtr handle = CreateFontIndirect(lf);
SelectObjectCE(hdc, handle);
Size MeasureSize = new Size(0, 0);
String str = "s";
int length = str.Length;
GetTextExtentPoint(hdc, str, length, ref MeasureSize);

MessageBox.Show(MeasureSize.Width.ToString());
g.ReleaseHdc(hdc);
MessageBox.Show(g.MeasureString(str, this.Font).Width.ToString());
}
}
}
freeman102414 2006-10-19
  • 打赏
  • 举报
回复
比如打印预览时候,开始不是要输入坐标吗,如果想这行字到达某个坐标点以后就换行了,要怎么做?谢谢1
OfMusic 2006-10-19
  • 打赏
  • 举报
回复
不懂
up

110,545

社区成员

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

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

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