如何解决两个不同图形之间的度量字符串计算?

时间:2017-02-16 16:40:44

标签: c# winforms

我从PaintEventArgs获取图形并通过MeasureString方法测量字符串。

Public class CustomControl: Control
{

    protected override void OnPaint(PaintEventArgs pe)
{
    Graphics g= pe.Graphics;
    SizeF size = g.MeasureString("Print a character and preview",font)
}

}
Output: {Width = 150.813126 Height = 14.6523409}
Height: 14.6523409
IsEmpty: false
Width: 150.813126

所以我可以在文本矩形width = 154,Height = 18)

中设置上面的文本

如果我使用了PrintDocument的上述场景,它提供了不同的解决方案

public class CustomPrintDocument : PrintDocument
{
    protected override void OnPrintPage(PrintPageEventArgs e)
    {
Graphics g= e.Graphics;
    SizeF size = g.MeasureString("Print a character and preview",font)

}
}
Output: {Width = 157.093277 Height = 15.2628555}
Height: 15.2628555
IsEmpty: false
Width: 157.093277

在上面的场景中,我无法在CustomPrintDocument中绘制矩形内的全文,并且无法从CustomControl获取图形,因为预览文档时CustomControl不处于活动状态。

我的查询我需要在CustomPrintDocument中绘制文本,如CustomControl

截图 section 3 'Source File Structure'

注意: 我不想更改文本的字体以适应CustomPrintDocument

请建议我解决这个问题。

由于

0 个答案:

没有答案