プリンタで使用できる用紙とサイズを取得 <TOP>
EnumPrinters
プリンタの詳細な情報を取得
OpenPrinter
プリンタオブジェクトをオープン
EnumForms
プリンタで利用できる用紙を列挙
MoveMemory
メモリの指定領域をコピー
lstrcpy 文字列をコピーする
ClosePrinter
プリンタオブジェクトを閉じる
'================================================================ '= プリンタで使用できる用紙とサイズを取得 '= (EnumForms.bas) '================================================================ #include "Windows.bi" Type PRINTER_DEFAULTS pDatatype As Long pDevMode As Long DesiredAccess As Long End Type Type SIZE cx As Long cy As Long End Type Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Type FORM_INFO_1 Flags As Long pName As Long Size As SIZE ImageableArea As RECT End Type Type PRINTER_INFO_5 pPrinterName As Long pPortName As Long Attributes As Long DeviceNotSelectedTimeout As Long TransmissionRetryTimeout As Long End Type #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 PRINTER_ENUM_NAME &H8 'Nameで指定されたプリンタを列挙 #define MAX_DEVICENAME 64 ' ' プリンタの詳細な情報を取得 Declare Function Api_EnumPrinters& Lib "winspool.drv" Alias "EnumPrintersA" (ByVal Flags&, ByVal Name$, ByVal Level&, pPrinterEnum As Any, ByVal cdBuf&, pcbNeededed&, pcReturned&) ' プリンタオブジェクトをオープン Declare Function Api_OpenPrinter& Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName$, phPrinter&, pDefault As Any) ' プリンタで利用できる用紙を列挙 Declare Function Api_EnumForms& Lib "winspool.drv" Alias "EnumFormsA" (ByVal hPrinter&, ByVal Level&, pForm As Any, ByVal cbBuf&, pcbNeeded&, pcReturned&) ' メモリの指定領域をコピー Declare Sub MoveMemory Lib "Kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal Length&) ' 文字列をコピーする Declare Function Api_lstrcpy& Lib "Kernel32" Alias "lstrcpy" (lpszString1 As Any, lpszString2 As Any) ' プリンタオブジェクトを閉じる Declare Function Api_ClosePrinter& Lib "winspool.drv" Alias "ClosePrinter" (ByVal hPrinter&) Var Shared List1 As Object Var Shared Combo1 As Object Var Shared Button1 As Object List1.Attach GetDlgItem("List1") : List1.SetFontSize 14 Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14 Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14 '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() Var PrinterServer As String Var PrinterName As String Var PortName As String Var Needed As Long Var Returned As Long Var Level As Long Var cnt As Long Var Ret As Long PrinterServer = "" Level = 5 Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrinterServer, Level, Chr$(0), 0, Needed, Returned) If Needed = 0 Then End Var Buffer(Needed - 1) As Byte Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrinterServer, Level, Buffer(0), Needed, Needed, Returned) Var pi5(Returned - 1) As PRINTER_INFO_5 For cnt = 0 To Returned - 1 MoveMemory pi5(cnt), Buffer(cnt * Len(pi5(cnt))), Len(pi5(cnt)) 'プリンタ名取得 PrinterName = String$(MAX_DEVICENAME, Chr$(0)) MoveMemory PrinterName, ByVal pi5(cnt).pPrinterName, Len(PrinterName) PrinterName = Left$(PrinterName, InStr(PrinterName, Chr$(0)) - 1) Combo1.AddString PrinterName Next End Sub '================================================================ '= '================================================================ Declare Sub Button1_on edecl () Sub Button1_on() Var DeviceName As String Var pd As PRINTER_DEFAULTS Var hPrinter As Long Var Level As Long Var Needed As Long Var Returned As Long Var FormName As String * 64 Var cnt As Long Var txt As String Var Ret As Long 'リストビューを初期化 List1.Resetcontent 'プリンタ名を指定 DeviceName = Combo1.GetWindowText 'プリンタアクセス権を指定 pd.DesiredAccess = PRINTER_ALL_ACCESS 'プリンタのオブジェクトハンドルを取得 Ret = Api_OpenPrinter(DeviceName, hPrinter, pd) '構造体のレベルを指定 Level = 1 'バッファに必要なサイズを取得 Ret = Api_EnumForms(hPrinter, Level, 0, 0, Needed, Returned) '用紙情報が取得できないときは If Needed <= 0 Then List1.AddString "取得できません。" '取得できないときの終了処理へ分岐 GoTo *ClosePrt End If 'バッファを確保 Var Buffer(Needed - 1) As Byte '用紙情報を取得 Ret = Api_EnumForms(hPrinter, Level, Buffer(0), Needed, Needed, Returned) '取得した用紙情報の構造体を確保 Var Info(Returned - 1) As FORM_INFO_1 '取得した用紙情報を構造体へ移動 MoveMemory Info(0), Buffer(0), Len(Info(0)) * Returned 'バッファに用紙情報がある間は For cnt = 0 To Returned - 1 '用紙情報を列挙(用紙名を複写) Ret = Api_lstrcpy(FormName, ByVal Info(cnt).pName) '用紙名を取得 txt = Left$(Left$(FormName, InStr(FormName, Chr$(0)) - 1) & Space$(24), 24) '幅を取得 txt = txt & " " & Format$(Info(cnt).Size.cx / 1000, "###.#mm") '高さを取得 txt = txt & " " & Format$(Info(cnt).Size.cy / 1000, "###.#mm") List1.AddString txt Next cnt *ClosePrt 'プリンタオブジェクトをクローズ Ret = Api_ClosePrinter(hPrinter) End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End