6

これは紙の形のスケッチです こんにちは、私は C# VS 2010 EE でプログラミングを学んでおり、事前に印刷されたフォームに記入するためのアプリケーションを作成しています。このフォームには、異なる座標にいくつかの場所があります。用紙上の 3 つのボックスは、幅 5 インチ x 高さ 2 インチのマルチライン ボックスです。

TextBox紙のフォームの各場所に1 つずつ Windows フォームを作成しました。

問題は、これらの複数行の TextBoxes に情報を入力するとき、さらにテキストを入力するために紙に何行残っているか、また、PrePrinted ボックスに使用できるスペースがなくなったためにいつ入力を停止するかを知る必要があるということです。

私は多くの検索を行いましたが、私が見つけたのはすべて画面上で測定することであり、紙の最終結果と一致しません.

言い換えれば、テキストボックスに入力されている間に用紙上の文字列の寸法がどのようになるかを調べ、それを PrePrinted フォームで利用可能なスペースと比較して、それが超えてしまう前に停止できるようにする方法を知る必要があります。用紙上のボックスの下枠。

用紙の最初のボックスは、幅 5 インチ、高さ 2 インチで、「<code>new RectangleF(60.0F, 200.0F, 560.0F, 200.0F)」で始まります。それらの数字が 100 分の 1 インチであることは理解しています。

すべての文字が同じスペースを占めるわけではないため、TextBox を文字数で制限できないことを考慮して、これらすべてを行いますH != I; M != l;

よろしくお願いいたします。今日 2011 年 9 月 5 日、あなたのコメントと提案に基づいて、 Graphics.MeasureString を使用するようにコードを変更しました。

これは、Graphics.MeasureString と 1 つだけの richTextBox を使用したコードです。printDocument1_PrintPage イベントから完全に機能していますが、richTextBox1_TextChanged イベントから機能させる方法がわかりません

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;
    }
  }
}
4

3 に答える 3

5

これがあなたが求めているものだと思います。

MeasureString(...)

グラフィックス オブジェクトが 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            
        }
    } 
}
于 2011-08-14T05:37:57.810 に答える
0

私が正しければ、 FormattedText というクラスを使用して、印刷する前にフォーマットすることができます。その後、FormatedText の width プロパティを確認できます。

ここに印刷に関する優れたチュートリアルがあります: http://www.switchonthecode.com/tutorials/printing-in-wpf。2番目の部分は、ページネーションに焦点を当てています(これはあなたが扱っていると思います):http://www.switchonthecode.com/tutorials/wpf-printing-part-2-pagination。FormattedText がここに表示されます。

于 2011-08-14T04:46:28.097 に答える