brenlex
asked on
Flicker on main form when using PrintDialog
From within my C# app, when I launch a print preview dialog using ShowDialog from a button click on my main form, my main form (behind the preview dialog) flickers whilst the preview job is being built. The print document is built from the contents of a listbox using the code below.
private void buttonPrint_Click(object sender, EventArgs e)
{
System.Drawing.Printing.Pr intDocumen t pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this .pd_PrintP age);
printPreviewDialog1.Docume nt = pd;
printPreviewDialog1.ShowDi alog();
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font = new Font("Courier New", 9);
linesPerPage = ev.MarginBounds.Height / Font.GetHeight(ev.Graphics );
while(count < linesPerPage && nextItem < listBoxResults.Items.Count )
{
yPos = topMargin + (count * Font.GetHeight(ev.Graphics ));
line = listBoxResults.Items[nextI tem].ToStr ing();
ev.Graphics.DrawString(lin e, Font, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
nextItem++;
if (nextItem <= listBoxResults.Items.Count - 1)
ev.HasMorePages = true;
else
{
ev.HasMorePages = false;
nextItem = 0;
break;
}
}
Font = new Font("Microsoft Sans Serif", 8); // Set our font back on main form!
}
How can I stop the flicker on the main form, as it seems the pd_PrintPage event is being fired by the ShowDialog() method itself?
private void buttonPrint_Click(object sender, EventArgs e)
{
System.Drawing.Printing.Pr
pd.PrintPage += new PrintPageEventHandler(this
printPreviewDialog1.Docume
printPreviewDialog1.ShowDi
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font = new Font("Courier New", 9);
linesPerPage = ev.MarginBounds.Height / Font.GetHeight(ev.Graphics
while(count < linesPerPage && nextItem < listBoxResults.Items.Count
{
yPos = topMargin + (count * Font.GetHeight(ev.Graphics
line = listBoxResults.Items[nextI
ev.Graphics.DrawString(lin
count++;
nextItem++;
if (nextItem <= listBoxResults.Items.Count
ev.HasMorePages = true;
else
{
ev.HasMorePages = false;
nextItem = 0;
break;
}
}
Font = new Font("Microsoft Sans Serif", 8); // Set our font back on main form!
}
How can I stop the flicker on the main form, as it seems the pd_PrintPage event is being fired by the ShowDialog() method itself?
ASKER
In which case is there any other way to change the preview / printed font to Courier New? I need this as the print jobs' text columns are not aligned otherwise.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window