|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis printing utility is a combination of two efforts. It has the ability to print the content of user controls and it is, moreover, an attempt to create an "all-in-one" printing utility. The latter is an integration into a single control of what is otherwise spread across several standard dialogs for printer properties, page setup options and print previewing. BackgroundGrateful use is made of the following article here on CodeProject, authored by Nader Elshehabi: A component that prints any control... I needed to print the content of a treeview, amongst other things, and the above solution proved to do a fine job. I did not extend very much on it, but instead wrapped it with a printing utility that integrates the facilities of the Another incentive for this control is the variety of printing dialogs out there. If you look at the printing tools supplied with the purchase of an average printer, their appearance, non-standardisation and staggering complexity are, exceptions aside, overwhelming, intimidating and fundamentally scary. They definitely do not help people to understand their printers. This is not an approach with a juicy UI-appeal, but that was not intended. I'm somewhat devoted to the power of PropertyGrid leveraged by the Using the codeThe solution contains two projects:
The printing utility project contains several classes, which will be commented on below.
The control checks whether the Print Spooler service is running and, if so, also checks whether there is a printer installed. It wires the various elements of the control together and contains the methods that do the actual work, notably The The
private void ShowPreview()
{
Cursor = Cursors.WaitCursor;
//rig up a new PrintPreviewControl
NewPrintPreviewControl();
//set several properties of the PrintDocument
_printDocument.DocumentName = _printDocumentSettings.HeaderText;
_printDocument.DefaultPageSettings = _printDocumentSettings.PageSettings;
_printDocument.PrinterSettings = _printDocumentSettings.PrinterSettings;
_printDocument.OriginAtMargins = _printDocumentSettings.OriginAtMargins;
//create a PreviewPrintController
PreviewPrintController pc = new PreviewPrintController();
pc.UseAntiAlias = true;
//assign the PreviewPrintController to the PrintDocument
_printDocument.PrintController = pc;
//assign the document to the PrintPreviewControl
_printPreviewControl.Document = _printDocument;
_printPreviewControl.StartPage = 0;
_printPreviewControl.UseAntiAlias = true;
//set page counter
_pageCounter = 1;
//set initial values for the preview toolstrip
SetPreviewToolStrip();
Cursor = Cursors.Default;
}
The private void PrintNow()
{
DialogResult result;
if (_printDocumentSettings.PrintToFile)
{
result =
MessageBox.Show(
"The document " + _printDocument.DocumentName +
" will be saved to file " +
_printDocumentSettings.FileName +
". Continue?",
"Print to file...",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2,
0);
if (result == DialogResult.Yes)
{
PrintToFile();
return;
}
}
result =
MessageBox.Show(
"The document " + _printDocument.DocumentName +
" from page " + _printDocumentSettings.FromPage +
" to page " + _printDocumentSettings.ToPage +
" will be sent to printer " +
_printDocumentSettings.SelectedPrinter +
". Continue?",
"Print to printer...",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2,
0);
if (result == DialogResult.Yes)
{
_printDocument.PrinterSettings =
_printDocumentSettings.PrinterSettings;
_printDocumentSettings.PageSettings.PrinterSettings =
_printDocument.PrinterSettings;
_printDocument.DefaultPageSettings =
_printDocumentSettings.PageSettings;
StandardPrintController spc = new StandardPrintController();
_printDocument.PrintController =
new PrintControllerWithStatusDialog(spc,
"Printing "+_printDocument.DocumentName + " to " +
_printDocumentSettings.SelectedPrinter);
_printDocument.Print();
}
}
The attributes used are mostly standard, but they do provide added value for the PropertyGrid used. I will not address the application of type converters here in-depth. As an example, the [Category(catPrinter)]
[TypeConverter(typeof(PrinterSelectionConverter))]
[RefreshProperties(RefreshProperties.All)]
[Description("Choice for the selected printer.")]
[DisplayName("Selected printer")]
public string SelectedPrinter
{
get
{
return _selectedPrinter;
}
set
{
_selectedPrinter = value;
_printerSettings.PrinterName = value;
}
}
Another example is the [Category(catPaper)]
[DefaultValue("A4")]
[Description("Paper size kind for selected printer.")]
[DisplayName("Paper size kind")]
[TypeConverter(typeof(PaperKindSelectionConverter))]
public string PaperKind
{
get
{
return ValidatePaperSize(_paperKind).ToString();
}
set
{
if (value != null)
{
_paperKind =
(PaperKind) Enum.Parse(typeof (PaperKind),
value.ToString());
_pageSettings.PaperSize = SetPaperSize(_paperKind);
}
}
}
The document settings further provide several options for printing header and footer info along with the control content. Text options, page numbering and a datetime can be included. Alignment, font and color preferences can also be set.
Points of interestThe user controls covered currently in the Printing the content of specific controls might require some addition to the My experience in using the Known issues and improvement points
History
|
||||||||||||||||||||||