我怎么能预先测量一个字符串之前打印? [英] How can I pre measure a string before is printed?

查看:165
本文介绍了我怎么能预先测量一个字符串之前打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好,我是学编程用C#VS 2010 EE和我正在申请填​​写预打印形式。这种形式在不同的坐标系好几个地方。纸张上的盒中的三个是多行5宽×2H框中。



我已经创建的窗口与一个文本框的纸张表单上的每个地方。

$形成b
$ b

的事情是,在这些多行文本框输入信息时,我需要知道有多少行被留在纸张输入更多的文字,而且,何时停止打字,因为没有在预印纸中提供更多的空间。



我已经做了很多的搜索,但一切我所发现的是关于屏幕,它不最终结果匹配测量在纸上。



在换句话说,我需要知道如何找出将要在纸张上,而该字符串的尺寸被输入到文本框,并将它与比较预印表格上的可用空间,所以它会在纸张上的框的底部边框才可以停下来。



在纸上第一个框5宽2的高度,开始在新的RectangleF(60.0F,200.0F,560.0F,200.0F)。我理解那些数字是百分之一英寸。



所有这一切,同时在考虑,我不能用的字符数量限制的文本框,因为不是所有的字符占用同一空间,如 H = I;! M =升;!



感谢您提前为您的帮助。
今天,2011年9月5日,根据您的意见和建议,我已经改变了使用Graphics.MeasureString的代码。



这是我现在有代码在Graphics.MeasureString且只有一个RichTextBox中:
是从printDocument1_PrintPage事件可以正常使用,但*我不知道如何使它从richTextBox1_TextChanged事件的工作*

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Drawing.Printing; //所需的PrintDocument()使用System.Linq的
;
使用System.Text;使用System.Windows.Forms的
;

命名打印
{
公共部分Form1类:表格
{
私人字体printFont1;
串strPrintText;

公共Form1中()
{
的InitializeComponent();
}

私人无效cmdPrint_Click(对象发件人,EventArgs五)
{

{
的PrintDocument pdocument =新的PrintDocument();
pdocument.PrintPage + =新PrintPageEventHandler
(this.printDocument1_PrintPage);
pdocument.Print();
}
赶上(异常前)
{
MessageBox.Show(ex.Message);
}
}

公共无效printDocument1_PrintPage(对象发件人,
System.Drawing.Printing.PrintPageEventArgs E)
{
strPrintText = richTextBox1 .Text.ToString();
printFont1 =新的字体(宋体,10); //我不得不删除从btnPrintAnexo1_Click
图形此行G = e.Graphics;
的StringFormat格式1 =新的StringFormat();
的RectangleF rectfText;
INT iCharactersFitted,iLinesFitted;

rectfText =新的RectangleF(60.0F,200.0F,560.0F,200.0F);
//下面e.Graphics.DrawRectangle是
//只与打印预览
e.Graphics.DrawRectangle(新朋(Color.Black,1F),
rectfText debuging .X,rectfText.Y,rectfText.Width,rectfText.Height);

format1.Trimming = StringTrimming.Word; //字包装

//的下一行代码StringFormatFlags.LineLimit被评价如此条件iLinesFitted> 12可以通过在MessageBox
//使用加以考虑下一行代码,如果你不希望显示最后一行,将被截断,在的RectangleF
//format1.FormatFlags = StringFormatFlags.LineLimit;

//不要使用此代码下一行。一些它是如何给我接错linesFilled
//g.MeasureString(strPrintText,字体,rectfFull.Size,
//StringFormat.GenericTypographic,出iCharactersFitted,出iLinesFilled);

//使用这个来代替:
//这是我们得到字符和线条使用
g.MeasureString(strPrintText,printFont1,rectfText.Size,格式1的数量,出iCharactersFitted,出iLinesFitted);

如果(strPrintText.Length == 0)
{
e.Cancel = TRUE;
的回报;
}
如果(iLinesFitted> 12)
{
MessageBox.Show(太多的行richTextBox1.\\\
Please减少文字输入。);
e.Cancel = TRUE;
的回报;
}

g.DrawString(strPrintText,printFont1,Brushes.Black,rectfText,格式1);
g.DrawString(iLinesFilled =线路中的矩形:+ iLinesFitted.ToString(),printFont1,Brushes.Black,
rectfText.X,rectfText.Height + rectfText.Y);
g.DrawString(iCharactersFitted =在矩形大字:+ iCharactersFitted.ToString(),printFont1,Brushes.Black,
rectfText.X,rectfText.Height + rectfText.Y + printFont1.Height) ;
}

私人无效richTextBox1_TextChanged(对象发件人,EventArgs五)
{
// ***,我不知道该怎么在这里输入***。 

如果(iLinesFitted == 13)
{
MessageBox.Show(太多的行richTextBox1.\\\
Please删除一些字符。);
}
}

私人无效cmdPrintPreview_Click(对象发件人,EventArgs五)
{
printPreviewDialog1.ShowDialog();
}

私人无效printDocument1_BeginPrint(对象发件人,System.Drawing.Printing.PrintEventArgs E)
{
// strPrintText = richTextBox1.Text;
}
}
}


