bring printdialogbox to front | Newbie | | Join Date: Nov 2009
Posts: 13
| | |
Hi,
I have this problem of the printdialogbox hiding behind my main form. The very first time i call the printdialogbox it comes to the top but subsequent calls always appear behind the main form.
How do you bring the printdialogbox to the front everytime it is called?
Thanks,
ET
| | Newbie | | Join Date: Nov 2009
Posts: 13
| | | re: bring printdialogbox to front
Just solved the problem i face which is kind of an isolated problem. anyway here is what happened.
The problem is due to the calling of the printdialogbox within a timerelapsed event - meaning when I hit the print button, the printdialogbox does not immediately launch. i needed a delay of 0.1seconds and so i added a timerelpased event of 0.1s. After 0.1s i start to call the printdialogbox within the timerelapsed routine. Because the printdialogbox is nested in a timerelapsed routine, the printdialogbox doesnt want to appear in front of my form.
My solution then was to call the printdialogbox immediately after the print button is hit but not the print action. I then start the 0.1seconds timer. After 0.1second elapses, the timerelapsedevent is called for which then only I call the printdoc.print to initiate the printing. Doing this avoids the printdialogbox being called inside the timerelapsed routine and this seems to have got rid of the problem.
here is my code for anyone who faced this problem. -
-
private void printToolStripMenuItem_Click(object sender, EventArgs e)
-
{
-
PrintDialog PrintDialog1 = new PrintDialog();
-
PrintDialog1.Document = printDoc;
-
if (PrintDialog1.ShowDialog() == DialogResult.OK)
-
{
-
System.Timers.Timer timer = new System.Timers.Timer(100);
-
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_Print);
-
timer.Start();
-
}
-
-
}
-
-
private void timer_Elapsed_Print(object sender, ElapsedEventArgs e)
-
{
-
System.Timers.Timer tm = (System.Timers.Timer)sender;
-
tm.Stop();
-
tm.Dispose();
-
Graphics g1 = this.CreateGraphics();
-
Image MyImage = new Bitmap(this.image.Width, this.image.Height, g1);
-
Graphics g2 = Graphics.FromImage(MyImage);
-
IntPtr dc1 = g1.GetHdc();
-
IntPtr dc2 = g2.GetHdc();
-
BitBlt(dc2, 0, 0, this.image.Width, this.image.Height, dc1, 0, 46, 13369376);
-
g1.ReleaseHdc(dc1);
-
g2.ReleaseHdc(dc2);
-
MyImage.Save(@"c:\PrintPage.wmf", ImageFormat.Wmf);
-
FileStream fileStream = new FileStream(@"c:\PrintPage.wmf", FileMode.Open, FileAccess.Read);
-
this.streamToPrint = fileStream;
-
this.streamType = "Image";
-
printDoc.Print();
-
fileStream.Close();
-
}
-
-
-
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
-
{
-
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
-
int width = image.Width;
-
int height = image.Height;
-
-
if (((double)width / (double)(e.PageBounds.Width - 40)) > ((double)height / (double)e.PageBounds.Height))
-
{
-
width = (e.PageBounds.Width - 40);
-
height = Convert.ToInt16((double)height * (double)(e.PageBounds.Width - 40) / (double)image.Width);
-
}
-
else
-
{
-
height = e.PageBounds.Height;
-
width = Convert.ToInt16((double)width * (double)e.PageBounds.Height / (double)image.Height);
-
}
-
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);
-
e.Graphics.DrawImage(image, destRect, 0, 0, Convert.ToInt16(image.Width),
-
Convert.ToInt16(image.Height), System.Drawing.GraphicsUnit.Pixel);
-
}
-
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | re: bring printdialogbox to front
Did you try setting the .TopMost property to true?
| | Newbie | | Join Date: Nov 2009
Posts: 13
| | | re: bring printdialogbox to front
Dialogboxes do not have .TopMost property
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: bring printdialogbox to front
If you are not allowing the user to change anything in the printdialog, why bother showing it at all?
Also, I think if you specify the owner form in the .ShowDialog(IWin32Window Owner) function overload it will do a better job at popping on top?
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | re: bring printdialogbox to front Quote:
Originally Posted by EddieT Dialogboxes do not have .TopMost property Some do. - PrintPreviewDialog myppd = new PrintPreviewDialog();
-
myppd.TopMost = true;
-
I tend to do print previews in my app so I have gotten used to using it. I didn't realize that the PrintDialog doesn't have this property. Weird really when you think about it.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: bring printdialogbox to front
PrintPreviewDialog has a .Show() and .ShowDialog()
PrintDialog only has .ShowDialog()
ShowDialog() is "supposed" to be topmost (of its owner form)
| | Newbie | | Join Date: Nov 2009
Posts: 13
| | | re: bring printdialogbox to front Quote:
ShowDialog() is "supposed" to be topmost (of its owner form)
I agree, but i did something abnormal which is having the printdialog.showdialog() called from a timerelapsed event(meaning i call the printdialog.showdialog() some delay later). I need to do this because i am 'sreencapturing' my picture box image and without the delay i will see the print menu being part of my 'screen capture'. The delay just provides enough time for the print menu to disaapear before 'screen capture'. This is a cheap way of printing images on the picture box. Until i have time to do it properly i will keep it this way.
This leads to another question:
Can you have any Form placed on Top of the Task Manager without manually changing the Options-->AlwaysOnTop setting to False in the Task Manager menu?
Or better still is it possible to use Csharp to change the Options-->AlwaysOnTop to False from my program?
(Task Manager are usually set by default to be OnTop of all Windows.)
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | re: bring printdialogbox to front Quote:
Originally Posted by Plater If you are not allowing the user to change anything in the printdialog, why bother showing it at all?
Also, I think if you specify the owner form in the .ShowDialog(IWin32Window Owner) function overload it will do a better job at popping on top? Great tip! I tried this with a different dialog type (a MessageBox) that would occassionally be stuck behind some other application and therefore not be seen.
I found that combining this along with raising my application to TopMost makes sure that the MessageBox gets the user's attention. - private void Form1_FormClosing(object sender, FormClosingEventArgs e)
-
{
-
this.TopMost = true; // so that our dialog will be on top and seen.
-
System.Windows.Forms.DialogResult result = MessageBox.Show(this,"Are you sure", "Close?",
-
MessageBoxButtons.YesNo,
-
MessageBoxIcon.Question);
-
if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
-
}
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|