Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Print Preview Prints Blank pages....

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
P?ll ?lafsson - 02 Dec 2004 19:02 GMT
Hi

Im trying to print out contents of a panel.  I use the Graphics object to
draw what I want to print and the print preview displays everything OK but
when I print the contents the printer prints out blank page?

here is a little description what this class does; I have a Panel that
consists of other panels.  Each of the inner panels consists of other
controls that I have to print out.  I create an enumerator to print the
panels in reverse order.

here is my code.. most of it I found on the net...
Hopefully someone can help me with this problem.... Im sure this is a minor
problem but I can't figure it out by myself?

public class PrintForm
{

 private Control _ctrl = null;
 private int _iCurrentPageIndex = 0;
 private int _iCurrentControlIndex = 0;
 private int m_DocHeight = 900;
 private int m_DocWidth = 624;
 private int m_iPaperHeight = 0;
 private Control m_CurrCtrl = null;
 private bool m_bHasMorePages = false;
 private ArrayList m_alControls = new ArrayList();
 private IEnumerator ieGrunnBelti = null;
 private int m_iTop = 0;

 public PrintForm(Control ctrl)
 {
  _ctrl = ctrl;
 }

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

 public void Print(Control ctrl)
 {
  if (ctrl == null)
   return;

  ieGrunnBelti = _ctrl.Controls.GetEnumerator();
  this.ControlsToArray(_ctrl);

  PrintDocument pd = new PrintDocument();
  pd.BeginPrint += new PrintEventHandler(this.PrintDocument_BeginPrint);
  pd.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);

  PrintPreviewDialog ppd = new PrintPreviewDialog();
  ppd.Document = pd;
  ppd.ShowDialog();
 }

 private void ControlsToArray(Control p_ctrl)
 {
  while(ieGrunnBelti.MoveNext())
   m_alControls.Add(ieGrunnBelti.Current);

  _iCurrentControlIndex = m_alControls.Count;
 }

 private Point[] GeneratePrintingOffsets(Rectangle rMargins)
 {
   ...
 }

 private void DrawControl(Control ctrl,Point ptOffset, Graphics g, ref
PrintPageEventArgs e)
 {
  m_bHasMorePages = false;
  Control c = null;
  while(m_alControls.Count > 0)
  {
   --_iCurrentControlIndex;
   if(_iCurrentControlIndex < 0)
    break;
   c = (Control)m_alControls[_iCurrentControlIndex];

   Point p = new Point(c.Left, c.Top - m_iTop);
   p.Offset(ptOffset.X, ptOffset.Y);

   if(c is Skyrslubelti.clsGrunnBelti || c is
Skyrslubelti.SkyrslaAreksturBill)
   {
    if((m_iPaperHeight + c.Height) < m_DocHeight)
     m_iPaperHeight += c.Height;
    else
    {
     m_iPaperHeight = 0;
     _iCurrentPageIndex++;
     ++_iCurrentControlIndex;
     m_iTop = c.Top;
     m_bHasMorePages = true;
     break;
    }
    DrawFrame(c, p, g);
    this.DrawSkyrsluBelti(c,p,g);
   }
  }
  if(m_bHasMorePages)
   e.HasMorePages = true;
  else
   e.HasMorePages = false;
 }

 private void DrawSkyrsluBelti(Control p_ctrl, Point ptOffset, Graphics g)
 {

  foreach(Control c in p_ctrl.Controls)
  {
   Point p = new Point(c.Left, c.Top);
   p.Offset(ptOffset.X, ptOffset.Y);

   if (c is Label)
    DrawLabel((Label)c, p, g);
   else if (c is CheckBox)
    DrawCheckBox((CheckBox)c, p, g);
   else if(c is PictureBox)
    DrawImage((PictureBox)c, p, g);
   else if (c is Skyrslubelti.clsSkyrslaTexti)
    DrawLine(c, p, g);
   else if (c is RichTextBox)
    DrawRTB((RichTextBox)c, p, g);
   else if (c is Skyrslubelti.SkyrslaAreksturBill)
    DrawFrame(c, p, g);

   DrawSkyrsluBelti(c, p, g);
  }
 }

 private void DrawImage(PictureBox pb, Point p, Graphics g)
 private void DrawLabel(Label lbl, Point p, Graphics g)
 private void DrawLine(Control c,Point p, Graphics g)
 private void DrawFrame(Control c,Point p, Graphics g)
 private void DrawRTB(RichTextBox p_RTB,Point p, Graphics g)
 private void DrawCheckBox(CheckBox chk, Point p, Graphics g)
 private void PrintDocument_BeginPrint(object sender, PrintEventArgs e)
 {
  this._iCurrentPageIndex = 0;
 }

 private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
 {
  Rectangle rPageMargins = new Rectangle(e.MarginBounds.Location,
e.MarginBounds.Size);

  Point[] ptOffsets = GeneratePrintingOffsets(rPageMargins);

  e.Graphics.SetClip(rPageMargins);

  Point ptOffset = new Point(-ptOffsets[0].X, -ptOffsets[0].Y);
  ptOffset.Offset(rPageMargins.X, rPageMargins.Y);

  DrawControl(_ctrl,ptOffset, e.Graphics, ref e);

 }

}
P?ll ?lafsson - 02 Dec 2004 21:47 GMT
Fixed it?

> Hi
>
[quoted text clipped - 158 lines]
>
>  }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.