Using an Escape to Obtain and Change Paper Size for Printer (96796)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 1.0

This article was previously published under Q96796

SUMMARY

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.

MORE INFORMATION

The example program listed below demonstrates how to use both printer escape constants (ENUMPAPERMETRICS and GETSETPAPERMETRICS) with the Windows API Escape() function.

An Important Note

The Windows API Escape() function is provided in Windows versions 3.0 and 3.1 for backward compatibility with earlier versions of Microsoft Windows. Applications are supposed to use the GDI DeviceCapabilities() and ExtDeviceMode() functions instead of the Escape() function, but neither DeviceCapabilities() nor ExtDeviceMode() can be called directly from Visual Basic. This is because they are exported by the printer driver, not by the Windows GDI. The only way to use ExtDeviceMode() or DeviceCapabilities() in Visual Basic is to create a DLL and call them from there. To execute the ExtDeviceMode() function, you need to obtain a function pointer to it from the current printer driver. Visual Basic does not support pointers.

Steps to Create Example

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
  2. From the File menu, choose New Module (ALT, F, M). Module1 is created by default.
  3. Add the following code to the general declarations section of Module1:
       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
    
    						
  4. Add the following code to the General Declarations section of Form1:

    Dim RectArray() As Rect
  5. Add a command button (Command1) to Form1.
  6. Add a list box (List1) to Form1.
  7. Add the following code to the Command1_Click event procedure. For readability some lines of code are shown as two lines but must be entered as a single line of code.
       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
    
    						
  8. Add the following code to the List1_Click event procedure:
       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
    
    						
  9. From the Run menu, choose Start (ALT, R, S) to run the program.
  10. Choose the Command1 button to display a list of available paper metrics in the List1 box. The paper metrics represent the size of the printable regions supported by the printer, not the physical paper sizes.
  11. Select one of the paper metrics shown in the List1 box. A message box appears indicating whether or not the paper size was successfully changed using the paper metrics you selected.

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:KB96796