プリンタの対応する用紙を取得          <TOP>


接続されているプリンタを列挙し、選択したプリンタの使用できる用紙の種類を取得します。

EnumPrinters 使用可能なプリンタ・プリントサーバーなどを列挙

RtlMoveMemory メモリブロックを別の領域に移動

DeviceCapabilities プリンタデバイスドライバの能力を取得

 

接続されているプリンタ名およびポート名がコンボボックスにリスト表示されます。

リスト行を選択し取得ボタンをクリックすることにより使用できる用紙の種類が表示されます。

 

 

 

'================================================================
'= 接続されているプリンタの使用できる用紙取得
'=    (DeviceCapabilities.bas)
'================================================================
#include "Windows.bi"

Type PRINTER_INFO_5
    pPrinterName As Long
    pPortName    As Long
    Attributes   As Long
    DeviceNotSelectedTimeOut As Long
    TransmissionRetryTimeOut As Long
End Type

' 使用可能なプリンタ・プリントサーバーなどを列挙する
Declare Function Api_EnumPrinters& Lib "winspool.drv" Alias "EnumPrintersA" (ByVal Flags& , ByVal Name$ , ByVal Level& , pPrinterEnum As Any , ByVal cbBuf& , pcbNeeded& , pcReturned& )

#define PRINTER_ENUM_NAME &H8
#define MAX_DEVICENAME 64

' メモリブロックを別の領域に移動する
Declare Sub Api_MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)

' プリンタデバイスドライバの能力を取得する関数の宣言
Declare Function Api_DeviceCapabilities& Lib "winspool.drv" Alias "DeviceCapabilitiesA" (ByVal lpDeviceName$, ByVal lpPort$, ByVal iIndex&, lpOutput As Any, lpDevMode As Any)

' プリンタで使用できる用紙を取得することを示す定数の宣言
#define DC_PAPERS 2

