NonSoft

Win32Apiによる印刷のサンプル(VB6)


 サンプル(ソース/コード)
<このサンプルの概要>
以下のWin32Apiを使用したサンプルソースです。
(1)PrintDlg
(2)AbortDoc
(3)StartDoc
(4)StartPage
(5)TextOut
(6)EndPage
(7)EndDoc
(8)MulDiv
(9)GetDeviceCaps
(10)CreateFont
(11)SelectObject

VB.NETでApiを宣言(Declare)する場合はVB.NET用に変換する必要があります。
変換方法のサンプルはこちらです。VB6とVB.NETのAPI宣言(Declare)の違いを比較したサンプル

C#.NETでApiを宣言(Declare)する場合はC#.NET用に変換する必要があります。
変換方法のサンプルはこちらです。VB6とC#.NETのAPI宣言(Declare)の違いを比較したサンプル

その他のサンプルソースはこちらです。
Win32Api定義のサンプル サンプルソース

Public Const LOGPIXELSX = 88
Public Const LOGPIXELSY = 90

Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

Public Const PD_ALLPAGES = &H0
Public Const PD_SELECTION = &H1
Public Const PD_PAGENUMS = &H2
Public Const PD_NOSELECTION = &H4
Public Const PD_NOPAGENUMS = &H8
Public Const PD_COLLATE = &H10
Public Const PD_PRINTTOFILE = &H20
Public Const PD_PRINTSETUP = &H40
Public Const PD_NOWARNING = &H80
Public Const PD_RETURNDC = &H100
Public Const PD_RETURNIC = &H200
Public Const PD_RETURNDEFAULT = &H400
Public Const PD_SHOWHELP = &H800

Type Size
        cx As Long
        cy As Long
End Type

Type DOCINFO
    cbSize As Long
    lpszDocName As String
    lpszOutput As String
End Type

Type DEVMODE
    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

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

Declare Function PrintDlg Lib "comdlg32.dll" Alias "PrintDlgA" _
    (pPrintdlg As PrintDlg) As Long
Declare Function AbortDoc Lib "gdi32" _
    (ByVal hdc 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 TextOut Lib "gdi32" Alias "TextOutA" _
    (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, _
     ByVal lpString As String, ByVal nCount 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
Declare Function MulDiv Lib "kernel32" _
    (ByVal nNumber As Long, ByVal nNumerator As Long, _
     ByVal nDenominator As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" _
    (ByVal hdc As Long, ByVal nIndex As Long) As Long
Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" _
    (ByVal H As Long, ByVal W As Long, ByVal E As Long, _
     ByVal O As Long, ByVal W As Long, ByVal i As Long, _
     ByVal u As Long, ByVal S As Long, ByVal C As Long, _
     ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, _
     ByVal PAF As Long, ByVal F As String) As Long
Declare Function SelectObject Lib "gdi32" _
    (ByVal hdc As Long, ByVal hObject As Long) As Long

Public Sub SampleModule()
    Dim lngHeig As Long
    Dim lngFont As Long
    Dim lngPrDC As Long
    Dim lngRslt As Long
    Dim typDocInfo As DOCINFO
    Dim typDevMode As DEVMODE
    Dim typPrintDlg As PrintDlg
    
    ' プリンタを選択します
    typPrintDlg.lStructSize = 66
    typPrintDlg.Flags = PD_RETURNDC
    lngRslt = PrintDlg(typPrintDlg)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        Exit Sub
    End If
    
    ' 印刷全体の開始を宣言します
    typDocInfo.cbSize = 12
    typDocInfo.lpszDocName = "文書のタイトル"
    lngPrDC = typPrintDlg.hdc
    lngRslt = StartDoc(lngPrDC, typDocInfo)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        Exit Sub
    End If
    
    ' ページ毎の開始を宣言します
    lngRslt = StartPage(lngPrDC)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        AbortDoc (lngPrDC)
        Exit Sub
    End If
    
    ' フォントを指定します
    lngHeig = -MulDiv(20, GetDeviceCaps(lngPrDC, LOGPIXELSY), 72)
    lngFont = CreateFont(lngHeig, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, _
                         &H1 Or &H30, "MS Pゴシック")
    lngFont = SelectObject(lngPrDC, lngFont)
    
    ' 文字を印刷します
    lngRslt = TextOut(lngPrDC, 0, 0, "あいうえお", 10)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        AbortDoc (lngPrDC)
        Exit Sub
    End If
    
    ' ページ毎の終了を宣言します
    lngRslt = EndPage(lngPrDC)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        AbortDoc (lngPrDC)
        Exit Sub
    End If
    
    ' 印刷全体の終了を宣言します
    lngRslt = EndDoc(lngPrDC)
    If lngRslt <= 0 Then
        MsgBox Err.Description
        AbortDoc (lngPrDC)
        Exit Sub
    End If
End Sub