こんにちは、私は 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;
}
}
}