' 用紙を示す定数の宣言
#define DMPAPER_LETTER 1
#define DMPAPER_LETTERSMALL 2
#define DMPAPER_TABLOID 3
#define DMPAPER_LEDGER 4
#define DMPAPER_LEGAL 5
#define DMPAPER_STATEMENT 6
#define DMPAPER_EXECUTIVE 7
#define DMPAPER_A3 8
#define DMPAPER_A4 9
#define DMPAPER_A4SMALL 10
#define DMPAPER_A5 11
#define DMPAPER_B4 12
#define DMPAPER_B5 13
#define DMPAPER_FOLIO 14
#define DMPAPER_QUARTO 15
#define DMPAPER_10X14 16
#define DMPAPER_11X17 17
#define DMPAPER_NOTE 18
#define DMPAPER_ENV_9 19
#define DMPAPER_ENV_10 20
#define DMPAPER_ENV_11 21
#define DMPAPER_ENV_12 22
#define DMPAPER_ENV_14 23
#define DMPAPER_CSHEET 24
#define DMPAPER_DSHEET 25
#define DMPAPER_ESHEET 26
#define DMPAPER_ENV_DL 27
#define DMPAPER_ENV_C5 28
#define DMPAPER_ENV_C3 29
#define DMPAPER_ENV_C4 30
#define DMPAPER_ENV_C6 31
#define DMPAPER_ENV_C65 32
#define DMPAPER_ENV_B4 33
#define DMPAPER_ENV_B5 34
#define DMPAPER_ENV_B6 35
#define DMPAPER_ENV_ITALY 36
#define DMPAPER_ENV_MONARCH 37
#define DMPAPER_ENV_PERSONAL 38
#define DMPAPER_FANFOLD_US 39
#define DMPAPER_FANFOLD_STD_GERMAN 40
#define DMPAPER_FANFOLD_LGL_GERMAN 41
#define DMPAPER_ISO_B4 42
#define DMPAPER_JAPANESE_POSTCARD 43
#define DMPAPER_9X11 44
#define DMPAPER_10X11 45
#define DMPAPER_15X11 46
#define DMPAPER_ENV_INVITE 47
#define DMPAPER_RESERVED_48 48
#define DMPAPER_RESERVED_49 49
#define DMPAPER_LETTER_EXTRA 50
#define DMPAPER_LEGAL_EXTRA 51
#define DMPAPER_TABLOID_EXTRA 52
#define DMPAPER_A4_EXTRA 53
#define DMPAPER_LETTER_TRANSVERSE 54
#define DMPAPER_A4_TRANSVERSE 55
#define DMPAPER_LETTER_EXTRA_TRANSVERSE 56
#define DMPAPER_A_PLUS 57
#define DMPAPER_B_PLUS 58
#define DMPAPER_LETTER_PLUS 59
#define DMPAPER_A4_PLUS 60
#define DMPAPER_A5_TRANSVERSE 61
#define DMPAPER_B5_TRANSVERSE 62
#define DMPAPER_A3_EXTRA 63
#define DMPAPER_A5_EXTRA 64
#define DMPAPER_B5_EXTRA 65
#define DMPAPER_A2 66
#define DMPAPER_A3_TRANSVERSE 67
#define DMPAPER_A3_EXTRA_TRANSVERSE 68
#define DMPAPER_DBL_JAPANESE_POSTCARD 69
#define DMPAPER_A6 70
#define DMPAPER_JENV_KAKU2 71
#define DMPAPER_JENV_KAKU3 72
#define DMPAPER_JENV_CHOU3 73
#define DMPAPER_JENV_CHOU4 74
#define DMPAPER_LETTER_ROTATED 75
#define DMPAPER_A3_ROTATED 76
#define DMPAPER_A4_ROTATED 77
#define DMPAPER_A5_ROTATED 78
#define DMPAPER_B4_JIS_ROTATED 79
#define DMPAPER_B5_JIS_ROTATED 80
#define DMPAPER_JAPANESE_POSTCARD_ROTATED 81
#define DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED 82
#define DMPAPER_A6_ROTATED 83
#define DMPAPER_JENV_KAKU2_ROTATED 84
#define DMPAPER_JENV_KAKU3_ROTATED 85
#define DMPAPER_JENV_CHOU3_ROTATED 86
#define DMPAPER_JENV_CHOU4_ROTATED 87
#define DMPAPER_B6_JIS 88
#define DMPAPER_B6_JIS_ROTATED 89
#define DMPAPER_12X11 90
#define DMPAPER_JENV_YOU4 91
#define DMPAPER_JENV_YOU4_ROTATED 92
#define DMPAPER_P16K 93
#define DMPAPER_P32K 94
#define DMPAPER_P32KBIG 95
#define DMPAPER_PENV_1 96
#define DMPAPER_PENV_2 97
#define DMPAPER_PENV_3 98
#define DMPAPER_PENV_4 99
#define DMPAPER_PENV_5 100
#define DMPAPER_PENV_6 101
#define DMPAPER_PENV_7 102
#define DMPAPER_PENV_8 103
#define DMPAPER_PENV_9 104
#define DMPAPER_PENV_10 105
#define DMPAPER_P16K_ROTATED 106
#define DMPAPER_P32K_ROTATED 107
#define DMPAPER_P32KBIG_ROTATED 108
#define DMPAPER_PENV_1_ROTATED 109
#define DMPAPER_PENV_2_ROTATED 110
#define DMPAPER_PENV_3_ROTATED 111
#define DMPAPER_PENV_4_ROTATED 112
#define DMPAPER_PENV_5_ROTATED 113
#define DMPAPER_PENV_6_ROTATED 114
#define DMPAPER_PENV_7_ROTATED 115
#define DMPAPER_PENV_8_ROTATED 116
#define DMPAPER_PENV_9_ROTATED 117
#define DMPAPER_PENV_10_ROTATED 118
#define DMPAPER_USER 256 

Var Shared List1 As Object
Var Shared Combo1 As Object
Var Shared Text(5) As Object

Combo1.Attach GetDlgItem("Combo1")
Combo1.SetFontSize 14
List1.Attach GetDlgItem("List1")
List1.SetFontSize 14
For i = 0 To 5
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i+1)))
    Text(i).SetFontSize 14
Next i

Var Shared PrinterName$ As String
Var Shared PortName$ As String

