| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 703 人关注过本帖
标题:VB6知道打印机的HDC,如何打印
只看楼主 加入收藏
hahjs
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-2-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
VB6知道打印机的HDC,如何打印
获得打印机的HDC
程序代码:
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


以上可以打印,但是就是字符打印太小,不知道的?谢谢!













搜索更多相关主题的帖子: Long Lib 打印机 打印 Function 
2023-04-24 11:32
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:20 
可以尝试修改打印文本的字体大小。可以在PrintDoc子程序中添加以下代码:

程序代码:
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)

' 添加以下两行代码,设置打印文本的字体和大小
Dim fnt As Long
fnt = CreateFont(24, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, _
                 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, _
                 DEFAULT_QUALITY, FIXED_PITCH, "Courier New")
SelectObject lPrinterHDC, fnt

SetMapMode lPrinterHDC, MM_TEXT
TextOut lPrinterHDC, 50, 80, "Word", LenB(StrConv("Word", vbFromUnicode))
RestoreDC lPrinterHDC, -1
EndPage lPrinterHDC
EndDoc lPrinterHDC
DeleteDC lPrinterHDC
End Sub


在代码中,CreateFont函数创建了一个字体对象,参数24设置字体大小为24pt,可以根据需要调整。然后使用SelectObject函数将新创建的字体对象选择到打印机的HDC中,从而实现打印文本的字体和大小修改。




会当凌绝顶,一览众山小.
2023-04-24 18:56
hahjs
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-2-20
收藏
得分:0 
先要感谢 东海ECS 的回复,多谢!

前边的问题,可能是我没有说清楚,我做了一个文本框的控件,每个字符的LOGFONT都有,在屏幕显示正常,将字体输出到打印机时就变小了。

昨天,终于发现大小字体的比例是屏幕的DPI与打印机的DPI “GetDeviceCaps(lPrinterHDC, LOGPIXELSX)”之比。
不过,一者这是我测试的,不知道是否正确;二者每个字符在打印前都要将LOGFONT进行转换,打印后再转换过来,有点繁,精度也存在问题。不知是否有便捷的方法。
2023-04-28 08:42
快速回复:VB6知道打印机的HDC,如何打印
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032125 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved