Results 1 to 4 of 4

Thread: How to change Printer Orientation specially for Dot Matrix Printer?

  1. #1
    brahmankarnikhi
    Guest

    Post How to change Printer Orientation specially for Dot Matrix Printer?

    I want to konw API to change Printer Orientation? How to do it?

  2. #2
    DaoK
    Guest
    VB Code:
    1. 'This project needs 6 command buttons
    2. Option Explicit
    3. Const FW_NORMAL = 400
    4. Const DEFAULT_CHARSET = 1
    5. Const OUT_DEFAULT_PRECIS = 0
    6. Const CLIP_DEFAULT_PRECIS = 0
    7. Const DEFAULT_QUALITY = 0
    8. Const DEFAULT_PITCH = 0
    9. Const FF_ROMAN = 16
    10. Const CF_PRINTERFONTS = &H2
    11. Const CF_SCREENFONTS = &H1
    12. Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
    13. Const CF_EFFECTS = &H100&
    14. Const CF_FORCEFONTEXIST = &H10000
    15. Const CF_INITTOLOGFONTSTRUCT = &H40&
    16. Const CF_LIMITSIZE = &H2000&
    17. Const REGULAR_FONTTYPE = &H400
    18. Const LF_FACESIZE = 32
    19. Const CCHDEVICENAME = 32
    20. Const CCHFORMNAME = 32
    21. Const GMEM_MOVEABLE = &H2
    22. Const GMEM_ZEROINIT = &H40
    23. Const DM_DUPLEX = &H1000&
    24. Const DM_ORIENTATION = &H1&
    25. Const PD_PRINTSETUP = &H40
    26. Const PD_DISABLEPRINTTOFILE = &H80000
    27. Private Type POINTAPI
    28.     x As Long
    29.     y As Long
    30. End Type
    31. Private Type RECT
    32.     Left As Long
    33.     Top As Long
    34.     Right As Long
    35.     Bottom As Long
    36. End Type
    37. Private Type OPENFILENAME
    38.     lStructSize As Long
    39.     hwndOwner As Long
    40.     hInstance As Long
    41.     lpstrFilter As String
    42.     lpstrCustomFilter As String
    43.     nMaxCustFilter As Long
    44.     nFilterIndex As Long
    45.     lpstrFile As String
    46.     nMaxFile As Long
    47.     lpstrFileTitle As String
    48.     nMaxFileTitle As Long
    49.     lpstrInitialDir As String
    50.     lpstrTitle As String
    51.     flags As Long
    52.     nFileOffset As Integer
    53.     nFileExtension As Integer
    54.     lpstrDefExt As String
    55.     lCustData As Long
    56.     lpfnHook As Long
    57.     lpTemplateName As String
    58. End Type
    59. Private Type PAGESETUPDLG
    60.     lStructSize As Long
    61.     hwndOwner As Long
    62.     hDevMode As Long
    63.     hDevNames As Long
    64.     flags As Long
    65.     ptPaperSize As POINTAPI
    66.     rtMinMargin As RECT
    67.     rtMargin As RECT
    68.     hInstance As Long
    69.     lCustData As Long
    70.     lpfnPageSetupHook As Long
    71.     lpfnPagePaintHook As Long
    72.     lpPageSetupTemplateName As String
    73.     hPageSetupTemplate As Long
    74. End Type
    75. Private Type CHOOSECOLOR
    76.     lStructSize As Long
    77.     hwndOwner As Long
    78.     hInstance As Long
    79.     rgbResult As Long
    80.     lpCustColors As String
    81.     flags As Long
    82.     lCustData As Long
    83.     lpfnHook As Long
    84.     lpTemplateName As String
    85. End Type
    86. Private Type LOGFONT
    87.         lfHeight As Long
    88.         lfWidth As Long
    89.         lfEscapement As Long
    90.         lfOrientation As Long
    91.         lfWeight As Long
    92.         lfItalic As Byte
    93.         lfUnderline As Byte
    94.         lfStrikeOut As Byte
    95.         lfCharSet As Byte
    96.         lfOutPrecision As Byte
    97.         lfClipPrecision As Byte
    98.         lfQuality As Byte
    99.         lfPitchAndFamily As Byte
    100.         lfFaceName As String * 31
    101. End Type
    102. Private Type CHOOSEFONT
    103.         lStructSize As Long
    104.         hwndOwner As Long          '  caller's window handle
    105.         hDC As Long                '  printer DC/IC or NULL
    106.         lpLogFont As Long          '  ptr. to a LOGFONT struct
    107.         iPointSize As Long         '  10 * size in points of selected font
    108.         flags As Long              '  enum. type flags
    109.         rgbColors As Long          '  returned text color
    110.         lCustData As Long          '  data passed to hook fn.
    111.         lpfnHook As Long           '  ptr. to hook function
    112.         lpTemplateName As String     '  custom template name
    113.         hInstance As Long          '  instance handle of.EXE that
    114.                                        '    contains cust. dlg. template
    115.         lpszStyle As String          '  return the style field here
    116.                                        '  must be LF_FACESIZE or bigger
    117.         nFontType As Integer          '  same value reported to the EnumFonts
    118.                                        '    call back with the extra FONTTYPE_
    119.                                        '    bits added
    120.         MISSING_ALIGNMENT As Integer
    121.         nSizeMin As Long           '  minimum pt size allowed &
    122.         nSizeMax As Long           '  max pt size allowed if
    123.                                        '    CF_LIMITSIZE is used
    124. End Type
    125. Private Type PRINTDLG_TYPE
    126.     lStructSize As Long
    127.     hwndOwner As Long
    128.     hDevMode As Long
    129.     hDevNames As Long
    130.     hDC As Long
    131.     flags As Long
    132.     nFromPage As Integer
    133.     nToPage As Integer
    134.     nMinPage As Integer
    135.     nMaxPage As Integer
    136.     nCopies As Integer
    137.     hInstance As Long
    138.     lCustData As Long
    139.     lpfnPrintHook As Long
    140.     lpfnSetupHook As Long
    141.     lpPrintTemplateName As String
    142.     lpSetupTemplateName As String
    143.     hPrintTemplate As Long
    144.     hSetupTemplate As Long
    145. End Type
    146. Private Type DEVNAMES_TYPE
    147.     wDriverOffset As Integer
    148.     wDeviceOffset As Integer
    149.     wOutputOffset As Integer
    150.     wDefault As Integer
    151.     extra As String * 100
    152. End Type
    153. Private Type DEVMODE_TYPE
    154.     dmDeviceName As String * CCHDEVICENAME
    155.     dmSpecVersion As Integer
    156.     dmDriverVersion As Integer
    157.     dmSize As Integer
    158.     dmDriverExtra As Integer
    159.     dmFields As Long
    160.     dmOrientation As Integer
    161.     dmPaperSize As Integer
    162.     dmPaperLength As Integer
    163.     dmPaperWidth As Integer
    164.     dmScale As Integer
    165.     dmCopies As Integer
    166.     dmDefaultSource As Integer
    167.     dmPrintQuality As Integer
    168.     dmColor As Integer
    169.     dmDuplex As Integer
    170.     dmYResolution As Integer
    171.     dmTTOption As Integer
    172.     dmCollate As Integer
    173.     dmFormName As String * CCHFORMNAME
    174.     dmUnusedPadding As Integer
    175.     dmBitsPerPel As Integer
    176.     dmPelsWidth As Long
    177.     dmPelsHeight As Long
    178.     dmDisplayFlags As Long
    179.     dmDisplayFrequency As Long
    180. End Type
    181. Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
    182. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    183. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    184. Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
    185. Private Declare Function PAGESETUPDLG Lib "comdlg32.dll" Alias "PageSetupDlgA" (pPagesetupdlg As PAGESETUPDLG) As Long
    186. Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
    187. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    188. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    189. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    190. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    191. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    192. Dim OFName As OPENFILENAME
    193. Dim CustomColors() As Byte
    194. Private Sub Command1_Click()
    195.     Dim sFile As String
    196.     sFile = ShowOpen
    197.     If sFile <> "" Then
    198.         MsgBox "You chose this file: " + sFile
    199.     Else
    200.         MsgBox "You pressed cancel"
    201.     End If
    202. End Sub
    203. Private Sub Command2_Click()
    204.     Dim sFile As String
    205.     sFile = ShowSave
    206.     If sFile <> "" Then
    207.         MsgBox "You chose this file: " + sFile
    208.     Else
    209.         MsgBox "You pressed cancel"
    210.     End If
    211. End Sub
    212. Private Sub Command3_Click()
    213.     Dim NewColor As Long
    214.     NewColor = ShowColor
    215.     If NewColor <> -1 Then
    216.         Me.BackColor = NewColor
    217.     Else
    218.         MsgBox "You chose cancel"
    219.     End If
    220. End Sub
    221. Private Sub Command4_Click()
    222.     MsgBox ShowFont
    223. End Sub
    224. Private Sub Command5_Click()
    225.     ShowPrinter Me
    226. End Sub
    227. Private Sub Command6_Click()
    228.     ShowPageSetupDlg
    229. End Sub

  3. #3
    DaoK
    Guest

    code part 2

    VB Code:
    1. Private Sub Form_Load()
    2.     'KPD-Team 1998
    3.     'URL: [url]http://www.allapi.net/[/url]
    4.     'E-Mail: [email][email protected][/email]
    5.     'Redim the variables to store the cutstom colors
    6.     ReDim CustomColors(0 To 16 * 4 - 1) As Byte
    7.     Dim i As Integer
    8.     For i = LBound(CustomColors) To UBound(CustomColors)
    9.         CustomColors(i) = 0
    10.     Next i
    11.     'Set the captions
    12.     Command1.Caption = "ShowOpen"
    13.     Command2.Caption = "ShowSave"
    14.     Command3.Caption = "ShowColor"
    15.     Command4.Caption = "ShowFont"
    16.     Command5.Caption = "ShowPrinter"
    17.     Command6.Caption = "ShowPageSetupDlg"
    18. End Sub
    19. Private Function ShowColor() As Long
    20.     Dim cc As CHOOSECOLOR
    21.     Dim Custcolor(16) As Long
    22.     Dim lReturn As Long
    23.  
    24.     'set the structure size
    25.     cc.lStructSize = Len(cc)
    26.     'Set the owner
    27.     cc.hwndOwner = Me.hwnd
    28.     'set the application's instance
    29.     cc.hInstance = App.hInstance
    30.     'set the custom colors (converted to Unicode)
    31.     cc.lpCustColors = StrConv(CustomColors, vbUnicode)
    32.     'no extra flags
    33.     cc.flags = 0
    34.  
    35.     'Show the 'Select Color'-dialog
    36.     If CHOOSECOLOR(cc) <> 0 Then
    37.         ShowColor = cc.rgbResult
    38.         CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
    39.     Else
    40.         ShowColor = -1
    41.     End If
    42. End Function
    43. Private Function ShowOpen() As String
    44.     'Set the structure size
    45.     OFName.lStructSize = Len(OFName)
    46.     'Set the owner window
    47.     OFName.hwndOwner = Me.hwnd
    48.     'Set the application's instance
    49.     OFName.hInstance = App.hInstance
    50.     'Set the filet
    51.     OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    52.     'Create a buffer
    53.     OFName.lpstrFile = Space$(254)
    54.     'Set the maximum number of chars
    55.     OFName.nMaxFile = 255
    56.     'Create a buffer
    57.     OFName.lpstrFileTitle = Space$(254)
    58.     'Set the maximum number of chars
    59.     OFName.nMaxFileTitle = 255
    60.     'Set the initial directory
    61.     OFName.lpstrInitialDir = "C:\"
    62.     'Set the dialog title
    63.     OFName.lpstrTitle = "Open File - KPD-Team 1998"
    64.     'no extra flags
    65.     OFName.flags = 0
    66.  
    67.     'Show the 'Open File'-dialog
    68.     If GetOpenFileName(OFName) Then
    69.         ShowOpen = Trim$(OFName.lpstrFile)
    70.     Else
    71.         ShowOpen = ""
    72.     End If
    73. End Function
    74. Private Function ShowFont() As String
    75.     Dim cf As CHOOSEFONT, lfont As LOGFONT, hMem As Long, pMem As Long
    76.     Dim fontname As String, retval As Long
    77.     lfont.lfHeight = 0  ' determine default height
    78.     lfont.lfWidth = 0  ' determine default width
    79.     lfont.lfEscapement = 0  ' angle between baseline and escapement vector
    80.     lfont.lfOrientation = 0  ' angle between baseline and orientation vector
    81.     lfont.lfWeight = FW_NORMAL  ' normal weight i.e. not bold
    82.     lfont.lfCharSet = DEFAULT_CHARSET  ' use default character set
    83.     lfont.lfOutPrecision = OUT_DEFAULT_PRECIS  ' default precision mapping
    84.     lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS  ' default clipping precision
    85.     lfont.lfQuality = DEFAULT_QUALITY  ' default quality setting
    86.     lfont.lfPitchAndFamily = DEFAULT_PITCH Or FF_ROMAN  ' default pitch, proportional with serifs
    87.     lfont.lfFaceName = "Times New Roman" & vbNullChar  ' string must be null-terminated
    88.     ' Create the memory block which will act as the LOGFONT structure buffer.
    89.     hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(lfont))
    90.     pMem = GlobalLock(hMem)  ' lock and get pointer
    91.     CopyMemory ByVal pMem, lfont, Len(lfont)  ' copy structure's contents into block
    92.     ' Initialize dialog box: Screen and printer fonts, point size between 10 and 72.
    93.     cf.lStructSize = Len(cf)  ' size of structure
    94.     cf.hwndOwner = Form1.hwnd  ' window Form1 is opening this dialog box
    95.     cf.hDC = Printer.hDC  ' device context of default printer (using VB's mechanism)
    96.     cf.lpLogFont = pMem   ' pointer to LOGFONT memory block buffer
    97.     cf.iPointSize = 120  ' 12 point font (in units of 1/10 point)
    98.     cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
    99.     cf.rgbColors = RGB(0, 0, 0)  ' black
    100.     cf.nFontType = REGULAR_FONTTYPE  ' regular font type i.e. not bold or anything
    101.     cf.nSizeMin = 10  ' minimum point size
    102.     cf.nSizeMax = 72  ' maximum point size
    103.     ' Now, call the function.  If successful, copy the LOGFONT structure back into the structure
    104.     ' and then print out the attributes we mentioned earlier that the user selected.
    105.     retval = CHOOSEFONT(cf)  ' open the dialog box
    106.     If retval <> 0 Then  ' success
    107.         CopyMemory lfont, ByVal pMem, Len(lfont)  ' copy memory back
    108.         ' Now make the fixed-length string holding the font name into a "normal" string.
    109.         ShowFont = Left(lfont.lfFaceName, InStr(lfont.lfFaceName, vbNullChar) - 1)
    110.         Debug.Print  ' end the line
    111.     End If
    112.     ' Deallocate the memory block we created earlier.  Note that this must
    113.     ' be done whether the function succeeded or not.
    114.     retval = GlobalUnlock(hMem)  ' destroy pointer, unlock block
    115.     retval = GlobalFree(hMem)  ' free the allocated memory
    116. End Function
    117. Private Function ShowSave() As String
    118.     'Set the structure size
    119.     OFName.lStructSize = Len(OFName)
    120.     'Set the owner window
    121.     OFName.hwndOwner = Me.hwnd
    122.     'Set the application's instance
    123.     OFName.hInstance = App.hInstance
    124.     'Set the filet
    125.     OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    126.     'Create a buffer
    127.     OFName.lpstrFile = Space$(254)
    128.     'Set the maximum number of chars
    129.     OFName.nMaxFile = 255
    130.     'Create a buffer
    131.     OFName.lpstrFileTitle = Space$(254)
    132.     'Set the maximum number of chars
    133.     OFName.nMaxFileTitle = 255
    134.     'Set the initial directory
    135.     OFName.lpstrInitialDir = "C:\"
    136.     'Set the dialog title
    137.     OFName.lpstrTitle = "Save File - KPD-Team 1998"
    138.     'no extra flags
    139.     OFName.flags = 0
    140.  
    141.     'Show the 'Save File'-dialog
    142.     If GetSaveFileName(OFName) Then
    143.         ShowSave = Trim$(OFName.lpstrFile)
    144.     Else
    145.         ShowSave = ""
    146.     End If
    147. End Function
    148. Private Function ShowPageSetupDlg() As Long
    149.     Dim m_PSD As PAGESETUPDLG
    150.     'Set the structure size
    151.     m_PSD.lStructSize = Len(m_PSD)
    152.     'Set the owner window
    153.     m_PSD.hwndOwner = Me.hwnd
    154.     'Set the application instance
    155.     m_PSD.hInstance = App.hInstance
    156.     'no extra flags
    157.     m_PSD.flags = 0
    158.  
    159.     'Show the pagesetup dialog
    160.     If PAGESETUPDLG(m_PSD) Then
    161.         ShowPageSetupDlg = 0
    162.     Else
    163.         ShowPageSetupDlg = -1
    164.     End If
    165. End Function

  4. #4
    DaoK
    Guest

    part 3 and last part!!!

    VB Code:
    1. Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
    2.     '-> Code by Donald Grover
    3.     Dim PrintDlg As PRINTDLG_TYPE
    4.     Dim DevMode As DEVMODE_TYPE
    5.     Dim DevName As DEVNAMES_TYPE
    6.  
    7.     Dim lpDevMode As Long, lpDevName As Long
    8.     Dim bReturn As Integer
    9.     Dim objPrinter As Printer, NewPrinterName As String
    10.  
    11.     ' Use PrintDialog to get the handle to a memory
    12.     ' block with a DevMode and DevName structures
    13.  
    14.     PrintDlg.lStructSize = Len(PrintDlg)
    15.     PrintDlg.hwndOwner = frmOwner.hwnd
    16.  
    17.     PrintDlg.flags = PrintFlags
    18.     On Error Resume Next
    19.     'Set the current orientation and duplex setting
    20.     DevMode.dmDeviceName = Printer.DeviceName
    21.     DevMode.dmSize = Len(DevMode)
    22.     DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
    23.     DevMode.dmPaperWidth = Printer.Width
    24.     DevMode.dmOrientation = Printer.Orientation
    25.     DevMode.dmPaperSize = Printer.PaperSize
    26.     DevMode.dmDuplex = Printer.Duplex
    27.     On Error GoTo 0
    28.  
    29.     'Allocate memory for the initialization hDevMode structure
    30.     'and copy the settings gathered above into this memory
    31.     PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
    32.     lpDevMode = GlobalLock(PrintDlg.hDevMode)
    33.     If lpDevMode > 0 Then
    34.         CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
    35.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
    36.     End If
    37.  
    38.     'Set the current driver, device, and port name strings
    39.     With DevName
    40.         .wDriverOffset = 8
    41.         .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
    42.         .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
    43.         .wDefault = 0
    44.     End With
    45.  
    46.     With Printer
    47.         DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
    48.     End With
    49.  
    50.     'Allocate memory for the initial hDevName structure
    51.     'and copy the settings gathered above into this memory
    52.     PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
    53.     lpDevName = GlobalLock(PrintDlg.hDevNames)
    54.     If lpDevName > 0 Then
    55.         CopyMemory ByVal lpDevName, DevName, Len(DevName)
    56.         bReturn = GlobalUnlock(lpDevName)
    57.     End If
    58.  
    59.     'Call the print dialog up and let the user make changes
    60.     If PrintDialog(PrintDlg) <> 0 Then
    61.  
    62.         'First get the DevName structure.
    63.         lpDevName = GlobalLock(PrintDlg.hDevNames)
    64.         CopyMemory DevName, ByVal lpDevName, 45
    65.         bReturn = GlobalUnlock(lpDevName)
    66.         GlobalFree PrintDlg.hDevNames
    67.  
    68.         'Next get the DevMode structure and set the printer
    69.         'properties appropriately
    70.         lpDevMode = GlobalLock(PrintDlg.hDevMode)
    71.         CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
    72.         bReturn = GlobalUnlock(PrintDlg.hDevMode)
    73.         GlobalFree PrintDlg.hDevMode
    74.         NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
    75.         If Printer.DeviceName <> NewPrinterName Then
    76.             For Each objPrinter In Printers
    77.                 If UCase$(objPrinter.DeviceName) = NewPrinterName Then
    78.                     Set Printer = objPrinter
    79.                     'set printer toolbar name at this point
    80.                 End If
    81.             Next
    82.         End If
    83.  
    84.         On Error Resume Next
    85.         'Set printer object properties according to selections made
    86.         'by user
    87.         Printer.Copies = DevMode.dmCopies
    88.         Printer.Duplex = DevMode.dmDuplex
    89.         Printer.Orientation = DevMode.dmOrientation
    90.         Printer.PaperSize = DevMode.dmPaperSize
    91.         Printer.PrintQuality = DevMode.dmPrintQuality
    92.         Printer.ColorMode = DevMode.dmColor
    93.         Printer.PaperBin = DevMode.dmDefaultSource
    94.         On Error GoTo 0
    95.     End If
    96. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width