'================================================================
'=
'================================================================
Declare Sub Mainform_Start edecl ()
Sub Mainform_Start()
    Var PrintServer$ As String
    Var Need As Long
    Var Returned As Long
    Var Level As Long
    Var CT As Long
    Var Ret As Long

    'ローカルプリンタから検索(サーバー名 \\xxx 指定可)
    PrintServer$ = ""

    'PRINTER_INFO_5構造体を受け取る
    Level = 5

    'バッファに必要なバイト数を調べる
    Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrintServer$, Level, Chr$(0), 0, Need, Returned)
    If Need = 0 Then End

    '全プリンタ情報を得る
    Var Buffer(Need-1) As Byte
    Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrintServer$, Level, Buffer(0), Need, Need, Returned)

    '構造体リストの準備
    Var PI_5(Returned-1) As PRINTER_INFO_5

    For CT = 0 To Returned - 1

        'バッファから構造体1つ分を抜き取る
        Api_MoveMemory PI_5(CT), Buffer(CT * Len(PI_5(CT))), Len(PI_5(CT))

        'プリンタ名を得る
        PrinterName$ = String$(MAX_DEVICENAME, Chr$(0))
        Api_MoveMemory PrinterName$, ByVal PI_5(CT).pPrinterName, Len(PrinterName$)
        PrinterName$ = kLeft$(PrinterName$, kInStr(1, PrinterName$, Chr$(0)) - 1)

        'ポート名を得る
        PortName$ = String$(MAX_DEVICENAME, Chr$(0))
        Api_MoveMemory PortName$ , ByVal PI_5( CT ).pPortName, Len( PORTNAME$ )
        PortName$ = kLeft$(PortName$, kInStr(1, PortName$, Chr$(0)) - 1)
        Combo1.AddString Left$(PrinterName$ & Space$(16), 16) & PortName$
    Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Combo1_Change edecl ()
