看到一個VB打印模塊,保存一下_第1頁
看到一個VB打印模塊,保存一下_第2頁
看到一個VB打印模塊,保存一下_第3頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、看到一個 VB 打印模塊 ,保存一下'''' 模塊 modPrintOption ExplicitPublic 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

2、LongPublic Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _(ByVal hPrinter As Long, ByVal Level As Long, pFormAs Byte) As LongPublic Declare Function DeleteForm Lib "winspool.drv"Alias "DeleteFormA" _(ByVal hPrinter As Long, ByVal pFormName As St

3、ring)As LongPublic Declare Function OpenPrinter Lib "winspool.drv" _Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As LongPublic Declare Function ClosePrinter Lib "winspool.drv" _(ByVal hPrinter As Long) As LongPublic Decl

4、are 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 LongPublic Declare Function ResetDC Lib "gdi32"

5、; Alias "ResetDCA" _(ByVal hdc As Long, lpInitData As Any) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA&q

6、uot; _(ByVal lpString1 As String, ByRef lpString2 As Long) As Long'''' Optional functions not used in this sample, but may be useful.Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String,ByVal Level

7、 As Long, pForm As Byte, ByVal cbBuf As Long, _pcbNeeded As Long) As LongPublic Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _(ByVal hPrinter As Long, ByVal pFormName As String,ByVal Level As Long, pForm As Byte) As Long'''' Constants for DEVMODEPu

8、blic Const CCHFORMNAME = 32Public Const CCHDEVICENAME = 32Public Const DM_FORMNAME As Long = &H10000Public Const DM_ORIENTATION = &H1&'''' Constants for PRINTER_DEFAULTS.DesiredAccessPublic Const PRINTER_ACCESS_ADMINISTER = &H4Public Const PRINTER_ACCE

9、SS_USE = &H8Public Const STANDARD_RIGHTS_REQUIRED = &HF0000Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE) '''' Constants for DocumentProperties() call Public Const DM_MODIFY = 8 Public Const DM_IN_BUFFE

10、R = DM_MODIFY Public Const DM_COPY = 2 Public Const DM_OUT_BUFFER = DM_COPY '''' Custom constants for this sample''''s SelectForm function Public Const FORM_NOT_SELECTED = 0 Public Const FORM_SELECTED = 1 Public Const FORM_ADDED = 2 Public Type RECTLLeft As Long Top A

11、s Long Right As Long Bottom As LongEnd TypePublic Type SIZEcx As Longcy As LongEnd TypePublic Type SECURITY_DESCRIPTORRevision As ByteSbz1 As ByteControl As LongOwner As LongGroup As LongSacl As Long '''' ACLDacl As Long '''' ACLEnd Type'''' The two de

12、finitions for FORM_INFO_1 make the coding easier.Public Type FORM_INFO_1Flags As LongpName As Long'''' StringSize As SIZEL ImageableArea As RECTLEnd TypePublic Type sFORM_INFO_1Flags As Long pName As String Size As SIZEL ImageableArea As RECTLEnd TypePublic Type DEVMODEdmDeviceName A

13、s 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

14、dmPrintQuality As Integer dmColor As Integer dmDuplex As IntegerdmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * CCHFORMNAME dmUnusedPadding As Integer dmBitsPerPel As Long dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As

15、 LongEnd TypePublic Type PRINTER_DEFAULTSpDatatype As String pDevMode As Long '''' DEVMODE DesiredAccess As LongEnd TypePublic Type PRINTER_INFO_2 pServerName As String pPrinterName As String pShareName As String pPortName As String pDriverName As StringpComment As String pLocation A

16、s String pDevMode As DEVMODE pSepFile As String pPrintProcessor As String pDatatype As String pParameters As String pSecurityDescriptor As SECURITY_DESCRIPTORAttributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long cJobs As Long AveragePPM As Lon

