PDA

View Full Version : مشکل با PRINT



reza_comp
دوشنبه 30 شهریور 1388, 13:29 عصر
من از کد زیر برای print از یک صفحه که backgroun اون یه richtextbox هست و چند تا textbox روش قرار داره استفاده میکنم:





private void DrawControl(Control ctl, Point ptOffset, Graphics g)
{
foreach (Control c in ctl.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 if (c is RichTextBox)
DrawRichTextBox((RichTextBox)c,p,g);
else
return;
// Draw the controls within this control
DrawControl(c, p, g);
}
}

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-5,
txt.Height-5);
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 DrawRichTextBox(RichTextBox rtxt, Point p, Graphics g)
{
int iBorder =1;
g.FillRectangle(new SolidBrush(rtxt.BackColor),
p.X + iBorder,
p.Y + iBorder,
rtxt.Width +50,
rtxt.Height +50);
g.DrawString(txt.Text,txt.Font, new SolidBrush(txt.ForeColor),p.X +iBorder,p.Y);
}
private void DrawPanel(Panel lbl, Point p, Graphics g)
{
Form2 fr = new Form2();
string pn= fr.panel1.Name;
if (lbl.Name ==pn)
{
g.FillRectangle(new SolidBrush(lbl.BackColor),
p.X,
p.Y,
lbl.Width,
lbl.Height);

// Label's text centered on both axis
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));
}
}


مشکلی که دارم این که نمیدونم چرا در PRINTPREVIEW برای صفحه از بالا و سمت چپ حاشیه(فاصله لبه کاغذ با متن) در نظر میگیره؟ولی از دو طرف دیگه فاصله رو به صفر میرسونه؟
عکس فرم و PRINTPREVIEW رو هم ضمیمه میکنم.

mortezamhd
دوشنبه 30 شهریور 1388, 23:04 عصر
سلام
برنامتو اپ کن شاید بتونم کمکت کنم

reza_comp
سه شنبه 31 شهریور 1388, 07:35 صبح
ممنون از اینکه جواب دادید ولی به خاطر تعهدی که دادم امکان آپ کردن برنامم نیست!

reza_comp
چهارشنبه 01 مهر 1388, 08:24 صبح
کمکم کنید!