Sub Combo1_Change()
    PrinterName$ = Trim$(Left$(Combo1.GetText(Combo1.GetCursel), 16))
    PortName$ = Trim$(Mid$(Combo1.GetText(Combo1.GetCursel), 17))
    Text(4).SetWindowText PrinterName$
    Text(5).SetWindowText PortName$
    List1.ResetContent
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Device As Long
    Var Need As Long
    Var Ret As Long
    Var CT As Integer

    List1.ResetContent
    Device = DC_PAPERS

    'バッファに必要なサイズを取得
    Need = Api_DeviceCapabilities(PrinterName$, PortName$, Device, ByVal 0, ByVal 0)

    'バッファを確保
    Var Paper(Need-1) As Integer

    '使用できる用紙を取得
    Ret = Api_DeviceCapabilities(PrinterName$, PortName$, Device, Paper(0), ByVal 0)

    '使用できる給紙方法を列挙
    For CT = 0 To Need-1
        '用紙を表示
        Select Case Paper(CT)
            Case DMPAPER_LETTER
                List1.AddString "LETTER"
            Case DMPAPER_LETTERSMALL
                List1.AddString "LETTERSMALL"
            Case DMPAPER_TABLOID
                List1.AddString "TABLOID"
            Case DMPAPER_LEDGER
                List1.AddString "LEDGER"
            Case DMPAPER_LEGAL
                List1.AddString "LEGAL"
            Case DMPAPER_STATEMENT
                List1.AddString "STATEMENT"
            Case DMPAPER_EXECUTIVE
                List1.AddString "EXECUTIVE"
            Case DMPAPER_A3
                List1.AddString "A3"
            Case DMPAPER_A4
                List1.AddString "A4"
            Case DMPAPER_A4SMALL
                List1.AddString "A4SMALL"
            Case DMPAPER_A5
                List1.AddString "A5"
            Case DMPAPER_B4
                List1.AddString "B4"
            Case DMPAPER_B5
                List1.AddString "B5"
            Case DMPAPER_FOLIO
                List1.AddString "FOLIO"
            Case DMPAPER_QUARTO
                List1.AddString "QUARTO"
            Case DMPAPER_10X14
                List1.AddString "10X14"
            Case DMPAPER_11X17
                List1.AddString "11X17"
            Case DMPAPER_NOTE
                List1.AddString "NOTE"
            Case DMPAPER_ENV_9
                List1.AddString "ENV_9"
            Case DMPAPER_ENV_10
                List1.AddString "ENV_10"
            Case DMPAPER_ENV_11
                List1.AddString "ENV_11"
            Case DMPAPER_ENV_12
                List1.AddString "ENV_12"
            Case DMPAPER_ENV_14
                List1.AddString "ENV_14"
            Case DMPAPER_CSHEET
                List1.AddString "CSHEET"
            Case DMPAPER_DSHEET
                List1.AddString "DSHEET"
            Case DMPAPER_ESHEET
                List1.AddString "ESHEET"
            Case DMPAPER_ENV_DL
                List1.AddString "ENV_DL"
            Case DMPAPER_ENV_C5
                List1.AddString "ENV_C5"
            Case DMPAPER_ENV_C3
                List1.AddString "ENV_C3"
            Case DMPAPER_ENV_C4
                List1.AddString "ENV_C4"
            Case DMPAPER_ENV_C6
                List1.AddString "ENV_C6"
            Case DMPAPER_ENV_C65
                List1.AddString "ENV_C65"
            Case DMPAPER_ENV_B4
                List1.AddString "ENV_B4"
            Case DMPAPER_ENV_B5
                List1.AddString "ENV_B5"
            Case DMPAPER_ENV_B6
                List1.AddString "ENV_B6"
            Case DMPAPER_ENV_ITALY
                List1.AddString "ENV_ITALY"
            Case DMPAPER_ENV_MONARCH
                List1.AddString "ENV_MONARCH"
            Case DMPAPER_ENV_PERSONAL
                List1.AddString "ENV_PERSONAL"
            Case DMPAPER_FANFOLD_US
                List1.AddString "FANFOLD_US"
            Case DMPAPER_FANFOLD_STD_GERMAN
                List1.AddString "FANFOLD_STD_GERMAN"
            Case DMPAPER_FANFOLD_LGL_GERMAN
                List1.AddString "FANFOLD_LGL_GERMAN"
            Case DMPAPER_ISO_B4
                List1.AddString "ISO_B4"
            Case DMPAPER_JAPANESE_POSTCARD
                List1.AddString "JAPANESE_POSTCARD"
            Case DMPAPER_9X11
                List1.AddString "9X11"
            Case DMPAPER_10X11
                List1.AddString "10X11"
            Case DMPAPER_15X11
                List1.AddString "15X11"
            Case DMPAPER_ENV_INVITE
                List1.AddString "ENV_INVITE"
            Case DMPAPER_RESERVED_48
                List1.AddString "RESERVED_48"
            Case DMPAPER_RESERVED_49
                List1.AddString "RESERVED_49"
            Case DMPAPER_LETTER_EXTRA
                List1.AddString "LETTER_EXTRA"
            Case DMPAPER_LEGAL_EXTRA
                List1.AddString "LEGAL_EXTRA"
            Case DMPAPER_TABLOID_EXTRA
                List1.AddString "TABLOID_EXTRA"
            Case DMPAPER_A4_EXTRA
                List1.AddString "A4_EXTRA"
            Case DMPAPER_LETTER_TRANSVERSE
                List1.AddString "LETTER_TRANSVERSE"
            Case DMPAPER_A4_TRANSVERSE
                List1.AddString "A4_TRANSVERSE"
            Case DMPAPER_A_PLUS
                List1.AddString "A_PLUS"
            Case DMPAPER_B_PLUS
                List1.AddString "B_PLUS"
            Case DMPAPER_LETTER_PLUS
                List1.AddString "LETTER_PLUS"
            Case DMPAPER_A4_PLUS
                List1.AddString "A4_PLUS"
            Case DMPAPER_A5_TRANSVERSE
                List1.AddString "A5_TRANSVERSE"
            Case DMPAPER_B5_TRANSVERSE
                List1.AddString "B5_TRANSVERSE"
            Case DMPAPER_A3_EXTRA
                List1.AddString "A3_EXTRA"
            Case DMPAPER_A5_EXTRA
                List1.AddString "A5_EXTRA"
            Case DMPAPER_B5_EXTRA
                List1.AddString "B5_EXTRA"
            Case DMPAPER_A2
                List1.AddString "A2"
            Case DMPAPER_A3_TRANSVERSE
                List1.AddString "A3_TRANSVERSE"
            Case DMPAPER_A3_EXTRA_TRANSVERSE
                List1.AddString "A3_EXTRA_TRANSVERSE"
            Case DMPAPER_DBL_JAPANESE_POSTCARD
                List1.AddString "DBL_JAPANESE_POSTCARD"
            Case DMPAPER_A6
                List1.AddString "A6"
            Case DMPAPER_JENV_KAKU2
                List1.AddString "JENV_KAKU2"
            Case DMPAPER_JENV_KAKU3
                List1.AddString "JENV_KAKU3"
            Case DMPAPER_JENV_CHOU3
                List1.AddString "JENV_CHOU3"
            Case DMPAPER_JENV_CHOU4
                List1.AddString "JENV_CHOU4"
            Case DMPAPER_LETTER_ROTATED
                List1.AddString "LETTER_ROTATED"
            Case DMPAPER_A3_ROTATED
                List1.AddString "A3_ROTATED"
            Case DMPAPER_A4_ROTATED
                List1.AddString "A4_ROTATED"
            Case DMPAPER_A5_ROTATED
                List1.AddString "A5_ROTATED"
            Case DMPAPER_B4_JIS_ROTATED
                List1.AddString "B4_JIS_ROTATED"
            Case DMPAPER_B5_JIS_ROTATED
                List1.AddString "B5_JIS_ROTATED"
            Case DMPAPER_JAPANESE_POSTCARD_ROTATED
                List1.AddString "JAPANESE_POSTCARD_ROTATED"
            Case DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED
                List1.AddString "DBL_JAPANESE_POSTCARD_ROTATED"
            Case DMPAPER_A6_ROTATED
                List1.AddString "A6_ROTATED"
            Case DMPAPER_JENV_KAKU2_ROTATED
                List1.AddString "JENV_KAKU2_ROTATED"
            Case DMPAPER_JENV_KAKU3_ROTATED
                List1.AddString "JENV_KAKU3_ROTATED"
            Case DMPAPER_JENV_CHOU3_ROTATED
                List1.AddString "JENV_CHOU3_ROTATED"
            Case DMPAPER_JENV_CHOU4_ROTATED
                List1.AddString "JENV_CHOU4_ROTATED"
            Case DMPAPER_B6_JIS
                List1.AddString "B6_JIS"
            Case DMPAPER_B6_JIS_ROTATED
                List1.AddString "B6_JIS_ROTATED"
            Case DMPAPER_12X11
                List1.AddString "12X11"
            Case DMPAPER_JENV_YOU4
                List1.AddString "JENV_YOU4"
            Case DMPAPER_JENV_YOU4_ROTATED
                List1.AddString "JENV_YOU4_ROTATED"
            Case DMPAPER_P16K
                List1.AddString "P16K"
            Case DMPAPER_P32K
                List1.AddString "P32K"
            Case DMPAPER_P32KBIG
                List1.AddString "P32KBIG"
            Case DMPAPER_PENV_1
                List1.AddString "PENV_1"
            Case DMPAPER_PENV_2
                List1.AddString "PENV_2"
            Case DMPAPER_PENV_3
                List1.AddString "PENV_3"
            Case DMPAPER_PENV_4
                List1.AddString "PENV_4"
            Case DMPAPER_PENV_5
                List1.AddString "PENV_5"
            Case DMPAPER_PENV_6
                List1.AddString "PENV_6"
            Case DMPAPER_PENV_7
                List1.AddString "PENV_7"
            Case DMPAPER_PENV_8
                List1.AddString "PENV_8"
            Case DMPAPER_PENV_9
                List1.AddString "PENV_9"
            Case DMPAPER_PENV_10
                List1.AddString "PENV_10"
            Case DMPAPER_P16K_ROTATED
                List1.AddString "P16K_ROTATED"
            Case DMPAPER_P32K_ROTATED
                List1.AddString "P32K_ROTATED"
            Case DMPAPER_P32KBIG_ROTATED
                List1.AddString "P32KBIG_ROTATED"
            Case DMPAPER_PENV_1_ROTATED
                List1.AddString "PENV_1_ROTATED"
            Case DMPAPER_PENV_2_ROTATED
                List1.AddString "PENV_2_ROTATED"
            Case DMPAPER_PENV_3_ROTATED
                List1.AddString "PENV_3_ROTATED"
            Case DMPAPER_PENV_4_ROTATED
                List1.AddString "PENV_4_ROTATED"
            Case DMPAPER_PENV_5_ROTATED
                List1.AddString "PENV_5_ROTATED"
            Case DMPAPER_PENV_6_ROTATED
                List1.AddString "PENV_6_ROTATED"
            Case DMPAPER_PENV_7_ROTATED
                List1.AddString "PENV_7_ROTATED"
            Case DMPAPER_PENV_8_ROTATED
                List1.AddString "PENV_8_ROTATED"
            Case DMPAPER_PENV_9_ROTATED
                List1.AddString "PENV_9_ROTATED"
            Case DMPAPER_PENV_10_ROTATED
                List1.AddString "PENV_10_ROTATED"
            Case Else

                'ユーザー定義のときは
                If Paper(CT) >= DMPAPER_USER Then
                    List1.AddString "USER" & "(" & Str$(Paper(CT)) & ")"

                '未定義のときは
                Else
                    List1.AddString "UNKNOWN" & "(" & Str$(Paper(CT)) & ")"
                End If
        End Select
    Next CT
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End