17、g End Type '''' 判斷系統(tǒng)是否為 NT 系統(tǒng) Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _(lpVersionInformation AsOSVERSIONINFO) As Long Public Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumbe

18、r As Long dwPlatformId As Long szCSDVersion As String * 128 '''' Maintenance string for PSS usageEnd TypePublic Function SelectForm(FormName As String, ByValMyhWnd As Long) _As Integer'''' Size of DEVMODE'''' Handle to printer'''' Handl

19、e to Printer DC'''' Working DEVMODEDim nSize As LongDim pDevMode As DEVMODEDim PrinterHandle As LongDim hPrtDC As LongDim PrinterName As StringDim aDevMode() As ByteDim FormSize As SIZELDim tmpFormName As StringDim isNT As BooleanDim PaperWidth As Long, PaperHeight As Long''&

20、#39;'在這里統(tǒng)一添加自定義紙張Select Case FormNameCase " 廣東省發(fā)票 "With FormSize'''' Desired page size.cx = 186000.cy = 102000End WithPaperWidth = 10544 ''''15.6cmPaperHeight = 5784 ''''10.2cmCase "80 列報表 "With FormSize'''' Desi

21、red page size.cx = 210000.cy = 280000End WithPaperWidth = 11904 ''''21cmPaperHeight = 15872 ''''28cmCase "40 列報表 "With FormSize'''' Desired page size.cx = 210000.cy = 140000End WithPaperWidth = 11904 ''''21cmPaperHeight = 7936

22、 ''''14cmCase "20 列報表 "With FormSize'''' Desired page size.cx = 210000.cy = 70000End WithPaperWidth = 11904 ''''21cmPaperHeight = 4008 ''''7cmEnd Select''''不是 NT 直接給 Printer 賦值得了If Not IsNtOs ThenPrinter.Width

23、= PaperWidthPrinter.Height = PaperHeight SelectForm = FORM_SELECTEDExit FunctionEnd IfPrinterName = Printer.DeviceName'''' Current printerhPrtDC = Printer.hdc'''' hDC for currentPrinterSelectForm = FORM_NOT_SELECTED '''' Set for failure unless reset in

24、 code.'''' Get a handle to the printer.If OpenPrinter(PrinterName, PrinterHandle, 0&)Then'''' Retrieve the size of the DEVMODE.nSize = DocumentProperties(MyhWnd,PrinterHandle, PrinterName, 0&, _0&, 0&)'''' Reserve memory

25、 for the actual size of the DEVMODE.ReDim aDevMode(1 To nSize)'''' Fill the DEVMODE from the printer.nSize = DocumentProperties(MyhWnd,PrinterHandle, PrinterName, _aDevMode(1), 0&,DM_OUT_BUFFER)'''' Copy the Public (predefined) portion of the DEVMODE.Call Copy

26、Memory(pDevMode, aDevMode(1), Len(pDevMode)'''' If FormName is "MyCustomForm", we must make sure it exists'''' before using it. Otherwise, it came from our EnumForms list,'''' and we do not need to check first. Note that we could have'

27、9;'' passed in a Flag instead of checking for a literal name.tmpFormName = FormNameIf GetFormName(PrinterHandle, FormSize, tmpFormName) = 0 Then'''' Form not found - Either of the next 2 lines will work.''''FormName = AddNewForm(PrinterHandle,FormSize, "M

28、yCustomForm")AddNewForm PrinterHandle, FormSize,FormNameIf GetFormName(PrinterHandle, FormSize, FormName) = 0 ThenClosePrinter (PrinterHandle)SelectForm = FORM_NOT_SELECTED '''' Selection Failed!Exit FunctionElseSelectForm = FORM_ADDED '''' Form Added, Selection

29、succeeded!End IfEnd If'''' Change the appropriate member in the DevMode. '''' In this case, you want to change the form name. pDevMode.dmFormName = FormName &Chr(0) '''' Must be NULL terminated!'''' Set the dmFields bit flag to

