PrintDialog : no way to set the title?

  • Thread starter Thread starter Uri Dor
  • Start date Start date
U

Uri Dor

Hi,
I'm developing a C# app which allows printing reports. sometimes one
print dialog will initiate a big bunch of reports (hundreds).
I want to give the user some idea of how many reports he's printing now.
I looked for ways to set the title of the PrintDialog, but none
appeared. I even tried CreateParams, but PrintDialog is a sealed class...

any ideas? maybe a 3rd party control? anything?
I'd even settle for a way to know where the PrintDialog appears, so I
can show it modelessly and show another modeless dialog next to it...
 
Uri,

You can set the title of the PrintDialog by handling the WM_ACTIVATE message
that is sent by the dialog to your form. To do this you need to write your
own WndProc, but it's not complicated.

Since we're going to call a Windows API function, start by adding this line
at the top of your program:

using System.Runtime.InteropServices;

Then add these lines just after the declaration of your class:

[DllImport("User32.Dll")]
public static extern void SetWindowText(int h, String s);
public const int WM_ACTIVATE = 6;

You also want to declare a couple of private variables:

private bool setPrintDialogTitle = false;
private string printDialogTitle = "";

Let's say you have a button to start printing, so this is what you have to
change:

private void button1_Click(object sender, System.EventArgs e)
{
// initialize the document to print (your code here)
printDialog1.Document = new System.Drawing.Printing.PrintDocument();

// set title and enable flag
printDialogTitle = "Print [1 document]";
setPrintDialogTitle = true;

// display print dialog
printDialog1.ShowDialog(this);
}

Finally write a custom WndProc to change the window title:

protected override void WndProc(ref Message m)
{
// check for flag and message
if(setPrintDialogTitle && (m.Msg == WM_ACTIVATE))
{
// disable flag
setPrintDialogTitle = false;

// change window title
SetWindowText((int)m.LParam, printDialogTitle);
}

// default processing
base.WndProc(ref m);
}

Regards,

Gabriele
 
Back
Top