PDA

View Full Version : سوال: lمشکل با printpreview



sepehr_sepehr
چهارشنبه 25 شهریور 1388, 09:40 صبح
من برای چاپ فرمی که حاوی textbox هست از printpreview و printdocument استفاده میکنم.در واقع کد زیر رو دارم:






private void btnprint_Click(object sender, EventArgs e)
{

try
{
this.Cursor = Cursors.WaitCursor;

PrintForm pf = new PrintForm(this);
pf.Print();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}




که printform یه کلاس که در اون متدهایی مثل drawtextboxوdrawcontrol وجود داره.
مشکلی که دارم اینه که فرم من به انتخاب کاربربه اندازه A4یا A5یا A3 و به شکل افقی یا عمودی (landscape)در میاد. ولی در printpreview اندازه صفحه به طور پیش فرض A4 و به صورت عمودی(vertical) است.
چه طور میتونم بر اساس اندازه فرمی که قراره print گرفته بشه اندازه صفحه printpreview رو تغییر بدم؟؟؟؟

اینم کدهای مربوط به کلاس:::





namespace create_form
{
class PrintForm
{



private Form _frm = null;
private int _iCurrentPageIndex = 0;


public PrintForm()
{
}


public PrintForm(Form frm)
{
_frm = frm;
}



public void Print()
{
Print(_frm);
}

public void Print(Form frm)
{
if (frm == null)
return;

// Setup a PrintDocument
PrintDocument pd = new PrintDocument();

pd.BeginPrint += new PrintEventHandler(this.printDocument_BeginPrint);
pd.PrintPage+=new PrintPageEventHandler(this.printDocument_PrintPage );
// Setup & show the PrintPreviewDialog
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();
}

#endregion

#region Private Methods


private Point[] GeneratePrintingOffsets(Rectangle rMargins)
{
// Setup the array of Points
int x = (int)Math.Ceiling((double)(_frm.Width) / (double)(rMargins.Width));
int y = (int)Math.Ceiling((double)(_frm.Height) / (double)(rMargins.Height));
Point[] arrPoint = new Point[x * y];

// Fill the array
for (int i = 0; i < y; i++)
for (int j = 0; j < x; j++)
arrPoint[i * x + j] = new Point(j * rMargins.Width, i * rMargins.Height);

return arrPoint;
}
private void DrawForm(Graphics g, Point ptOffset)
{
// Calculate the Title Bar rectangle
int iBarHeight = (int)g.MeasureString(_frm.Text, _frm.Font).Height;
Rectangle rTitleBar = new Rectangle(ptOffset.X, ptOffset.Y, _frm.Width, iBarHeight + 2);

// Draw the rest of the Form under the Title Bar
ptOffset.Offset(0, rTitleBar.Height);
DrawControl(_frm, ptOffset, g);
Size s = new Size(16, 14); // size determined from graphics program


private void DrawControl(Control ctl, Point ptOffset, Graphics g)
{
// Cycle through each control on the form and paint it on the graphics object
foreach (Control c in ctl.Controls)
{
// Skip invisible controls
if (!c.Visible)
continue;
Point p = new Point(c.Left, c.Top);
p.Offset(ptOffset.X, ptOffset.Y);


if (c is TextBox)
DrawTextBox((TextBox)c, p, g);

else if (c is Panel)
DrawPanel((Panel)c, p, g);

else
return;

// Draw the controls within this control
DrawControl(c, p, g);
}
}


/// <summary>
private void DrawTextBox(TextBox txt, Point p, Graphics g)
{
int iBorder = 2;


g.FillRectangle(new SolidBrush(txt.BackColor),
p.X + iBorder,
p.Y + iBorder,
txt.Width - 4,
txt.Height - 5);

// TextBox's text left justified & centered vertically
g.DrawString(txt.Text,
txt.Font,
new SolidBrush(txt.ForeColor),
p.X + iBorder,
p.Y + (txt.Height / 2) - (g.MeasureString(txt.Text, txt.Font).Height / 2));
}



private void DrawPanel(Panel lbl, Point p, Graphics g)
{
Form2 fr = new Form2();
string pn= fr.panel1.Name;
if (lbl.Name ==pn)
// Control's BackColor
{
g.FillRectangle(new SolidBrush(lbl.BackColor),
p.X,
p.Y,
lbl.Width,
lbl.Height);


g.DrawString(
lbl.Text,
lbl.Font,
new SolidBrush(lbl.ForeColor),
p.X + (lbl.Width / 2) - (g.MeasureString(lbl.Text, lbl.Font).Width / 2),
p.Y + (lbl.Height / 2) - (g.MeasureString(lbl.Text, lbl.Font).Height / 2));
}
}

#endregion
private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
_iCurrentPageIndex = 0;
}

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{


Rectangle rPageMargins = new Rectangle(e.MarginBounds.Location, e.MarginBounds.Size);

// Generate the offset origins for the printing window
Point[] ptOffsets = GeneratePrintingOffsets(rPageMargins);

// Make sure nothing gets printed in the margins
e.Graphics.SetClip(rPageMargins);

// Draw the rest of the Form using the calculated offsets
Point ptOffset = new Point(-ptOffsets[_iCurrentPageIndex].X, -ptOffsets[_iCurrentPageIndex].Y);
ptOffset.Offset(rPageMargins.X, rPageMargins.Y);
DrawForm(e.Graphics, ptOffset);

// Determine if there are more pages
e.HasMorePages = (_iCurrentPageIndex < ptOffsets.Length - 1);

_iCurrentPageIndex++;
}

}
}

sepehr_sepehr
چهارشنبه 25 شهریور 1388, 20:30 عصر
راه حل مشکلم رو پیدا کردم.اینجا میزارمش شاید یه روز به درد یه نفر بخوره:


PrintDocument pd = new PrintDocument();
pd.BeginPrint += new PrintEventHandler(this.printDocument_BeginPrint);
pd.PrintPage+=new PrintPageEventHandler(this.printDocument_PrintPage );
pd.DefaultPageSettings.PaperSize = new PaperSize("testprint", 827, 1169);
pd.DefaultPageSettings.Landscape=true;

sepehr_sepehr
چهارشنبه 25 شهریور 1388, 23:08 عصر
فقط مشکلی که دارم اینه که وقتی قراره فرم جدید باز بشه من اندازه طول و عرض فرم رو با دستور
f1.width=a,f1.height=b
با مقادیر (A3,A4,A5)
مقدار کذاری میکنم
.حالا وقتی بعد از باز شدن فرم به اندازه دلخواه از دستور
f1.width و f1.height
برای بدست آوردن این مقادیر استفاده میکنم برای من همان مقدار ی که به طور پیش فرض در
properties
فرم قرار داره بر میگردونه!!!1
میشه کمکم کنید.