Option Explicit'@~~~~~~~ NT Security Constants ~~~~~~~~@ 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)'@~~~~~~~ DEVMODE Constants ~~~~~~~~@ Private Const DM_MODIFY = 8 Private Const DM_IN_BUFFER = DM_MODIFY Private Const DM_COPY = 2 Private Const DM_OUT_BUFFER = DM_COPY Private Const DM_ORIENTATION = &H1& Private Const DMORIENT_LANDSCAPE = 2 Private Const DMORIENT_PORTRAIT = 1'@~~~~~~~~~~~ DEVMODE ~~~~~~~~~~~@ ' I have removed all of the NT only and ' Windows 9X (2000 as well) only elementsPrivate Type DEVMODE dmDeviceName As String * 32 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 * 32 dmLogPixels As Integer dmBitsPerPel As Long dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type Private Type PRINTER_DEFAULTS pDatatype As String pDevMode As Long DesiredAccess As Long End TypePrivate Declare Function OpenPrinter Lib _ "winspool.drv" Alias "OpenPrinterA" _ (ByVal pPrinterName As String, phPrinter As Long, _ pDefault As PRINTER_DEFAULTS) As LongPrivate Declare Function SetPrinter Lib _ "winspool.drv" Alias "SetPrinterA" _ (ByVal hPrinter As Long, ByVal Level As Long, _ pPrinter As Any, ByVal Command As Long) As LongPrivate 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 LongPrivate Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" (hpvDest As Any, _ hpvSource As Any, ByVal cbCopy As Long) Private Declare Function ClosePrinter Lib _ "winspool.drv" (ByVal hPrinter As Long) As LongPrivate Declare Function DocumentProperties Lib _ "winspool.drv" Alias "DocumentPropertiesA" _ (ByVal hwnd As Long, ByVal hPrinter As Long, _ ByVal pDeviceName As String, ByVal _ pDevModeOutput As Any, ByVal pDevModeInput As Any, _ ByVal fMode As Long) As Long'The Procedure that does the change Public Sub SetOrientation(strPrnName As String, intOrient As Integer) Dim udtPD As PRINTER_DEFAULTS Dim udtDEVMODE As DEVMODE Dim lngBuffer() As Long Dim lngPrnHndle As Long Dim lngRetVal As Long Dim lngDMpntr As Long Dim lngRet As Long udtPD.pDatatype = vbNullString udtPD.pDevMode = 0& 'The next call is NT security, it 'Has no adverse affect on Windows 9X or 2000 udtPD.DesiredAccess = PRINTER_ALL_ACCESS
'The pointer (7th element of the array) to the DEVMODE lngDMpntr = lngBuffer(7) 'Public to Private and vice-versa Call CopyMemory(udtDEVMODE, ByVal lngDMpntr, Len(udtDEVMODE))
'Mark the bit and Change the orientation udtDEVMODE.dmFields = DM_ORIENTATION udtDEVMODE.dmOrientation = intOrient
'The Magic happens right here! lngRet = SetPrinter(lngPrnHndle, 2, lngBuffer(0), 0&) 'All done Call ClosePrinter(lngPrnHndle) End Sub'@~~~~~~~ Test it both ways ~~~~~~~~~~~@Public Sub TestLandScape() SetOrientation "Canon Bubble-Jet BJC-4000", _ DMORIENT_LANDSCAPE 'See the values in the constants End SubPublic Sub TestPortrait() SetOrientation "Canon Bubble-Jet BJC-4000", _ DMORIENT_PORTRAIT 'See the values in the constants End Sub
LONG DocumentProperties(
HWND hWnd, //显视对话框的句柄
HANDLE hPrinter, //打印机句柄
LPTSTR pDeviceName, //设备名称地址
PDEVMODE pDevmodeOutput, //指向改变后的设置后的地址
PDEVMODE pDevmodeInput, //指向原始设置地址
DWORD fMode //模式指针,取值为DM_IN_BUFFER
//DM_IN_PROMPT 或 DM_OUT_BUFFER
);
借助Printer.PaperSize可以设定打印机的纸张大小
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)'@~~~~~~~ DEVMODE Constants ~~~~~~~~@
Private Const DM_MODIFY = 8
Private Const DM_IN_BUFFER = DM_MODIFY
Private Const DM_COPY = 2
Private Const DM_OUT_BUFFER = DM_COPY
Private Const DM_ORIENTATION = &H1&
Private Const DMORIENT_LANDSCAPE = 2
Private Const DMORIENT_PORTRAIT = 1'@~~~~~~~~~~~ DEVMODE ~~~~~~~~~~~@
' I have removed all of the NT only and
' Windows 9X (2000 as well) only elementsPrivate Type DEVMODE
dmDeviceName As String * 32
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 * 32
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Private Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As Long
DesiredAccess As Long
End TypePrivate Declare Function OpenPrinter Lib _
"winspool.drv" Alias "OpenPrinterA" _
(ByVal pPrinterName As String, phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As LongPrivate Declare Function SetPrinter Lib _
"winspool.drv" Alias "SetPrinterA" _
(ByVal hPrinter As Long, ByVal Level As Long, _
pPrinter As Any, ByVal Command As Long) As LongPrivate 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 LongPrivate Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (hpvDest As Any, _
hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function ClosePrinter Lib _
"winspool.drv" (ByVal hPrinter As Long) As LongPrivate Declare Function DocumentProperties Lib _
"winspool.drv" Alias "DocumentPropertiesA" _
(ByVal hwnd As Long, ByVal hPrinter As Long, _
ByVal pDeviceName As String, ByVal _
pDevModeOutput As Any, ByVal pDevModeInput As Any, _
ByVal fMode As Long) As Long'The Procedure that does the change
Public Sub SetOrientation(strPrnName As String, intOrient As Integer)
Dim udtPD As PRINTER_DEFAULTS
Dim udtDEVMODE As DEVMODE
Dim lngBuffer() As Long
Dim lngPrnHndle As Long
Dim lngRetVal As Long
Dim lngDMpntr As Long
Dim lngRet As Long udtPD.pDatatype = vbNullString
udtPD.pDevMode = 0&
'The next call is NT security, it
'Has no adverse affect on Windows 9X or 2000
udtPD.DesiredAccess = PRINTER_ALL_ACCESS
lngRet = OpenPrinter(strPrnName, lngPrnHndle, udtPD)
lngRet = GetPrinter(lngPrnHndle, 2, ByVal 0&, 0, lngRetVal)
ReDim lngBuffer((lngRetVal \ 4))
lngRet = GetPrinter(lngPrnHndle, 2, lngBuffer(0), _
lngRetVal, lngRetVal)
'The pointer (7th element of the array) to the DEVMODE
lngDMpntr = lngBuffer(7)
'Public to Private and vice-versa
Call CopyMemory(udtDEVMODE, ByVal lngDMpntr, Len(udtDEVMODE))
'Mark the bit and Change the orientation
udtDEVMODE.dmFields = DM_ORIENTATION
udtDEVMODE.dmOrientation = intOrient
Call CopyMemory(ByVal lngDMpntr, udtDEVMODE, Len(udtDEVMODE)) lngRet = DocumentProperties(ThisDrawing.hwnd, _
lngPrnHndle, strPrnName, ByVal lngDMpntr, _
ByVal lngDMpntr, DM_IN_BUFFER Or DM_OUT_BUFFER)
'The Magic happens right here!
lngRet = SetPrinter(lngPrnHndle, 2, lngBuffer(0), 0&)
'All done
Call ClosePrinter(lngPrnHndle)
End Sub'@~~~~~~~ Test it both ways ~~~~~~~~~~~@Public Sub TestLandScape()
SetOrientation "Canon Bubble-Jet BJC-4000", _
DMORIENT_LANDSCAPE
'See the values in the constants
End SubPublic Sub TestPortrait()
SetOrientation "Canon Bubble-Jet BJC-4000", _
DMORIENT_PORTRAIT
'See the values in the constants
End Sub