AYUDA! Imprimir formulario en VB6

#1
que tal! pues necesito de su ayuda :(

ando haciendo un programa en visual basic 6 y tengo que mandar a imprimir un formulario desde otro

osea, en el Form1 hay un boton que manda a imprimir el formulario Form2

pero necesito que el formulario Form2 se imprimia en blanco y negro porque lleva imagenes

entonces, para llamar la ventana de propiedades de impresion que codigo puedo usar? para que la persona q ocupe el programa decida si quiere imprimir a color o b/n y si quiere mas de una copia

espero haberme explicado y que puedan ayudarme
Saludos
 
#2
Hola, que tal !!! Mmmm.. para resolver este inconveniente, yo te sugeriría que utilices CommonDialog de Visual Basic para imprimir.

La info completa esta en este enlace:
http://msdn.microsoft.com/es-es/library/256tssz7%28v=vs.80%29.aspx

Del mismo modo, ya aterrizando una solución como tal, podrías intentar lo que pusieron en este otro enlace:

http://www.recursosvisualbasic.com.ar/htm/listado-api/156-commondialog-imprimir.htm


He aquí un preview:

El ejemplo permite abrir el diálogo de imprimir :



y el de configurar página :



Controles

  1. Colocar en un proyecto un módulo bas.
  2. En el formulario agregar dos Commandbutton.El Commnad1 muestra el cuadro de diálogo para Imprimir y el Command2 el de Configurar página.

Código fuente en el módulo bas:

Texto planoImprimir


  1. Option Explicit
  2. 'Constantes
  3. Const CCHDEVICENAME = 32
  4. Const CCHFORMNAME = 32
  5. Const DM_DUPLEX = &H1000&
  6. Const DM_ORIENTATION = &H1&
  7. Const PD_PRINTSETUP = &H40
  8. Const GMEM_MOVEABLE = &H2
  9. Const GMEM_ZEROINIT = &H40
  10. Const PD_DISABLEPRINTTOFILE = &H80000
  11. 'Funciones API
  12. Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" ( _
  13. pPrintdlg As PRINTDLG_TYPE) As Long
  14. Private Declare Function PAGESETUPDLG Lib "comdlg32.dll" Alias "PageSetupDlgA" ( _
  15. pPagesetupdlg As PAGESETUPDLG) As Long
  16. Private Declare Function GlobalLock Lib "kernel32" ( _
  17. ByVal hMem As Long) As Long
  18. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  19. Private Declare Function GlobalAlloc Lib "kernel32" ( _
  20. ByVal wFlags As Long, _
  21. ByVal dwBytes As Long) As Long
  22. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  23. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  24. hpvDest As Any, _
  25. hpvSource As Any, _
  26. ByVal cbCopy As Long)
  27. ' UDT
  28. Private Type POINTAPI
  29. x As Long
  30. y As Long
  31. End Type
  32. Private Type RECT
  33. Left As Long
  34. Top As Long
  35. Right As Long
  36. Bottom As Long
  37. End Type
  38. Private Type PAGESETUPDLG
  39. lStructSize As Long
  40. hwndOwner As Long
  41. hDevMode As Long
  42. hDevNames As Long
  43. Flags As Long
  44. ptPaperSize As POINTAPI
  45. rtMinMargin As RECT
  46. rtMargin As RECT
  47. hInstance As Long
  48. lCustData As Long
  49. lpfnPageSetupHook As Long
  50. lpfnPagePaintHook As Long
  51. lpPageSetupTemplateName As String
  52. hPageSetupTemplate As Long
  53. End Type
  54. Private Type PRINTDLG_TYPE
  55. lStructSize As Long
  56. hwndOwner As Long
  57. hDevMode As Long
  58. hDevNames As Long
  59. hDC As Long
  60. Flags As Long
  61. nFromPage As Integer
  62. nToPage As Integer
  63. nMinPage As Integer
  64. nMaxPage As Integer
  65. nCopies As Integer
  66. hInstance As Long
  67. lCustData As Long
  68. lpfnPrintHook As Long
  69. lpfnSetupHook As Long
  70. lpPrintTemplateName As String
  71. lpSetupTemplateName As String
  72. hPrintTemplate As Long
  73. hSetupTemplate As Long
  74. End Type
  75. Private Type DEVNAMES_TYPE
  76. wDriverOffset As Integer
  77. wDeviceOffset As Integer
  78. wOutputOffset As Integer
  79. wDefault As Integer
  80. extra As String * 100
  81. End Type
  82. Private Type DEVMODE_TYPE
  83. dmDeviceName As String * CCHDEVICENAME
  84. dmSpecVersion As Integer
  85. dmDriverVersion As Integer
  86. dmSize As Integer
  87. dmDriverExtra As Integer
  88. dmFields As Long
  89. dmOrientation As Integer
  90. dmPaperSize As Integer
  91. dmPaperLength As Integer
  92. dmPaperWidth As Integer
  93. dmScale As Integer
  94. dmCopies As Integer
  95. dmDefaultSource As Integer
  96. dmPrintQuality As Integer
  97. dmColor As Integer
  98. dmDuplex As Integer
  99. dmYResolution As Integer
  100. dmTTOption As Integer
  101. dmCollate As Integer
  102. dmFormName As String * CCHFORMNAME
  103. dmUnusedPadding As Integer
  104. dmBitsPerPel As Integer
  105. dmPelsWidth As Long
  106. dmPelsHeight As Long
  107. dmDisplayFlags As Long
  108. dmDisplayFrequency As Long
  109. End Type
  110. 'Fin de declaraciones
  111. '----------------------------------
  112. ' función Para el Common diálogo de Configurar página
  113. '---------------------------------------------------------
  114. Function Configuarar_Pagina(HwndForm As Long) As Long
  115. Dim T_Configurar_Pagina As PAGESETUPDLG
  116. With T_Configurar_Pagina
  117. .lStructSize = Len(T_Configurar_Pagina)
  118. .hwndOwner = HwndForm
  119. .hInstance = App.hInstance
  120. .Flags = 0
  121. End With
  122. If PAGESETUPDLG(T_Configurar_Pagina) Then
  123. Configuarar_Pagina = 0
  124. Else
  125. Configuarar_Pagina = -1
  126. End If
  127. End Function
  128. 'Para el Common diálogo de imprimir ( pasar el formulario como parámetro )
  129. '---------------------------------------------------------
  130. Public Sub Show_Printer(El_Formulario As Form, Optional Flags As Long)
  131. On Error GoTo ErrSub
  132. Dim t_Printer As PRINTDLG_TYPE
  133. Dim DevMode As DEVMODE_TYPE
  134. Dim DevName As DEVNAMES_TYPE
  135. Dim lpDevMode As Long, lpDevName As Long
  136. Dim bReturn As Integer
  137. Dim objPrinter As Printer, NewPrinterName As String
  138. With t_Printer
  139. .lStructSize = Len(t_Printer)
  140. .hwndOwner = El_Formulario.hWnd
  141. .Flags = Flags
  142. End With
  143. On Error Resume Next
  144. DevMode.dmDeviceName = Printer.DeviceName
  145. DevMode.dmSize = Len(DevMode)
  146. DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
  147. DevMode.dmPaperWidth = Printer.Width
  148. DevMode.dmOrientation = Printer.Orientation
  149. DevMode.dmPaperSize = Printer.PaperSize
  150. DevMode.dmDuplex = Printer.Duplex
  151. On Error GoTo 0
  152. t_Printer.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
  153. lpDevMode = GlobalLock(t_Printer.hDevMode)
  154. If lpDevMode > 0 Then
  155. CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
  156. bReturn = GlobalUnlock(t_Printer.hDevMode)
  157. End If
  158. With DevName
  159. .wDriverOffset = 8
  160. .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
  161. .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
  162. .wDefault = 0
  163. End With
  164. With Printer
  165. DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
  166. End With
  167. t_Printer.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
  168. lpDevName = GlobalLock(t_Printer.hDevNames)
  169. If lpDevName > 0 Then
  170. CopyMemory ByVal lpDevName, DevName, Len(DevName)
  171. bReturn = GlobalUnlock(lpDevName)
  172. End If
  173. If PrintDialog(t_Printer) <> 0 Then
  174. lpDevName = GlobalLock(t_Printer.hDevNames)
  175. CopyMemory DevName, ByVal lpDevName, 45
  176. bReturn = GlobalUnlock(lpDevName)
  177. GlobalFree t_Printer.hDevNames
  178. lpDevMode = GlobalLock(t_Printer.hDevMode)
  179. CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
  180. bReturn = GlobalUnlock(t_Printer.hDevMode)
  181. GlobalFree t_Printer.hDevMode
  182. NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _
  183. InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
  184. If Printer.DeviceName <> NewPrinterName Then
  185. For Each objPrinter In Printers
  186. If UCase$(objPrinter.DeviceName) = NewPrinterName Then
  187. Set Printer = objPrinter
  188. End If
  189. Next
  190. End If
  191. On Error Resume Next
  192. With Printer
  193. .PaperSize = DevMode.dmPaperSize
  194. .PrintQuality = DevMode.dmPrintQuality
  195. .ColorMode = DevMode.dmColor
  196. .PaperBin = DevMode.dmDefaultSource
  197. .Copies = DevMode.dmCopies
  198. .Duplex = DevMode.dmDuplex
  199. .Orientation = DevMode.dmOrientation
  200. End With
  201. On Error GoTo 0
  202. End If
  203. Exit Sub
  204. ErrSub:
  205. If Err.Number = 484 Then
  206. MsgBox "Error al obtener información de la impresora." & _
  207. "Asegurarse que está instalada correctamente.", vbCritical
  208. End If
  209. End Sub

