|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionOne of the most common questions is "how can I print / print preview my form"? This is quite easily done by getting the form to "draw itself" on the printed page but this is often unsatisfactory because:
The attached component addresses the problem in a different manner.... What you do is drop a FormPrintHelperComponent component onto your form an it extends each control with a bunch of new properties (look in the properties window for each control in the section named "MCL Form Print Helper").
You then set these to indicate:
The component then allows you to preview the printed page at design time and to preview or print it at run time How it worksThe component implements The class is decorated with
When the component is invoked to print or print preview, it iterates through all the controls on the form that have the extended property The
"Print as image" printing functionalityIf the control doesn't have an Image attribute the control is drawn onto a bitmap which is then printed on the form by using the control class' Private Function GetControlImage(ByVal ctlIn As Control) As Image
Dim Imageproperty As System.Reflection.PropertyInfo
Imageproperty = ctlIn.GetType.GetProperty("Image", GetType(Image))
If Imageproperty Is Nothing Then
Dim imgNew As New Bitmap(ctlIn.Width, ctlIn.Height)
ctlIn.DrawToBitmap(imgNew, ctlIn.ClientRectangle)
Return imgNew
Else
Return CType(Imageproperty.GetValue(ctlIn, Nothing), Image)
End If
End Function
"Owner drawn" printing functionalityIf you set the extended property Private Sub FormPrintHelperComponent1_OwnerDrawnControlPrint(ByVal sender As Object, _
ByVal e As MCL_Form_Print_Control.OwnerPrintControlEventArgs) _
Handles FormPrintHelperComponent1.OwnerDrawnControlPrint
'\\ The application code should draw this
'\\ control because it is special....
With e
If .ControlToPrint.Name = "DateTimePicker1" Then
e.Canvas.FillRectangle(Brushes.Aqua, e.BoundingRectangle)
e.Canvas.DrawString(">> " & .ControlToPrint.Text & "<<", _
.ControlToPrint.Font, Brushes.Azure, e.BoundingRectangle)
End If
End With
End Sub
Worked exampleFirst add a reference to the control's dll to your application and (optionally) add it to the toolbox. The next step is to layout your windows form in design mode with all the controls that you want on it. (You can add controls at a later date and the extender will pick them up so don't worry about having the full design in place to start with). You then drag an instance of the component from the tool bar on to your form. Then select the control(s) you want to print - e.g. on the form in the demo project in the source code above the controls Label_Databasename, CheckedListBox_Databases etc. are to be printed but the user does not want to print the buttons Button1 and Button2. On the control properties pane for the controls to be printed navigate down to the section "MCL Form print Utility" and set the extended property named "Print on FormPrintHelperComponent1" to true. Setting the rich text box mode to PrintAsRichText means the content of a rich text box is printed in what-you-see-is-what-you-get mode Selecting where to print the component on the pageBy default the property "BoundingRectangle on FormPrinthelperComponent1" is set to the same rectangle as the control occupies on the form. You can alter this (to make the layout more suited to the size and orientation of the printed page) by altering the values in this property. To see how this change looks you can preview the form print layout by selecting the FormPrintHelperComponent instance and from its smart tag menu select the option "Preview Document". Other extender provided properties:The component provides the following extended properties to the form on which it is sited:
The component provides the following extended properties to all the controls on the form on which the component is sited:
Multi-page documentsOf course there are many cases where a single form needs to be printed over multiple logical pages - for example if a form has many tabs on it you might find it sensible to print one logical page per tab The component implements this functionality by having a property
If the MultiPageprintMethod is set to Logical pages?When the data in a control (such as a multi-line textbox, a rich textbox or a grid) is too large for the area allocated to print it on the document you can optionally set it to flow over to a new page until it reaches the end of the data by setting the extended property Updates:
|
||||||||||||||||||||||