bring printdialogbox to front

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EddieT
    New Member
    • Nov 2009
    • 13

    bring printdialogbox to front

    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
  • EddieT
    New Member
    • Nov 2009
    • 13

    #2
    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 timerelapsedeve nt 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.
    Code:
            
    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);
            }

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Did you try setting the .TopMost property to true?

      Comment

      • EddieT
        New Member
        • Nov 2009
        • 13

        #4
        Dialogboxes do not have .TopMost property

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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(IWi n32Window Owner) function overload it will do a better job at popping on top?

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by EddieT
            Dialogboxes do not have .TopMost property
            Some do.
            Code:
            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.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              PrintPreviewDia log has a .Show() and .ShowDialog()
              PrintDialog only has .ShowDialog()

              ShowDialog() is "supposed" to be topmost (of its owner form)

              Comment

              • EddieT
                New Member
                • Nov 2009
                • 13

                #8
                ShowDialog() is "supposed" to be topmost (of its owner form)
                I agree, but i did something abnormal which is having the printdialog.sho wdialog() called from a timerelapsed event(meaning i call the printdialog.sho wdialog() 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.)

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  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(IWi n32Window 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.

                  Code:
                  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
                  }

                  Comment

                  Working...