#2
东海ECS2023-04-24 18:56
|
程序代码:
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long
Declare Function StartPage Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function EndPage Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As Long
Public Const MM_TEXT = 1
Public Const WS_EX_CLIENTEDGE = &H200&
Public Enum EPrintDialog
PD_ALLPAGES = &H0
PD_SELECTION = &H1
PD_PAGENUMS = &H2
PD_NOSELECTION = &H4
PD_NOPAGENUMS = &H8
PD_COLLATE = &H10
PD_PRINTTOFILE = &H20
PD_PRINTSETUP = &H40
PD_NOWARNING = &H80
PD_RETURNDC = &H100
PD_RETURNIC = &H200
PD_RETURNDEFAULT = &H400
PD_SHOWHELP = &H800
PD_ENABLEPRINTHOOK = &H1000
PD_ENABLESETUPHOOK = &H2000
PD_ENABLEPRINTTEMPLATE = &H4000
PD_ENABLESETUPTEMPLATE = &H8000
PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
PD_ENABLESETUPTEMPLATEHANDLE = &H20000
PD_USEDEVMODECOPIES = &H40000
PD_USEDEVMODECOPIESANDCOLLATE = &H40000
PD_DISABLEPRINTTOFILE = &H80000
PD_HIDEPRINTTOFILE = &H100000
PD_NONETWORKBUTTON = &H200000
End Enum
Type PrintDlg
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
Type DOCINFO
cbSize As Long
lpszDocName As Long
lpszOutput As Long
End Type
Public Declare Function PrintDlg Lib "comdlg32.dll" Alias "PrintDlgA" (prtdlg As PrintDlg) As Long
Public Sub PrintPicDC( )
Dim pd As PrintDlg
pd.lStructSize = Len(pd) '// 结构体大小
pd.hwndOwner = form1.hwnd '//<strong>HWND,拥有该设置对话框窗口的句柄,当该对话框所属窗体时,可以为NULL</strong>
pd.hDevMode = 0 '结构的可移动全局内存对象句柄
pd.hDevNames = 0
pd.nFromPage = 0 '//起始页编辑控件初始值
pd.nToPage = 0 '/结束页编辑控件初始值
pd.nMinPage = 0 '//起始页编辑控件最小值(用来控制nFromPage)
pd.nMaxPage = 0 '//结束页编辑控件最大值(用来控制nToPage)
pd.nCopies = 0 ' //打印份数
pd.hInstance = App.hInstance
pd.Flags = PD_RETURNDC Or PD_NOSELECTION Or PD_PRINTSETUP '初始化打印对话框,当对话框返回时,将标志设置为用户输入,可以是一个或多这个
pd.lpfnSetupHook = 0
pd.lpSetupTemplateName = 0
pd.lpfnPrintHook = 0
pd.lpPrintTemplateName = 0
If (PrintDlg(pd) <> 0) Then '//获取打印机DC
PrintDoc pd.hdc
End If
End Sub
Private Sub PrintDoc(lPrinterHDC As Long)
Dim i As Long, j As Long
Dim B() As Byte
Dim di As DOCINFO
B = StrConv("ABC", vbFromUnicode)
ReDim Preserve B(0 To UBound(B) + 1) As Byte
di.lpszDocName = VarPtr(B(0))
di.cbSize = Len(di)
di.lpszOutput = 0
Call SetBkMode(lPrinterHDC, WS_EX_CLIENTEDGE)
Call StartDoc(lPrinterHDC, di)
Call StartPage(lPrinterHDC) ' //打印机走纸,开始打印
Call SaveDC(lPrinterHDC) ' //保存打印机设备句柄
SetMapMode lPrinterHDC, MM_TEXT
TextOut lPrinterHDC,50, 80, "Word", LenB(StrConv("Word", vbFromUnicode))
RestoreDC lPrinterHDC, -1
EndPage lPrinterHDC ' //打印机停纸,停止打印
EndDoc lPrinterHDC '//结束一个打印作业
DeleteDC lPrinterHDC '/ 用API函数DeleteDC销毁一个打印机设备句柄
End Sub
以上可以打印,但是就是字符打印太小,不知道的?谢谢!