Connecting Tech Pros Worldwide Help | Site Map

bring printdialogbox to front

Newbie
 
Join Date: Nov 2009
Posts: 13
#1: 2 Weeks Ago
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
#2: 2 Weeks Ago

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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void printToolStripMenuItem_Click(object sender, EventArgs e)
  3.         {
  4.             PrintDialog PrintDialog1 = new PrintDialog();
  5.             PrintDialog1.Document = printDoc;
  6.             if (PrintDialog1.ShowDialog() == DialogResult.OK)
  7.             {
  8.                 System.Timers.Timer timer = new System.Timers.Timer(100);
  9.                 timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_Print);
  10.                 timer.Start();
  11.             }
  12.  
  13.         }
  14.  
  15.         private void timer_Elapsed_Print(object sender, ElapsedEventArgs e)
  16.         {
  17.             System.Timers.Timer tm = (System.Timers.Timer)sender;
  18.             tm.Stop();
  19.             tm.Dispose();
  20.             Graphics g1 = this.CreateGraphics();
  21.             Image MyImage = new Bitmap(this.image.Width, this.image.Height, g1);
  22.             Graphics g2 = Graphics.FromImage(MyImage);
  23.             IntPtr dc1 = g1.GetHdc();
  24.             IntPtr dc2 = g2.GetHdc();
  25.             BitBlt(dc2, 0, 0, this.image.Width, this.image.Height, dc1, 0, 46, 13369376);
  26.             g1.ReleaseHdc(dc1);
  27.             g2.ReleaseHdc(dc2);
  28.             MyImage.Save(@"c:\PrintPage.wmf", ImageFormat.Wmf);
  29.             FileStream fileStream = new FileStream(@"c:\PrintPage.wmf", FileMode.Open, FileAccess.Read);
  30.             this.streamToPrint = fileStream;
  31.             this.streamType = "Image";
  32.             printDoc.Print();
  33.             fileStream.Close();
  34.         }
  35.  
  36.  
  37.         private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  38.         {
  39.             System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
  40.             int width = image.Width;
  41.             int height = image.Height;
  42.  
  43.             if (((double)width / (double)(e.PageBounds.Width - 40)) > ((double)height / (double)e.PageBounds.Height))
  44.             {
  45.                 width = (e.PageBounds.Width - 40);
  46.                 height = Convert.ToInt16((double)height * (double)(e.PageBounds.Width - 40) / (double)image.Width);
  47.             }
  48.             else
  49.             {
  50.                 height = e.PageBounds.Height;
  51.                 width = Convert.ToInt16((double)width * (double)e.PageBounds.Height / (double)image.Height);
  52.             }
  53.             System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);
  54.             e.Graphics.DrawImage(image, destRect, 0, 0, Convert.ToInt16(image.Width),
  55.                 Convert.ToInt16(image.Height), System.Drawing.GraphicsUnit.Pixel);
  56.         }
  57.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#3: 2 Weeks Ago

re: bring printdialogbox to front


Did you try setting the .TopMost property to true?
Newbie
 
Join Date: Nov 2009
Posts: 13
#4: 2 Weeks Ago

re: bring printdialogbox to front


Dialogboxes do not have .TopMost property
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#5: 2 Weeks Ago

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?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#6: 2 Weeks Ago

re: bring printdialogbox to front


Quote:

Originally Posted by EddieT

Dialogboxes do not have .TopMost property

Some do.
Expand|Select|Wrap|Line Numbers
  1. PrintPreviewDialog myppd = new PrintPreviewDialog();
  2. myppd.TopMost = true;
  3.  
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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#7: 2 Weeks Ago

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
#8: 2 Weeks Ago

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.)
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#9: 2 Weeks Ago

re: bring printdialogbox to front


Quote:

Originally Posted by Plater View Post

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.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3.     this.TopMost = true; // so that our dialog will be on top and seen.
  4.     System.Windows.Forms.DialogResult result = MessageBox.Show(this,"Are you sure", "Close?", 
  5.                                                                MessageBoxButtons.YesNo,
  6.                                                                MessageBoxIcon.Question);
  7.     if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
  8. }
Reply

Tags
bringtofront, printdialog