AVAX-software.com Forums: PrintToPrinter not working correctly for me - AVAX-software.com Forums

Jump to content

Page 1 of 1

PrintToPrinter not working correctly for me

#1 User is offline   DGrace Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 16-May 06

Posted 18 March 2008 - 02:06 AM

I am using the OCX and I am having trouble with PrintToPrinter method. I cannot make the Landscape automatically select with the printer. Also, if I supply the printer name without showing the dialog, it will not print. If I have it show the print dialog, it works fine but I do have to manually change to Landscape. Any suggestions to try.

Thanks,
Daryl Grace

#2 User is offline   DGrace Icon

  • Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 16-May 06

Posted 19 March 2008 - 01:12 PM

After updating to the latest Avax ocx, I can get the PrintToPrinter to respond, but I still cannot get it to print in landscape mode. I have the fLandscape equal to True. Any suggestions?

Thanks,
Daryl

#3 User is offline   Athanasios Gardos Icon

  • Administrator
  • Icon
  • Group: Admin
  • Posts: 333
  • Joined: 21-March 05
  • Gender:Male

Posted 20 March 2008 - 09:33 AM

fLandscape parameter doesn't changes the orientation of the paper. It is an information to the control for the paper orientation (portrait or landscape). You have to change the orientation using code before calling PrintToPrinter function. Please, have a look to the following VB6 example:

Option Explicit

'Constants used in the DevMode structure
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

'Constants for NT security
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)

'Constants used to make changes to the values contained in the DevMode
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_DUPLEX = &H1000&
Private Const DMDUP_SIMPLEX = 1
Private Const DMDUP_VERTICAL = 2
Private Const DMDUP_HORIZONTAL = 3
Private Const DM_ORIENTATION = &H1&
Private PageDirection As Integer

'------USER DEFINED TYPES
Private Type DEVMODE
	dmDeviceName As String * CCHDEVICENAME
	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
	dmLogPixels As Integer
	dmBitsPerPel As Long
	dmPelsWidth As Long
	dmPelsHeight As Long
	dmDisplayFlags As Long
	dmDisplayFrequency As Long
	dmICMMethod As Long		' // Windows 95 only
	dmICMIntent As Long		' // Windows 95 only
	dmMediaType As Long		' // Windows 95 only
	dmDitherType As Long	   ' // Windows 95 only
	dmReserved1 As Long		' // Windows 95 only
	dmReserved2 As Long		' // Windows 95 only
End Type

Private Type PRINTER_DEFAULTS
	pDatatype As String
	pDevMode As Long
	DesiredAccess As Long
End Type


'------DECLARATIONS

Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Private Declare Function SetPrinter 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 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 Long

Private 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

Private Sub SetOrientation(NewSetting As Long, chng As Integer, ByVal frm As Form)
	Dim PrinterHandle As Long
	Dim PrinterName As String
	Dim pd As PRINTER_DEFAULTS
	Dim MyDevMode As DEVMODE
	Dim Result As Long
	Dim Needed As Long
	Dim pFullDevMode As Long
	Dim pi2_buffer() As Long
	
	PrinterName = Printer.DeviceName
	If PrinterName = "" Then
		Exit Sub
	End If
	pd.pDatatype = vbNullString
	pd.pDevMode = 0&
	pd.DesiredAccess = PRINTER_ALL_ACCESS
	Result = OpenPrinter(PrinterName, PrinterHandle, pd)
	Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
	ReDim pi2_buffer((Needed \ 4))
	Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
	pFullDevMode = pi2_buffer(7)
	Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
	'Make desired changes
	MyDevMode.dmDuplex = NewSetting
	MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION
	MyDevMode.dmOrientation = chng
	Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
	Result = DocumentProperties(frm.hwnd, PrinterHandle, PrinterName, ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)
	Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)
	Call ClosePrinter(PrinterHandle)
	Dim p As Printer
	For Each p In Printers
		If p.DeviceName = PrinterName Then
			Set Printer = p
			Exit For
		End If
	Next p
	Printer.Duplex = MyDevMode.dmDuplex
End Sub

Private Sub ChngPrinterOrientationLandscape(ByVal frm As Form)
	PageDirection = 2
	Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
End Sub

Private Sub ResetPrinterOrientation(ByVal frm As Form)
	If PageDirection = 1 Then
		PageDirection = 2
	Else
		PageDirection = 1
	End If
	Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
End Sub

Private Sub ChngPrinterOrientationPortrait(ByVal frm As Form)
	PageDirection = 1
	Call SetOrientation(DMDUP_SIMPLEX, PageDirection, frm)
End Sub

Private Sub cmdChangePrinterOrient()
	If optPort.Value = True Then
		ChngPrinterOrientationPortrait Me
	ElseIf optLand.Value = True Then
		ChngPrinterOrientationLandscape Me
	End If
End Sub

Private Sub cmdPrint_Click()
	Dim fLandScape As Boolean
	If optPort.Value = True Then
	   fLandScape = False
	ElseIf optLand.Value = True Then
	   fLandScape = True
	End If
	Call cAvax1.PrintToPrinter(Printer.DeviceName, epsA4, 10!, , , , , , fLandScape)
End Sub

Private Sub Form_Load()
	optPort.Caption = "Portrait"
	optLand.Caption = "Landscape"
	cmdPrint.Caption = "Print"
	Call cAvax1.StartAvax
	Call cAvax1.Add_Rectangle(0, 0, 20, 10)
End Sub

Private Sub Form_Unload(Cancel As Integer)
	Call cAvax1.EndAvax
End Sub

Private Sub optLand_Click()
	Call cmdChangePrinterOrient
End Sub

Private Sub optPort_Click()
	Call cmdChangePrinterOrient
End Sub

Athanasios Gardos
Avax-Software.com

Page 1 of 1


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users