Using an Escape to Obtain and Change Paper Size for PrinterID: Q96796
|
By using the Windows API Escape() function, an application can change the
paper size on the printer and obtain a list of available paper metrics for
the default printer.
To get the list of available paper metrics, pass the ENUMPAPERMETRICS
printer escape constant to the Escape() function. The function will return
either an array containing the paper metrics or the number of paper metrics
available.
NOTE: Paper metrics differ from the physical paper sizes in that paper
metrics delineate the actual region that can be printed to, whereas paper
size is the physical size of the paper including the non-printable regions.
To change the paper size, pass the GETSETPAPERMETRICS printer escape
constant along with the paper metrics to the Escape() function.
The example program listed below demonstrates how to use both printer
escape constants (ENUMPAPERMETRICS and GETSETPAPERMETRICS) with the
Windows API Escape() function.
Type Rect
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
' Enter each Declare as one, single line.
Declare Function EnumPaperMetricsEscape% Lib "GDI" Alias "Escape"
(ByVal hDC%, ByVal nEscape%, ByVal IntegerSize%, lpMode%,
lpOutData As Rect)
Declare Function SetPaperMetricsEscape% Lib "GDI" Alias "Escape"
(ByVal hDC%, ByVal nEscape%, ByVal RectSize%, NewPaper As Rect,
PrevPaper As Rect)
Declare Function GetDeviceCaps% Lib "gdi" (ByVal hDC%, ByVal nIndex%)
Global Const ENUMPAPERMETRICS = 34
Global Const GETSETPAPERMETRICS = 35
Global Const LOGPIXELSX = 88 ' Logical pixels/inch in X
Global Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Sub Command1_Click ()
ReDim RectArray(1)
mode% = 0
' Enter the entire Result% statement as one, single line.
Result% = EnumPaperMetricsEscape(Printer.hDC, ENUMPAPERMETRICS,
2, mode%, RectArray(0))
If Result% = 0 Then ' If Result = 0, the call failed
MsgBox "Printer Driver does not Support EnumPaperMetrics", 48
Command1.Enabled = False
Exit Sub
End If
ReDim RectArray(Result% - 1) ' Result% contains num paper sizes
mode% = 1
' Enter the entire Result2% statement as one, single line.
Result2% = EnumPaperMetricsEscape(Printer.hDC, ENUMPAPERMETRICS,
2, mode%, RectArray(0))
HorzRatio% = GetDeviceCaps(Printer.hDC, LOGPIXELSX)
VertRatio% = GetDeviceCaps(Printer.hDC, LOGPIXELSY)
' Add Paper Sizes (Listed by actual printing region) in inches
' to the list box. Enter each of the PWidth$ and PHeight$ statements
' as one, single line.
For i% = 0 To Result% - 1
PWidth$ = Format$((RectArray(i%).Right - RectArray(i%).Left)
/ HorzRatio%) + Chr$(34) ' Enter as a single line
PHeight$ = Format$((RectArray(i%).Bottom - RectArray(i%).Top)
/ VertRatio%) + Chr$(34) ' Enter as a single line
List1.AddItem PWidth$ + " X " + PHeight$
Next i%
End Sub
Sub List1_Click ()
Dim PrevPaperSize As Rect
' Enter the entire Result% statement as one, single line.
Result% = SetPaperMetricsEscape(Printer.hDC, GETSETPAPERMETRICS,
Len(PrevPaperSize), RectArray(List1.ListIndex), PrevPaperSize)
If Result% = 0 Then
MsgBox "Printer Driver does not support this Escape.", 48
ElseIf Result% < 0 Then
MsgBox "Error in calling Escape with GETSETPAPERMETRICS."
Else
MsgBox "Paper size successfully changed!"
End If
End Sub
Additional query words: 2.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 14, 1999