Option Explicit 'Constantes Const CCHDEVICENAME = 32 Const CCHFORMNAME = 32 Const DM_DUPLEX = &H1000& Const DM_ORIENTATION = &H1& Const PD_PRINTSETUP = &H40 Const GMEM_MOVEABLE = &H2 Const GMEM_ZEROINIT = &H40 Const PD_DISABLEPRINTTOFILE = &H80000 'Funciones API Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" ( _ pPrintdlg As PRINTDLG_TYPE) As Long Private Declare Function PAGESETUPDLG Lib "comdlg32.dll" Alias "PageSetupDlgA" ( _ pPagesetupdlg As PAGESETUPDLG) As Long Private Declare Function GlobalLock Lib "kernel32" ( _ ByVal hMem As Long) As Long Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Function GlobalAlloc Lib "kernel32" ( _ ByVal wFlags As Long, _ ByVal dwBytes As Long) As Long Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ hpvDest As Any, _ hpvSource As Any, _ ByVal cbCopy As Long) ' UDT Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type PAGESETUPDLG lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long Flags As Long ptPaperSize As POINTAPI rtMinMargin As RECT rtMargin As RECT hInstance As Long lCustData As Long lpfnPageSetupHook As Long lpfnPagePaintHook As Long lpPageSetupTemplateName As String hPageSetupTemplate As Long End Type Private Type PRINTDLG_TYPE lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long hDC As Long Flags As Long nFromPage As Integer nToPage As Integer nMinPage As Integer nMaxPage As Integer nCopies As Integer hInstance As Long lCustData As Long lpfnPrintHook As Long lpfnSetupHook As Long lpPrintTemplateName As String lpSetupTemplateName As String hPrintTemplate As Long hSetupTemplate As Long End Type Private Type DEVNAMES_TYPE wDriverOffset As Integer wDeviceOffset As Integer wOutputOffset As Integer wDefault As Integer extra As String * 100 End Type Private Type DEVMODE_TYPE 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 dmUnusedPadding As Integer dmBitsPerPel As Integer dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type 'Fin de declaraciones '---------------------------------- ' función Para el Common diálogo de Configurar página '--------------------------------------------------------- Function Configuarar_Pagina(HwndForm As Long) As Long Dim T_Configurar_Pagina As PAGESETUPDLG With T_Configurar_Pagina .lStructSize = Len(T_Configurar_Pagina) .hwndOwner = HwndForm .hInstance = App.hInstance .Flags = 0 End With If PAGESETUPDLG(T_Configurar_Pagina) Then Configuarar_Pagina = 0 Else Configuarar_Pagina = -1 End If End Function 'Para el Common diálogo de imprimir ( pasar el formulario como parámetro ) '--------------------------------------------------------- Public Sub Show_Printer(El_Formulario As Form, Optional Flags As Long) On Error GoTo ErrSub Dim t_Printer As PRINTDLG_TYPE Dim DevMode As DEVMODE_TYPE Dim DevName As DEVNAMES_TYPE Dim lpDevMode As Long, lpDevName As Long Dim bReturn As Integer Dim objPrinter As Printer, NewPrinterName As String With t_Printer .lStructSize = Len(t_Printer) .hwndOwner = El_Formulario.hWnd .Flags = Flags End With On Error Resume Next DevMode.dmDeviceName = Printer.DeviceName DevMode.dmSize = Len(DevMode) DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX DevMode.dmPaperWidth = Printer.Width DevMode.dmOrientation = Printer.Orientation DevMode.dmPaperSize = Printer.PaperSize DevMode.dmDuplex = Printer.Duplex On Error GoTo 0 t_Printer.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode)) lpDevMode = GlobalLock(t_Printer.hDevMode) If lpDevMode > 0 Then CopyMemory ByVal lpDevMode, DevMode, Len(DevMode) bReturn = GlobalUnlock(t_Printer.hDevMode) End If With DevName .wDriverOffset = 8 .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName) .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port) .wDefault = 0 End With With Printer DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0) End With t_Printer.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName)) lpDevName = GlobalLock(t_Printer.hDevNames) If lpDevName > 0 Then CopyMemory ByVal lpDevName, DevName, Len(DevName) bReturn = GlobalUnlock(lpDevName) End If If PrintDialog(t_Printer) <> 0 Then lpDevName = GlobalLock(t_Printer.hDevNames) CopyMemory DevName, ByVal lpDevName, 45 bReturn = GlobalUnlock(lpDevName) GlobalFree t_Printer.hDevNames lpDevMode = GlobalLock(t_Printer.hDevMode) CopyMemory DevMode, ByVal lpDevMode, Len(DevMode) bReturn = GlobalUnlock(t_Printer.hDevMode) GlobalFree t_Printer.hDevMode NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _ InStr(DevMode.dmDeviceName, Chr$(0)) - 1)) If Printer.DeviceName <> NewPrinterName Then For Each objPrinter In Printers If UCase$(objPrinter.DeviceName) = NewPrinterName Then Set Printer = objPrinter End If Next End If On Error Resume Next With Printer .PaperSize = DevMode.dmPaperSize .PrintQuality = DevMode.dmPrintQuality .ColorMode = DevMode.dmColor .PaperBin = DevMode.dmDefaultSource .Copies = DevMode.dmCopies .Duplex = DevMode.dmDuplex .Orientation = DevMode.dmOrientation End With On Error GoTo 0 End If Exit Sub ErrSub: If Err.Number = 484 Then MsgBox "Error al obtener información de la impresora." & _ "Asegurarse que está instalada correctamente.", vbCritical End If End Sub
Código fuente en el formulario

Texto planoImprimir


  1. Option Explicit
  2. 'Abre el Diálogo Imprimir
  3. Private Sub Command1_Click()
  4. Call Show_Printer(Me)
  5. End Sub
  6. ' Abre el Diálogo de Configurar página
  7. Private Sub Command2_Click()
  8. Call Configuarar_Pagina(Me.hWnd)
  9. End Sub
  10. Private Sub Form_Load()
  11. Command1.Caption = " Show Printer "
  12. Command2.Caption = " Configuarar Pagina "
  13. End Sub
 
Arriba