解决方案

我认为这是你所追求的。



MeasureString(...)



Juat确保您的图形对象是一个PrintDocument的。

 使用系统; 
使用System.Drawing中;
使用System.Drawing.Printing;使用System.Windows.Forms的
;

命名空间GraphicsHandler
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
}

私人无效textBox1_TextChanged(对象发件人,EventArgs五)
{
//检查文本的宽度时,它被改变。如果
(checkTextWillFit(textBox1.Text)==真)
{
MessageBox.Show(输入的测试太宽,请减少字符数。);
}
}

私人布尔checkTextWillFit(字符串enteredText)
{
//创建一个句柄在PrintPage对象$ B $的图形性能b图形G = pd.PrinterSettings.CreateMeasurementGraphics();
//设置在测量
字体myFont =新字体(Arial字体,12,FontStyle.Regular,GraphicsUnit.Millimeter)要使用的字体;
//使用所选字体
测量字符串的大小//返回true,如果太大
如果(g.MeasureString(enteredText,myFont).WIDTH→100)
{
返回真;
}
,否则
{
返回FALSE;
}
}

的PrintDocument PD = NULL;

私人无效Form1_Load的(对象发件人,EventArgs五)
{
//初始化用来渲染打印的页面
PD =新的PrintDocument()打印documnet;
//创建时,页面打印
pd.PrintPage + =新PrintPageEventHandler(pd_PrintPage)事件处理程序;
}

无效pd_PrintPage(对象发件人,PrintPageEventArgs E)
{
//页打印逻辑这里
}
}
}


Hello, I am learning programming with C# VS 2010 EE and I’m making an application to fill up a preprinted form. This form has several places in different coordinates. Three of the boxes on the paper are multiline 5" W x 2" H boxes.

I already created the windows form with one TextBox for each place on the paper form.

The thing is that when entering the info in these multiline TextBoxes I need to know how many lines are left on the paper to enter more text, and also, when to stop typing because there is no more space available in the PrePrinted box.

I’ve done a lot of search but everything I have found is about measuring on the screen, which does not match the final result on the paper.

In other words, I need to know how to find out what are going to be the string dimensions on the paper while is being typed into the TextBoxes and compare it with the space available on the PrePrinted form so I can stop before it goes over the bottom border of the box on the paper.

The first box on the paper is 5" in width by 2" in height, starting at "new RectangleF(60.0F, 200.0F, 560.0F, 200.0F)". I understand those numbers are hundredths of an inch.

All of this, taking in to account that I can not limit the TextBoxes by quantity of characters because not all characters occupy the same space, like H != I; M != l; etc.

Thank you in advance for your help. Today Sep 05 2011, based on your comments and suggestions I’ve changed the code to use Graphics.MeasureString .

