プリンタの用紙を設定 <TOP>
OpenPrinter
プリンタオブジェクトをオープン
GetPrinter
プリンタの詳細な情報を取得
MoveMemory
メモリの指定領域をコピー
SetPrinter
プリンタの詳細な情報を設定
ClosePrinter
プリンタオブジェクトを閉じる
コンボボックスで用紙サイズを選択実行をクリックします。右図は印刷プロパティで確認しています。
'================================================================ '= プリンタの用紙を設定 '= (SetPrinter3.bas) '================================================================ #include "Windows.bi" #define STANDARD_RIGHTS_REQUIRED &HF0000 '標準的な権利を要求することを示す定数 #define PRINTER_ACCESS_ADMINISTER &H4 'プリンタアクセス権の管理者権限を示す #define PRINTER_ACCESS_USE &H8 'プリンタアクセス権のユーザー権限を示す #define PRINTER_ALL_ACCESS (&HF0000 or &H4 or &H8) ' #define CCHDEVICENAME 32 'デバイス名の長さを示す定数 #define CCHFORMNAME 32 'フォーム名の長さを示す定数 #define DM_PAPERSIZE &H2 '用紙が有効であること示す #define DMPAPER_LETTER 1 'レター、81/2x11in #define DMPAPER_LETTERSMALL 2 'レタースモール、81/2x11in #define DMPAPER_A3 8 'A3、297x420mm #define DMPAPER_A4 9 'A4、210x297mm #define DMPAPER_A4SMALL 10 'A4Small、210x297mm #define DMPAPER_A5 11 'A5、148x210mm #define DMPAPER_B4 12 'B4、250x354mm #define DMPAPER_B5 13 'B5、182x257mm #define DMPAPER_NOTE 18 'ノート、81/2x11in #define DMPAPER_JAPANESE_POSTCARD 43 'はがき100x148 Type PRINTER_DEFAULTS pDatatype As Long pDevMode As Long DesiredAccess As Long End Type Type DEVMODE dmDeviceName(CCHDEVICENAME - 1) As Byte 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(CCHFORMNAME - 1) As Byte dmLogPixels As Integer dmBitsPerPel As Long dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long End Type Type PRINTER_INFO_2 pServerName As Long pPrinterName As Long pShareName As Long pPortName As Long pDriverName As Long pComment As Long pLocation As Long pDevMode As Long pSepFile As Long pPrintProcessor As Long pDatatype As Long pParameters As Long pSecurityDescriptor As Long Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long cJobs As Long AveragePPM As Long End Type ' プリンタオブジェクトをオープン Declare Function Api_OpenPrinter& Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName$, phPrinter&, pDefault As Any) ' プリンタの詳細な情報を取得 Declare Function Api_GetPrinter& Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter&, ByVal Level&, pPrinter As Any, ByVal cbBuf&, pcbNeed&) ' メモリの指定領域をコピー Declare Sub MoveMemory Lib "Kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal Length&) ' プリンタの詳細な情報を設定 Declare Function Api_SetPrinter& Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter&, ByVal Level&, pPrinter As Any, ByVal Command&) ' プリンタオブジェクトを閉じる Declare Function Api_ClosePrinter& Lib "winspool.drv" Alias "ClosePrinter" (ByVal hPrinter&) Var Shared Text1 As Object Var Shared Edit1 As Object Var Shared Combo1 As Object Var Shared Button1 As Object Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14 Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14 Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14 Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14 Var Shared ps(9) As Long '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() 'ここではプリンタ名を明示的に指定 Edit1.SetWindowtext "magicolor 2400W"
Combo1.AddString "LETTER" : ps(0) = DMPAPER_LETTER
Combo1.AddString "LETTERSMALL" : ps(1) = DMPAPER_LETTERSMALL
Combo1.AddString "A3" : ps(2) = DMPAPER_A3
Combo1.AddString "A4" : ps(3) = DMPAPER_A4
Combo1.AddString "A4SMALL" : ps(4) = DMPAPER_A4SMALL
Combo1.AddString "A5" : ps(5) = DMPAPER_A5
Combo1.AddString "B4" : ps(6) = DMPAPER_B4
Combo1.AddString "B5" : ps(7) = DMPAPER_B5
Combo1.AddString "NOTE" : ps(8) = DMPAPER_NOTE
Combo1.AddString "JAPANESE_POSTCARD" : ps(9) = DMPAPER_JAPANESE_POSTCARD
End Sub
'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
Var DeviceName As String
Var pd As PRINTER_DEFAULTS
Var pi2 As PRINTER_INFO_2
Var dm As DEVMODE
Var hPrinter As Long
Var Level As Long
Var Needed As Long
Var Ret As Long
DeviceName = Edit1.GetWindowText
'プリンタアクセス権を指定
pd.DesiredAccess = PRINTER_ALL_ACCESS
'プリンタのオブジェクトハンドルを取得
Ret = Api_OpenPrinter(DeviceName, hPrinter, pd)
'構造体のレベルを指定
Level = 2
'バッファに必要なサイズを取得
Ret = Api_GetPrinter(hPrinter, Level, ByVal 0, 0, Needed)
'バッファを確保
Var bytPrinterInfo2Buffer(Needed - 1) As Byte
'詳細なプリンタ情報を取得
Ret = Api_GetPrinter(hPrinter, Level, bytPrinterInfo2Buffer(0), Needed, Needed)
'取得した詳細なプリンタ情報を構造体へ移動
MoveMemory pi2, bytPrinterInfo2Buffer(0), Len(pi2)
'取得した詳細なプリンタデバイス情報を構造体へ移動
MoveMemory dm, ByVal pi2.pDevMode, Len(dm)
'用紙の指定可能
If dm.dmFields And DM_PAPERSIZE Then
'用紙を指定
dm.dmPaperSize = ps(Combo1.GetCursel)
'構造体を取得した詳細なプリンタデバイス情報へ移動
MoveMemory ByVal pi2.pDevMode, dm, Len(dm)
'詳細なプリンタ情報を書き戻し
Ret = Api_SetPrinter(hPrinter, Level, pi2, 0)
End If
'プリンタオブジェクトをクローズ
Ret = Api_ClosePrinter(hPrinter)
End Sub
'================================================================
'=
'================================================================
While 1
WaitEvent
Wend
Stop
End