Remit to write a writing board with VB.NET (4)

zhaozj2021-02-11  239

All source code is downloaded here: http://www.up2e.com/resource.php

Remit to the Word Package Report (Final) with VB.NET --- By Zigz (Luhai)lulai@eastday.com

5) About print preview

It is very simple to start, but finally, the preview is always unable to preview the actual file, and ultimately get relevant information on the Microsoft site, and use him to use him to this application, and it is very successful, you can successfully preview. In order to be afraid of misleading others, it is printed in the original text.

Below is the effect of two images to demonstrate print previews. Figure 5 Text in RichtextBox Figure 6 Printing the text in the preview

Print preview related code:

(Note! The following information about printing is from Microsoft Technical Documents)

'Must determine that all print events are for the same printDocument

Private Withevents Pdoc as new printDocument ()

'Print file is a functional print event, and the event is triggered whenever you want to print.

'The following is a very fast and useful precision calculation to print if the text to be printed can be included to the entire print page

'It is the information I got from Microsoft Site, I applied it to my program.

Private sub pdoc_printpage (Byval e as system.drawing.printing.printpageeventargs) Handles PDOC.Printpage

'Declare a variable to hold the position of the lastprinted char. Declare

'as static so this subsequent PrintPage Events Can Reference It.

Static Intcurrentchar as Int32

'INITIALIZE The font to be buy for printing.

DIM FONT AS New Font ("Microsoft Sans Serif", 24)

Dim Intprintareaheight, intprintareawidth, marginleft, margintop as int32

With pdoc.defaultpagesettings

'Initialize Local Variables That Contain the Bounds of the Printing

'Area Rectangle.

INTPRINTAREAHEIGHT = .papersize.height - .Margins.top - .Margins.bottom

INTPRINTAREAWIDTH = .papersize.width - .MARGINS.LEFT - .MARGINS.RIGHT

'Initialize Local Variables to Hold Margin Values ​​That Will Serve

'as the x and y coordinates for the Upper Left corner of the printing

'Area Rectangle.

Marginleft = .MARGINS.LEFT 'X CORINATE

Margintop = .MARGINS.TOP 'Y COordinate

End with

'If the user selected landscape mode, swap the printing area height' and width.

If pdoc.defaultpagesettings.landscape kil

DIM INTTEMP AS INT32

INTTEMP = INTPRINTAREAHEIGHT

INTPRINTAREAHEIGHT = INTPRINTAREAWIDTH

INTPRINTAREAWIDTH = INTTEMP

END IF

'Calculate The Total Number of Lines in The Document Based on the Height of

'The Printing Area and The Height of the Font.

DIM INTLINECUNT AS INT32 = CINT (intprintareaheight / font.height)

'Initialize The Rectangle Structure That Defines The Printing Area.

Dim RectPrintingarea As New Rectanglef (Marginleft, Margintop, INTPRINTAREAWIDTH, INTPRINTAREAHET)

'Instantiate The StringFormat Class, Which Encapsulates Text Layout

'Information (Such As Alignment and Line Spacing, Display Manipulation

'(Such As EllipsIns INSERTION AND National Digit Substitution) and OpenType

'Features. Use of stringformat causes measurestrurestring and drawstring to use

'Only An Integer Number of Lines When Printing Each Page, Ignoring Partial

'Lines That Would Ootherwise LIKELY BE Printed If The Number of Lines Per

'Page Do Not Divide Up Cleanly for Each Page (Which is Usually The Case).

'See Further Discussion in The SDK Documentation About StringFormatFlags.

DIM FMT AS New StringFormat (StringFormatflags.LineLimit)

'Call MeasureString to Determine The Numr of Characters That Will Fit in

'The Printing Area Rectangle. The Charfitted Int32 IS Passed Byref and Used

'Later When Calculating INTCURRENTCHAR AND THUS HASMOREPAGES. LINESFILED

'Is Not Needed for this Sample But Must Be Passed When Passing Charsfitted.

'Mid IS Used to Pass the segment of remaining text left off from the

'Previous Page of Printing (Recall That Intcurrentchar Was Declared As

'static.

DIM INTLINESFILED, INTCHARSFITTED AS INT32

E.Graphics.MeasureString (MID (RTBOX.TEXT, INTCURRENTCHAR 1), FONT, _

New sizef (intprintareaheight), fmt, _

INTCHARSFITTED, INTLINESFILED

'Print the text to the page.

E.Graphics.drawstring (MID (RTBOX.TEXT, INTCURRENTCHAR 1), FONT, _

Brushes.black, recrintingarea, fmt)

'Advance The Current Char To The Last Char Printed On this page. As

'Intcurrentchar is a static variable, its value can be used for the next

'page to be printed. it is advanced by 1 and passed to mid () to print the

'Next Above In MeasureString ()).

INTCURRENTCHAR = INTCHARSFITTED

'Hasmorepages Tells The Printing Module WHether Another PrintPage Event

'SHOULD BE FIRED.

IF Intcurrentchar

e.hasmorepages = true

Else

E.hasmorepages = false

'You Must Explicitly Reset Intcurrentchar as it is static.

INTCURRENTCHAR = 0

END IF

End Sub

Private subprintpreview ()

DIM PPD As New PrintPreviewDialog ()

Try

PPD.Document = PDOC

ppd.showdialog ()

Catch Exp As Exception

Messagebox.show ("There is a mistake !! Can't preview!" & _

"Confident that you can now" "

"Connect to a printer?" & _

"Then preview can be.", Me.text, _

MessageboxButtons.ok, MessageBoxicon.Error

END TRY

End Sub

Private sub mprintpreview_click (Byval e as system.EventArgs) Handles MPRINTPREVIEW.CLICK

PRINTPREVIEW ()

End Sub

Other status hints, font dialogs, color dialogs, etc. of the status bar, please refer to the source code.

to sum up:

Overall, this program reaches 85% of the Windows Word board. It is a pity that there is no effect of the ruler, and some ideas is to use drag control code, set two controls, and is symmetrical. It is specified that these two controls can only be dragged on a horizontal line. According to the movement of the two controls, determine the space size of the text before and after the text of the text in RichTextBox, the rough ideas are like this. Full text.

By Zigz (Luhai)

Luluhai@eastday.com

转载请注明原文地址:https://www.9cbs.com/read-3754.html

New Post(0)