This is the code I have now with the Graphics.MeasureString and only one richTextBox: Is working perfectly from the printDocument1_PrintPage event, *but I have no idea how to make it work from the richTextBox1_TextChanged event*.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;//Needed for the PrintDocument()
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Printing
{
  public partial class Form1 : Form
  {
    private Font printFont1;
    string strPrintText;

    public Form1()
    {
      InitializeComponent();
    }

    private void cmdPrint_Click(object sender, EventArgs e)
    {
      try
      {
        PrintDocument pdocument = new PrintDocument();
        pdocument.PrintPage += new PrintPageEventHandler
        (this.printDocument1_PrintPage);
        pdocument.Print();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    public void printDocument1_PrintPage (object sender,
      System.Drawing.Printing.PrintPageEventArgs e)
    {
      strPrintText = richTextBox1.Text.ToString();
      printFont1 = new Font("Times New Roman", 10); //I had to remove this line from the btnPrintAnexo1_Click
      Graphics g = e.Graphics;
      StringFormat format1 = new StringFormat();
      RectangleF rectfText;
      int iCharactersFitted, iLinesFitted;

      rectfText = new RectangleF(60.0F, 200.0F, 560.0F, 200.0F);
      // The following e.Graphics.DrawRectangle is
      // just for debuging with printpreview
      e.Graphics.DrawRectangle(new Pen(Color.Black, 1F),
        rectfText.X, rectfText.Y, rectfText.Width, rectfText.Height);

      format1.Trimming = StringTrimming.Word; //Word wrapping

      //The next line of code "StringFormatFlags.LineLimit" was commented so the condition "iLinesFitted > 12" could be taken into account by the MessageBox
// Use next line of code if you don't want to show last line, which will be clipped, in rectangleF
      //format1.FormatFlags = StringFormatFlags.LineLimit;

      //Don't use this next line of code. Some how it gave me a wrong linesFilled
      //g.MeasureString(strPrintText, font, rectfFull.Size, 
      //StringFormat.GenericTypographic, out iCharactersFitted, out iLinesFilled);

      //Use this one instead:
      //Here is where we get the quantity of characters and lines used 
      g.MeasureString(strPrintText, printFont1, rectfText.Size, format1, out iCharactersFitted, out iLinesFitted);

      if (strPrintText.Length == 0)
      {
        e.Cancel = true;
        return;
      }
      if (iLinesFitted > 12)
      {
        MessageBox.Show("Too many lines in richTextBox1.\nPlease reduce text entered.");
        e.Cancel = true;
        return;
      }

      g.DrawString(strPrintText, printFont1, Brushes.Black, rectfText, format1);
      g.DrawString("iLinesFilled = Lines in the rectangle: " + iLinesFitted.ToString(), printFont1, Brushes.Black,
        rectfText.X, rectfText.Height + rectfText.Y);
      g.DrawString("iCharactersFitted = Characters in the rectangle: " + iCharactersFitted.ToString(), printFont1, Brushes.Black,
        rectfText.X, rectfText.Height + rectfText.Y + printFont1.Height);
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
      //***I don’t know what to type here.*** 

        if (iLinesFitted == 13)
        {
            MessageBox.Show("Too many lines in richTextBox1.\nPlease erase some characters.");
        }
    }

      private void cmdPrintPreview_Click(object sender, EventArgs e)
    {
      printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
//      strPrintText = richTextBox1.Text;
    }
  }
}

解决方案

I think that this is what you are after.

MeasureString(...)

Juat make sure that your graphics object is a PrintDocument.

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace GraphicsHandler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // Check the width of the text whenever it is changed.
            if (checkTextWillFit(textBox1.Text) == true)
            {
                 MessageBox.Show("Entered test is too wide, please reduce the number of characters.");
            }
        }

        private bool checkTextWillFit(string enteredText)
        {
            // Create a handle to the graphics property of the PrintPage object
            Graphics g = pd.PrinterSettings.CreateMeasurementGraphics();
            // Set up a font to be used in the measurement
            Font myFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Millimeter);
            // Measure the size of the string using the selected font
            // Return true if it is too large
            if (g.MeasureString(enteredText, myFont).Width > 100)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        PrintDocument pd = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Initialise the print documnet used to render the printed page
            pd = new PrintDocument();
            // Create the event handler for when the page is printed
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            // Page printing logic here            
        }
    } 
}

这篇关于我怎么能预先测量一个字符串之前打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