Print Preview Control used in a custom Print Preview Dialog
I have successfully implemented printing and print preview for my app using the PrintDocument, PrintDialog and PrintPreviewDialog classes of .NET.
However my app uses a toolkit to improve the appearance of the standard .NET controls. There are versions of most .NET controls in the toolkit, but none for the Print controls.
Therefore to ensure that the appearance of these controls matches the rest of the app I am creating a custom PrintPreviewDialog based on a toolkit form and embedding a .NET PrintPrewviewControl in it.
My problem is that the PrintPreviewControl always shows "No pages to display". I had no trouble getting this to work using the .NET PrintPreviewDialog and cannot see what I am doing wrong.
This is a .NET 2.0 PrintPreviewControl and so I know that I need to call InvalidatePreview() after assigning the PrintDocument. However it does not seem to matter where i place it, the Pri开发者_运维问答ntPage event handler never gets called...
public class PrintEngine
{
...rest of class...
public PrintEngine()
{
m_printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
}
public void PrintPreview()
{
//ORIGINAL CODE USING .NET DIALOG WORK OK
//PrintPreviewDialog dlg1 = new PrintPreviewDialog();
//dlg1.Document = m_printDoc;
//PrepareImageForPrinting();
//dlg1.ShowDialog();
//CODE USING MY CUSTOM DIALOG DO NOT WORK?
MyPrintPreviewDialog dlg2 = new MyPrintPreviewDialog();
dlg2.Document = m_printDoc;
PrepareImageForPrinting(); //Creates the m_printImage List
dlg2.ShowDialog();
}
private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(m_printImages[m_currentPage], new Point(0, 0));
m_currentPage++;
e.HasMorePages = m_currentPage < m_pagesHigh;
}
}//end PrintEngine class
public class MyPrintPreviewDialog : KryptonForm
{
public PrintDocument Document
{
get { return m_printPreviewControl.Document; }
set
{
m_printPreviewControl.Document = value;
m_printPreviewControl.InvalidatePreview();
}
}
public MyPrintPreviewDialog()
{
InitializeComponent();
m_printPreviewControl = new PrintPreviewControl();
m_printPreviewControl.StartPage = 0;
}
private void MyPrintPreviewDialog_Load(object sender, EventArgs e)
{
m_printPreviewControl.Document.DefaultPageSettings = new PageSettings();
m_printPreviewControl.Document.PrinterSettings = new PrinterSettings();
m_printPreviewControl.InvalidatePreview();
}
}//end MyPrintPreviewDialog class
I'm a bit puzzled by this line:
m_printPreviewControl = new PrintPreviewControl();
You don't seem to attach or position this control.
If you placed it with the Designer, this line should be removed, and you should use the name of the designtime control everywhere..
If you want to create it manually, you need atr least something like this.Controls.Add(m_printPreviewControl );
精彩评论