PDA

View Full Version : سوال: اندازه نوشته بر حسب pt



sohrabi1383
سه شنبه 03 دی 1392, 19:05 عصر
در کد زیر اگر بخواهیم عرض صفحه کاغذ را بر حسب پی تی ببینیم از دستور زیر استفاده می کنیم.
MessageBox.Show(e.PageBounds.Width.ToString());
اگر بخواهیم طول فونت را بر حسب پی تی ببینیم از دستور زیر استفاده می کنیم.
MessageBox.Show(drawFont.Size.ToString());
اگر بخواهیم ببینیم جمله « من به مدرسه رفتم.» چقدر جا در سطر بر حسب پی تی اشغال می کند باید از چه دستوری استفاده کنیم.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace printer2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument objPrintDocument = new PrintDocument();
objPrintDocument.DocumentName = "student";
printDialog1.AllowPrintToFile = false;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.Document = objPrintDocument;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
objPrintDocument.PrintPage += new PrintPageEventHandler(prtPage);
objPrintDocument.Print();
}
}
private void prtPage(object sender, PrintPageEventArgs e)
{
string drawString = "من به مدرسه رفتم.";
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new System.Drawing.Font("Arail", 10);
RectangleF recAtZero = new RectangleF(0, 0, e.PageBounds.Width, e.PageBounds.Height * 2);
MessageBox.Show(e.PageBounds.Width.ToString());
MessageBox.Show(drawFont.Size.ToString());
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero, drawFormat);

RectangleF recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height * 2);
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
}
}
}

abbas.oveissi
سه شنبه 03 دی 1392, 19:26 عصر
این کمکی نمیکنه ، شما جملتون رو به عنوان پارامتر اول بنویسید : using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}