30、indicate what you are changing.pDevMode.dmFields = DM_FORMNAME'''' Copy your changes back, then update DEVMODE.Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode)nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)n

31、Size = ResetDC(hPrtDC, aDevMode(1) Reset the DEVMODE for the DC.'''' Close the handle when you are finished with it.ClosePrinter (PrinterHandle)'''' Selection Succeeded! But was Form Added?If SelectForm <> FORM_ADDED ThenSelectForm = FORM_SELECTEDElseHHS

32、electForm = FORM_NOT_SELECTEDSelection Failed!End IfEnd FunctionPublic Function GetFormName(ByVal PrinterHandle As Long, _FormSize As SIZEL,FormName As String) As IntegerDim NumForms As Long, I As LongDim FI1 As FORM_INFO_1Dim aFI1() As FORM_INFO_1 '''' Working FI1 arrayDim Temp() As

33、 Byte'''' Temp FI1arrayDim FormIndex As IntegerDim BytesNeeded As LongDim RetVal As LongFormName = vbNullStringFormIndex = 0ReDim aFI1(1)'''' First call retrieves the BytesNeeded.RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&, BytesNeeded, NumForms)ReDim Tem

34、p(BytesNeeded)ReDim aFI1(BytesNeeded / Len(FI1)'''' Second call actually enumerates the supported forms.RetVal = EnumForms(PrinterHandle, 1, Temp(0), BytesNeeded, BytesNeeded, _NumForms)Call CopyMemory(aFI1(0), Temp(0), BytesNeeded)For I = 0 To NumForms - 1With aFI1(I)If .Size.cx = F

35、ormSize.cx And .Size.cy =FormSize.cy Then'''' Found the desired formFormName = PtrCtoVbString(.pName) FormIndex = I + 1Exit ForEnd IfEnd WithNext IGetFormName = FormIndex '''' Returns non-zero when form is found.End FunctionPublic Function AddNewForm(PrinterHandle As

36、Long, FormSize As SIZEL, _FormName As String) AsStringDim FI1 As sFORM_INFO_1Dim aFI1() As ByteDim RetVal As LongWith FI1.Flags = 0.pName = FormNameWith .Size.cx = FormSize.cx.cy = FormSize.cyEnd WithWith .ImageableArea.Left = 0.Top = 0.Right = .Bottom = End WithEnd WithReDim aFI1(Len(FI1)Call CopyM

37、emory(aFI1(0), FI1, Len(FI1) RetVal = AddForm(PrinterHandle, 1, aFI1(0)If RetVal = 0 ThenIf Err.LastDllError = 5 ThenMsgBox "You do not have permissions to add a form to " & _Printer.DeviceName, vbExclamation, "Access Denied!"ElseMsgBox "Error: " & Err.L

38、astDllError, , "Error Adding Form"''''MsgBox "Error: " & Err.LastDllError, "Error Adding Form"End IfAddNewForm = "none"ElseAddNewForm = FI1.pNameEnd IfEnd FunctionPublic Sub PrintTest()'''' Print two test pages to confir

39、m the page size. Printer.Print "Top of Page 1."Printer.NewPage'''' Spacing between lines should reflect the chosen pageheight.Printer.Print "Top of Page 2. - Check the page Height (Length.)"Printer.EndDocMsgBox "Check Printer " & Printer.DeviceNa

40、me, vbInformation, "Done!"End SubPublic Function PtrCtoVbString(ByVal Add As Long) As StringDim sTemp As String * 512, X As LongX = lstrcpy(sTemp, ByVal Add)If (InStr(1, sTemp, Chr(0) = 0) Then PtrCtoVbString = ""ElsePtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0) - 1)End IfEnd FunctionPublic Function IsNtOs() As BooleanDim ver As OSVERSIONINFOver.dwOSVersionInfoSize = 148 GetVersionEx ver With verSelect Case .dwPlatformIdCase 0, 1IsNtOs = FalseCase 2IsNtOs = TrueEnd SelectEnd WithEnd FunctionPublic Function UseForm(FormName As String, sHwnd A

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論