|
|
|
Here is the alternate method which shows how to use printer APIs to set custom papersize and other printer properties using API. This code will work with Win9x and WinNT both. |
Click here to copy the following block | Option Explicit
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type
Private Declare Function GetVersionExA Lib "kernel32" _ (lpVersionInformation As OSVERSIONINFO) As Integer
Private Const sCustomFormTitle As String = "TechnoLogismiki's Custom Form"
Private Const NULLPTR = 0&
Private Const CCHDEVICENAME = 32 Private Const CCHFORMNAME = 32
Private Const DM_IN_BUFFER = 8 Private Const DM_OUT_BUFFER = 2 Private Const DM_IN_PROMPT = 4
Private Const DM_ORIENTATION = &H1& Private Const DMORIENT_PORTRAIT = 1 Private Const DMORIENT_LANDSCAPE = 2
Private Const DM_PRINTQUALITY = &H400& Private Const DMRES_DRAFT = (-1) Private Const DMRES_HIGH = (-4) Private Const DMRES_LOW = (-2) Private Const DMRES_MEDIUM = (-3)
Private Const DM_TTOPTION = &H4000& Private Const DMTT_BITMAP = 1 Private Const DMTT_DOWNLOAD = 2 Private Const DMTT_DOWNLOAD_OUTLINE = 4 Private Const DMTT_SUBDEV = 3
Private Const DM_COLOR = &H800& Private Const DMCOLOR_COLOR = 2 Private Const DMCOLOR_MONOCHROME = 1
Private Const DM_COLLATE As Long = &H8000 Private Const DMCOLLATE_FALSE = 0 Private Const DMCOLLATE_TRUE = 1
Private Const DM_DUPLEX = &H1000& Private Const DMDUP_HORIZONTAL = 3 Private Const DMDUP_SIMPLEX = 1 Private Const DMDUP_VERTICAL = 2
Private Const DM_PAPERSIZE = &H2& Private Const DM_PAPERWIDTH = &H8& Private Const DM_PAPERLENGTH = &H4& Private Const DMPAPER_10X11 = 45 Private Const DMPAPER_10X14 = 16 Private Const DMPAPER_11X17 = 17 Private Const DMPAPER_15X11 = 46 Private Const DMPAPER_9X11 = 44 Private Const DMPAPER_A_PLUS = 57 Private Const DMPAPER_A2 = 66 Private Const DMPAPER_A3 = 8 Private Const DMPAPER_A3_EXTRA = 63 Private Const DMPAPER_A3_EXTRA_TRANSVERSE = 68 Private Const DMPAPER_A3_TRANSVERSE = 67 Private Const DMPAPER_A4 = 9 Private Const DMPAPER_A4_EXTRA = 53 Private Const DMPAPER_A4_PLUS = 60 Private Const DMPAPER_A4_TRANSVERSE = 55 Private Const DMPAPER_A4SMALL = 10 Private Const DMPAPER_A5 = 11 Private Const DMPAPER_A5_EXTRA = 64 Private Const DMPAPER_A5_TRANSVERSE = 61 Private Const DMPAPER_B_PLUS = 58 Private Const DMPAPER_B4 = 12 Private Const DMPAPER_B5 = 13 Private Const DMPAPER_B5_EXTRA = 65 Private Const DMPAPER_B5_TRANSVERSE = 62 Private Const DMPAPER_CSHEET = 24 Private Const DMPAPER_DSHEET = 25 Private Const DMPAPER_ENV_10 = 20 Private Const DMPAPER_ENV_11 = 21 Private Const DMPAPER_ENV_12 = 22 Private Const DMPAPER_ENV_14 = 23 Private Const DMPAPER_ENV_9 = 19 Private Const DMPAPER_ENV_B4 = 33 Private Const DMPAPER_ENV_B6 = 35 Private Const DMPAPER_ENV_C3 = 29 Private Const DMPAPER_ENV_C4 = 30 Private Const DMPAPER_ENV_C5 = 28 Private Const DMPAPER_ENV_C6 = 31 Private Const DMPAPER_ENV_C65 = 32 Private Const DMPAPER_ENV_DL = 27 Private Const DMPAPER_ENV_INVITE = 47 Private Const DMPAPER_ENV_ITALY = 36 Private Const DMPAPER_ENV_MONARCH = 37 Private Const DMPAPER_ENV_PERSONAL = 38 Private Const DMPAPER_ESHEET = 26 Private Const DMPAPER_EXECUTIVE = 7 Private Const DMPAPER_FANFOLD_LGL_GERMAN = 41 Private Const DMPAPER_FANFOLD_STD_GERMAN = 40 Private Const DMPAPER_FANFOLD_US = 39 Private Const DMPAPER_FOLIO = 14 Private Const DMPAPER_ISO_B4 = 42 Private Const DMPAPER_JAPANESE_POSTCARD = 43 Private Const DMPAPER_LAST = DMPAPER_FANFOLD_LGL_GERMAN Private Const DMPAPER_LEDGER = 4 Private Const DMPAPER_LEGAL = 5 Private Const DMPAPER_LEGAL_EXTRA = 51 Private Const DMPAPER_LETTER = 1 Private Const DMPAPER_FIRST = DMPAPER_LETTER Private Const DMPAPER_LETTER_EXTRA = 50 Private Const DMPAPER_LETTER_EXTRA_TRANSVERSE = 56 Private Const DMPAPER_LETTER_PLUS = 59 Private Const DMPAPER_LETTER_TRANSVERSE = 54 Private Const DMPAPER_LETTERSMALL = 2 Private Const DMPAPER_NOTE = 18 Private Const DMPAPER_QUARTO = 15 Private Const DMPAPER_RESERVED_48 = 48 Private Const DMPAPER_RESERVED_49 = 49 Private Const DMPAPER_STATEMENT = 6 Private Const DMPAPER_TABLOID = 3 Private Const DMPAPER_TABLOID_EXTRA = 52 Private Const DMPAPER_USER = 256
Private Const DM_COPIES = &H100& Private Const DM_SCALE = &H10& Private Const DM_FORMNAME As Long = &H10000
Private Type DEVMODE dmDeviceName(1 To CCHDEVICENAME) As Byte dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * CCHFORMNAME dmUnusedPadding As Integer dmBitsPerPel As Integer dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type
Private Type PRINTER_INFO_5 pPrinterName As String pPortName As String Attributes As Long DeviceNotSelectedTimeout As Long TransmissionRetryTimeout As Long End Type
Private Declare Function GetProfileString Lib "kernel32" _ Alias "GetProfileStringA" (ByVal lpAppName As String, _ ByVal lpKeyName As String, ByVal lpDefault As String, _ ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Declare Function WriteProfileString Lib "kernel32" _ Alias "WriteProfileStringA" (ByVal lpszSection As String, _ ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ lParam As String) As Long
Private Const HWND_BROADCAST = &HFFFF Private Const WM_WININICHANGE = &H1A
Private Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _ (ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _ ByVal cbBuf As Long, ByRef pcbNeeded As Long, _ ByRef pcReturned As Long) As Long
Private Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _ (ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As Long
Private Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _ (ByVal hPrinter As Long, ByVal pFormName As String) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _ "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _ ByVal pDefault As Long) As Long
Private Declare Function DocumentProperties Lib "winspool.drv" _ Alias "DocumentPropertiesA" (ByVal hwnd As Long, _ ByVal hPrinter As Long, ByVal pDeviceName As String, _ pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _ As Long
Private Declare Function ClosePrinter Lib "winspool.drv" _ (ByVal hPrinter As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _ (ByVal lpDriverName As String, ByVal lpDeviceName As String, _ ByVal lpOutput As Long, ByVal lpInitData As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" _ (ByVal hdc As Long) As Long
Private Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _ (ByVal hdc As Long, lpInitData As Any) As Long
Private Declare Function DeleteObject Lib "gdi32" _ (ByVal hObject As Long) As Long
Private Declare Function LstrCpy Lib "kernel32" Alias "lstrcpyA" _ (ByVal lpString1 As String, ByRef lpString2 As Long) As Long
Private Type PRINTER_DEFAULTS pDatatype As Long pDevMode As Long pDesiredAccess As Long End Type
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000 Private Const PRINTER_ACCESS_ADMINISTER = &H4 Private Const PRINTER_ACCESS_USE = &H8 Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
Private Const PRINTER_ATTRIBUTE_DEFAULT = 4
Private Declare Function OpenPrinterA Lib "winspool.drv" _ (ByVal pPrinterName As String, phPrinter As Long, _ pDefault As PRINTER_DEFAULTS) As Long
Private Declare Function SetPrinter2 Lib "winspool.drv" _ Alias "SetPrinterA" (ByVal hPrinter As Long, _ ByVal Level As Long, pPrinter As Any, _ ByVal Command As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" _ Alias "GetPrinterA" (ByVal hPrinter As Long, _ ByVal Level As Long, pPrinter As Any, _ ByVal cbBuf As Long, pcbNeeded As Long) As Long
Private Declare Function PrinterProperties Lib "winspool.drv" _ (ByVal hwnd As Long, ByVal hPrinter As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" _ (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Const TEXTCAPS = 34 Private Const CURVECAPS = 28 Private Const LINECAPS = 30 Private Const POLYGONALCAPS = 32 Private Const RASTERCAPS = 38 Private Const CLIPCAPS = 36
Private Const DRIVERVERSION = 0 Private Const TECHNOLOGY = 2 Private Const HORZSIZE = 4 Private Const VERTSIZE = 6 Private Const HORZRES = 8 Private Const VERTRES = 10 Private Const LOGPIXELSX = 88 Private Const LOGPIXELSY = 90 Private Const ASPECTX = 40 Private Const ASPECTY = 42 Private Const ASPECTXY = 44 Private Const PDEVICESIZE = 26 Private Const CP_REGION = 2 Private Const CP_RECTANGLE = 1 Private Const CP_NONE = 0 Private Const PHYSICALWIDTH = 110 Private Const PHYSICALOFFSETY = 113 Private Const PHYSICALOFFSETX = 112 Private Const PHYSICALHEIGHT = 111 Private Const SCALINGFACTORX = 114 Private Const SCALINGFACTORY = 115 Private Const RC_BANDING = 2 Private Const RC_BIGFONT = &H400 Private Const RC_BITBLT = 1 Private Const RC_BITMAP64 = 8 Private Const RC_DEVBITS = &H8000 Private Const RC_DIBTODEV = &H200 Private Const RC_FLOODFILL = &H1000 Private Const RC_NONE = 0 Private Const RC_SCALING = 4 Private Const RC_SAVEBITMAP = &H40 Private Const RC_PALETTE = &H100 Private Const RC_STRETCHBLT = &H800 Private Const RC_STRETCHDIB = &H2000 Private Const TC_CP_STROKE = &H4 Private Const TC_CR_90 = &H8 Private Const TC_CR_ANY = &H10 Private Const TC_EA_DOUBLE = &H200 Private Const TC_GP_TRAP = 2 Private Const TC_HARDERR = 1 Private Const TC_NORMAL = 0 Private Const TC_IA_ABLE = &H400 Private Const TC_OP_CHARACTER = &H1 Private Const TC_OP_STROKE = &H2 Private Const TC_RA_ABLE = &H2000 Private Const TC_RESERVED = &H8000 Private Const TC_SA_CONTIN = &H100 Private Const TC_SA_DOUBLE = &H40 Private Const TC_SA_INTEGER = &H80 Private Const TC_SCROLLBLT = &H10000 Private Const TC_SF_X_YINDEP = &H20 Private Const TC_SIGNAL = 3 Private Const TC_SO_ABLE = &H1000 Private Const TC_UA_ABLE = &H800 Private Const TC_VA_ABLE = &H4000
Private Const DT_PLOTTER = 0 Private Const DT_RASCAMERA = 3 Private Const DT_RASDISPLAY = 1 Private Const DT_RASPRINTER = 2 Private Const DT_CHARSTREAM = 4 Private Const DT_DISPFILE = 6 Private Const DT_METAFILE = 5
Private Declare Function DeviceCapabilities Lib "winspool.drv" _ Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, _ ByVal lpPort As String, ByVal iIndex As Long, ByVal lpOutput As String, _ ByVal lpDevMode As Long) As Long
Private Declare Function DeviceCapabilities2 Lib "winspool.drv" _ Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, _ ByVal lpPort As String, ByVal iIndex As Long, ptOutput As Any, _ ByVal lpDevMode As Long) As Long
Private Const DC_FIELDS = 1 Private Const DC_PAPERS = 2 Private Const DC_PAPERSIZE = 3 Private Const DC_MINEXTENT = 4 Private Const DC_MAXEXTENT = 5 Private Const DC_BINS = 6 Private Const DC_DUPLEX = 7 Private Const DC_SIZE = 8 Private Const DC_EXTRA = 9 Private Const DC_VERSION = 10 Private Const DC_DRIVER = 11 Private Const DC_BINNAMES = 12 Private Const DC_ENUMRESOLUTIONS = 13 Private Const DC_FILEDEPENDENCIES = 14 Private Const DC_TRUETYPE = 15 Private Const DC_PAPERNAMES = 16 Private Const DC_ORIENTATION = 17 Private Const DC_COPIES = 18 Private Const DCTT_BITMAP = &H1& Private Const DCTT_DOWNLOAD = &H2& Private Const DCTT_SUBDEV = &H4&
Private Type RECTL Left As Long Top As Long Right As Long Bottom As Long End Type
Private Type SIZEL cX As Long cY As Long End Type
Private Type FORM_INFO_1 Flags As Long pName As Long Size As SIZEL ImageableArea As RECTL End Type
Private Type sFORM_INFO_1 Flags As Long pName As String Size As SIZEL ImageableArea As RECTL End Type
Private Const TLNotSupported As Long = -1
Public Enum TLPrinterPrintQuality TLDraft = -1 TLHigh = -4 TLLow = -2 TLMedium = -3 End Enum
Public Enum TLPrinterOrientation TLOrientUndefined = 0 TLPortrait = 1 TLLandscape = 2 End Enum
Public Enum TLPrinterColor TLFullColor = 2 TLMonochrome = 1 End Enum
Public Enum TLPrinterTTOption TLTTBitmap = 1 TLTTDownLoad = 2 TLTTDownLoadOutline = 4 TLTTSubDev = 3 End Enum
Public Enum TLPrinterCollate TLCollateFalse = 0 TLCollateTrue = 1 End Enum
Public Enum TLPrinterDuplex TLDuplexSimple = 1 TLDuplexHorizontal = 2 TLDuplexVertical = 1 End Enum
Public Enum TLPrinterPaperSize TLLetter = 1 TLLetterSmall = 2 TLTabloid = 3 TLLedger = 4 TLLegal = 5 TLStatement = 6 TLExecutive = 7 TLA3 = 8 TLA4 = 9 TLA4Small = 10 TLA5 = 11 TLB4 = 12 TLB5 = 13 TLFolio = 14 TLQuarto = 15 TL10x14 = 16 TL11X17 = 17 TLNote = 18 TLEnv9 = 19 TLEnv10 = 20 TLEnv11 = 21 TLEnv12 = 22 TLEnv14 = 23 TLCSheet = 24 TLDSheet = 25 TLESheet = 26 TLEnvDl = 27 TLEnvC5 = 28 TLEnvC3 = 29 TLEnvC4 = 30 TLEnvC6 = 31 TLEnvC65 = 32 TLEnvB4 = 33 TLEnvB5 = 34 TLEnvB6 = 35 TLEnvItaly = 36 TLEnvMonarch = 37 TLEnvPersonal = 38 TLFanfoldUS = 39 TLFanfoldStdGerman = 40 TLFanfoldLglGerman = 41 TLISOB4 = 42 TLJapanesePostcard = 43 TL9X11 = 44 TL10X11 = 45 TL15X11 = 46 TLEnvInvite = 47 TLReserved48 = 48 TLReserved49 = 49 TLLetterExtra = 50 TLLegalExtra = 51 TLTabloidExtra = 52 TLA4Extra = 53 TLLetterTransverse = 54 TLA4Transverse = 55 TLLetterExtraTransverse = 56 TLAPlus = 57 TLBPlus = 58 TLLetterPlus = 59 TLA4Plus = 60 TLA5Transverse = 61 TLB5Transverse = 62 TLA3Extra = 63 TLA5Extra = 64 TLB5Extra = 65 TLA2 = 66 TLA3Transverse = 67 TLA3ExtraTransverse = 68 TLUser = 256 End Enum
Public Enum TLPrinterCapability TLIsPlotter TLIsRasterPrinter TLIsRasterCamera TLIsRasterDisplay TLIsCharacterStream TLIsDiplayFile TLIsMetafile TLBitBltSupported TLSetDIBitsToDeviceSupported TLStretchBltSupported TLStretchDIBSupported TLFloodFillSupported TLBigBitmapSupported TLBigFontSupported TLRasterFontSupported TLVectorFontSupported TLBoldFontSupported TLItalicFontSupported TLUnderlineFontSupported TLStrikeoutFontSupported TLClipToRectangleSupported TLClipToRegionSupported TLCharacterRotationSupported TLCharacter90DegreesRotationSupported End Enum
Private mPhysicalWidth As Long Private mPhysicalHeight As Long Private mHorzRes As Long Private mVertRes As Long Private mLogPixelsX As Long Private mLogPixelsY As Long Private mPhysicalOffsetX As Long Private mPhysicalOffsetY As Long Private mPageWidth As Single Private mPageHeight As Single Private mAvailWidth As Single Private mAvailHeight As Single Private mLeftGap As Single Private mTopGap As Single Private mRightGap As Single Private mBottomGap As Single Private mPrinterPixPerMMX As Single Private mPrinterPixPerMMY As Single Private mPrinterPixPerMM As Single
Private Const mMMPerInch As Single = 25.4
Private mhDCPrinter As Long Private mhPrinter As Long
Private mPrinterDeviceName As String Private mPrinterDriverName As String Private mPrinterPort As String Private mDEVMODE As DEVMODE Private mDEVMODESIZE As Long
Public Function IsWin9X() As Boolean On Error GoTo Er Dim oSinfo As OSVERSIONINFO Dim dl As Integer
IsWin9X = False
oSinfo.dwOSVersionInfoSize = 148 oSinfo.szCSDVersion = Space$(128)
dl = GetVersionExA(oSinfo)
If oSinfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then IsWin9X = True
Er: End Function
Private Function StripNulls(OriginalStr As String) As String On Error Resume Next If (InStr(OriginalStr, Chr$(0)) > 0) Then OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr$(0)) - 1) End If StripNulls = Trim$(OriginalStr) End Function
Private Function ByteToString(ByteArray() As Byte) As String On Error Resume Next Dim TempStr As String Dim I As Integer
For I = 1 To CCHDEVICENAME TempStr = TempStr & Chr$(ByteArray(I)) Next I ByteToString = StripNulls(TempStr) End Function
Private Function PtrCtoVbString(ByVal Add As Long) As String Dim sTemp As String * 512, X As Long On Error Resume Next X = LstrCpy(sTemp, ByVal Add) If (InStr(1, sTemp, Chr$(0)) = 0) Then PtrCtoVbString = "" Else PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr$(0)) - 1) End If End Function
Private Function CreatePrinterDC(sPrinterName As String) As Long On Error GoTo Er
Dim X As Printer Dim dl As Long
CreatePrinterDC = 0
If mhDCPrinter <> 0 Then dl = DeleteDC(mhDCPrinter)
For Each X In Printers If X.DeviceName = sPrinterName Then mhDCPrinter = CreateDC(X.DriverName, X.DeviceName, 0&, 0&) If mhDCPrinter <> 0 Then CreatePrinterDC = 1 mPrinterDeviceName = X.DeviceName mPrinterDriverName = X.DriverName mPrinterPort = X.Port GetDEVMODE LoadPrinterData End If Exit Function End If Next X
Er: End Function
Private Sub GetDEVMODE() On Error Resume Next
Dim dl As Long Dim nSize As Long Dim aDevMode() As Byte Dim TempStr As String Dim hPrinter As Long
If mhPrinter <> 0 Then Call ClosePrinter(mhPrinter) dl = DeleteObject(mhPrinter) mhPrinter = 0 End If
If OpenPrinter(mPrinterDeviceName, mhPrinter, NULLPTR) = 0 Then Exit Sub
nSize = DocumentProperties(NULLPTR, mhPrinter, mPrinterDeviceName, _ NULLPTR, NULLPTR, 0)
If nSize < 1 Then Exit Sub
ReDim aDevMode(1 To nSize)
mDEVMODESIZE = nSize
nSize = DocumentProperties(NULLPTR, mhPrinter, mPrinterDeviceName, _ aDevMode(1), NULLPTR, DM_OUT_BUFFER)
If nSize < 0 Then Exit Sub
Call CopyMemory(mDEVMODE, aDevMode(1), Len(mDEVMODE))
End Sub
Public Function SetPrinterCustomPaperSize(ByVal WidthMM As Single, ByVal HeightMM As Single) As Long On Error GoTo Er
Dim dl As Long
SetPrinterCustomPaperSize = 0 dl = PrivateSetPrinterCustomPaperSize(WidthMM, HeightMM, True) If dl = 0 Or Abs(mPageWidth - WidthMM) > 1 Or Abs(mPageHeight - HeightMM) > 1 Then If Me.GetPrinterOrientation = TLPortrait Then dl = Me.SetPrinterOrientation(TLLandscape) Else dl = Me.SetPrinterOrientation(TLPortrait) End If If dl = 0 Then Exit Function dl = PrivateSetPrinterCustomPaperSize(WidthMM, HeightMM, True) If dl = 0 Or Abs(mPageWidth - WidthMM) > 1 Or Abs(mPageHeight - HeightMM) > 1 Then If Not IsWin9X Then dl = PrivateSetPrinterCustomPaperSize(WidthMM, HeightMM, False) If dl = 0 Or Abs(mPageWidth - WidthMM) > 1 Or Abs(mPageHeight - HeightMM) > 1 Then If Me.GetPrinterOrientation = TLPortrait Then dl = Me.SetPrinterOrientation(TLLandscape) Else dl = Me.SetPrinterOrientation(TLPortrait) End If If dl = 0 Then Exit Function dl = PrivateSetPrinterCustomPaperSize(WidthMM, HeightMM, False) If dl = 0 Or Abs(mPageWidth - WidthMM) > 1 Or Abs(mPageHeight - HeightMM) > 1 Then Exit Function End If End If End If End If End If
SetPrinterCustomPaperSize = 1 Er: End Function
Private Function PrivateSetPrinterCustomPaperSize(ByVal WidthMM As Single, ByVal HeightMM As Single, _ Optional ByVal bWin9X As Boolean = True) As Long On Error Resume Next
PrivateSetPrinterCustomPaperSize = 0 If mhDCPrinter = 0 Then Exit Function
If bWin9X Then
Dim dl As Long Dim iA As Long Dim aDevMode() As Byte Dim nSize As Long Dim sCustomWidth As Long Dim sCustomHeight As Long
If mDEVMODESIZE < 1 Then Exit Function
mDEVMODE.dmFields = mDEVMODE.dmFields And _ (Not (DM_PAPERSIZE Or DM_PAPERWIDTH Or DM_PAPERLENGTH)) mDEVMODE.dmPaperSize = DMPAPER_USER mDEVMODE.dmPaperWidth = WidthMM * 10 mDEVMODE.dmPaperLength = HeightMM * 10 mDEVMODE.dmFields = mDEVMODE.dmFields Or DM_PAPERSIZE _ Or DM_PAPERLENGTH Or DM_PAPERWIDTH ReDim aDevMode(1 To mDEVMODESIZE) Call CopyMemory(aDevMode(1), mDEVMODE, Len(mDEVMODE)) nSize = DocumentProperties(NULLPTR, mhPrinter, mPrinterDeviceName, _ aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER) nSize = ResetDC(mhDCPrinter, aDevMode(1)) If nSize = 0 Then Exit Function Call CopyMemory(mDEVMODE, aDevMode(1), Len(mDEVMODE)) If nSize <> mhDCPrinter Then dl = DeleteDC(mhDCPrinter) mhDCPrinter = nSize End If LoadPrinterData
PrivateSetPrinterCustomPaperSize = 1
Else
Dim NumForms As Long Dim FI1 As FORM_INFO_1 Dim FI2 As sFORM_INFO_1 Dim aFI1() As FORM_INFO_1 Dim Temp() As Byte Dim aByte() As Byte Dim BytesNeeded As Long Dim FormName As String
If mDEVMODESIZE < 1 Then Exit Function
dl = EnumForms(mhPrinter, 1, 0&, 0&, BytesNeeded, _ NumForms) ReDim Temp(BytesNeeded) ReDim aFI1(BytesNeeded / Len(FI1)) dl = EnumForms(mhPrinter, 1, Temp(0), BytesNeeded, _ BytesNeeded, NumForms) Call CopyMemory(aFI1(0), Temp(0), BytesNeeded) For iA = 0 To NumForms - 1 With aFI1(iA) FormName = PtrCtoVbString(.pName) If sCustomFormTitle = FormName Then dl = DeleteForm(mhPrinter, sCustomFormTitle & Chr$(0)) Exit For End If End With Next iA
With FI2 .Flags = 0 .pName = sCustomFormTitle With .Size .cX = CLng(WidthMM * 1000) .cY = CLng(HeightMM * 1000) End With With .ImageableArea .Left = 0 .Top = 0 .Right = FI2.Size.cX .Bottom = FI2.Size.cY End With End With
ReDim aByte(1 To Len(FI2)) Call CopyMemory(aByte(1), FI2, Len(FI2))
dl = AddForm(mhPrinter, 1, aByte(1))
If dl = 0 Then Exit Function
mDEVMODE.dmFormName = sCustomFormTitle & Chr$(0) mDEVMODE.dmFields = mDEVMODE.dmFields Or DM_FORMNAME ReDim aDevMode(1 To mDEVMODESIZE) Call CopyMemory(aDevMode(1), mDEVMODE, Len(mDEVMODE)) dl = DocumentProperties(NULLPTR, mhPrinter, mPrinterDeviceName, _ aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER) dl = ResetDC(mhDCPrinter, aDevMode(1)) If dl = 0 Then Exit Function Call CopyMemory(mDEVMODE, aDevMode(1), Len(mDEVMODE)) If dl <> mhDCPrinter Then nSize = DeleteDC(mhDCPrinter) mhDCPrinter = dl End If LoadPrinterData
PrivateSetPrinterCustomPaperSize = 1
End If
End Function
Public Function SetPrinterOrientation(gOrientation As TLPrinterOrientation) As Long
On Error Resume Next
Dim dl As Long Dim nSize As Long Dim aDevMode() As Byte SetPrinterOrientation = 0 If mhDCPrinter = 0 Then Exit Function If gOrientation = mDEVMODE.dmOrientation Then SetPrinterOrientation = 1 Exit Function End If If mDEVMODESIZE < 1 Then Exit Function mDEVMODE.dmOrientation = gOrientation mDEVMODE.dmFields = mDEVMODE.dmFields Or DM_ORIENTATION ReDim aDevMode(1 To mDEVMODESIZE) Call CopyMemory(aDevMode(1), mDEVMODE, Len(mDEVMODE)) nSize = DocumentProperties(NULLPTR, mhPrinter, mPrinterDeviceName, _ aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER) nSize = ResetDC(mhDCPrinter, aDevMode(1)) If nSize = 0 Then Exit Function Call CopyMemory(mDEVMODE, aDevMode(1), Len(mDEVMODE)) If nSize <> mhDCPrinter Then dl = DeleteDC(mhDCPrinter) mhDCPrinter = nSize End If LoadPrinterData SetPrinterOrientation = 1
End Function
Private Function LoadPrinterData() As Long On Error GoTo myExit
Dim dl As Long
LoadPrinterData = 0
If mhDCPrinter = 0 Then GoTo myExit
mPhysicalWidth = GetDeviceCaps(mhDCPrinter, PHYSICALWIDTH) mPhysicalHeight = GetDeviceCaps(mhDCPrinter, PHYSICALHEIGHT)
mHorzRes = GetDeviceCaps(mhDCPrinter, HORZRES)
mVertRes = GetDeviceCaps(mhDCPrinter, VERTRES)
mLogPixelsX = GetDeviceCaps(mhDCPrinter, LOGPIXELSX) mLogPixelsY = GetDeviceCaps(mhDCPrinter, LOGPIXELSY)
mPhysicalOffsetX = GetDeviceCaps(mhDCPrinter, PHYSICALOFFSETX) mPhysicalOffsetY = GetDeviceCaps(mhDCPrinter, PHYSICALOFFSETY)
mPageWidth = mPhysicalWidth / mLogPixelsX * mMMPerInch mPageHeight = mPhysicalHeight / mLogPixelsX * mMMPerInch mAvailWidth = mHorzRes / mLogPixelsX * mMMPerInch mAvailHeight = mVertRes / mLogPixelsY * mMMPerInch
mLeftGap = mPhysicalOffsetX / mLogPixelsX * mMMPerInch mTopGap = mPhysicalOffsetY / mLogPixelsY * mMMPerInch mRightGap = mPageWidth - mAvailWidth - mLeftGap mBottomGap = mPageHeight - mAvailHeight - mTopGap
If mAvailWidth > mPageWidth Or mAvailHeight > mPageHeight Or _ mAvailWidth <= 0 Or mAvailHeight <= 0 Or _ mLeftGap < 0 Or mRightGap < 0 Or mTopGap < 0 Or mBottomGap < 0 Or _ Abs((mAvailWidth + mLeftGap + mRightGap) - mPageWidth) > 1 Or _ Abs((mAvailHeight + mTopGap + mBottomGap) - mPageHeight) > 1 Then mAvailWidth = mPageWidth mAvailHeight = mPageHeight mLeftGap = 0 mRightGap = 0 mTopGap = 0 mBottomGap = 0 End If
mPrinterPixPerMMX = mLogPixelsX / mMMPerInch mPrinterPixPerMMY = mLogPixelsY / mMMPerInch mPrinterPixPerMM = (mPrinterPixPerMMX + mPrinterPixPerMMY) / 2
LoadPrinterData = 1 Exit Function
myExit:
mLogPixelsX = 300 mLogPixelsY = 300 mPhysicalOffsetX = 0 mPhysicalOffsetY = 0
mLeftGap = 20 mRightGap = 20 mTopGap = 20 mBottomGap = 20
mPageWidth = 210 mPageHeight = 297 mAvailWidth = mPageWidth - 40 mAvailHeight = mPageHeight - 40
mPrinterPixPerMMX = mLogPixelsX / mMMPerInch mPrinterPixPerMMY = mLogPixelsY / mMMPerInch mPrinterPixPerMM = (mPrinterPixPerMMX + mPrinterPixPerMMY) / 2 End Function
Function GetPrinterOrientation() As TLPrinterOrientation
Dim bufsize, res As Long Dim dmIn As DEVMODE, dmOut As DEVMODE Dim dm As DEVMODE Dim DeviceName As String
If mhDCPrinter = 0 Then MsgBox "Unable to open default printer" Exit Function End If bufsize = DocumentProperties(hwnd, mhPrinter, mPrinterDeviceName, dm, dm, 0) res = DocumentProperties(0, mhPrinter, mPrinterDeviceName, dmOut, dm, DM_OUT_BUFFER) If dmOut.dmOrientation = DMORIENT_LANDSCAPE Then GetPrinterOrientation = TLLandscape Else GetPrinterOrientation = TLPortrait End If End Function
|
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|