プリンタ名とポート名を取得 <TOP>
プリンタ名とポート名を取得します。 EnumPrinters プリンタの詳細な情報を取得 CopyMemory ある位置から別の位置にメモリブロックを移動 PrinterProperties プリンタ設定ダイアログ表示 SetDefaultPrinter 通常使うプリンタの設定 OpenPrinter プリンタオブジェクトをオープン ClosePrinter プリンタオブジェクトを閉じる
'================================================================
'= プリンタ名とポート名を取得
'= (EnumPrinters.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
#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 Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)
' プリンタ設定ダイアログ表示
Declare Function Api_PrinterProperties& Lib "winspool.drv" Alias "PrinterProperties" (ByVal hWnd&, ByVal hPrinter&)
' 通常使うプリンタの設定
Declare Function Api_SetDefaultPrinter& Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter$)
' プリンタオブジェクトをオープン
Declare Function Api_OpenPrinter& Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName$, phPrinter&, pDefault As Any)
' プリンタオブジェクトを閉じる
Declare Function Api_ClosePrinter& Lib "winspool.drv" Alias "ClosePrinter" (ByVal hPrinter&)
Var Shared List1 As Object
Var Shared Button1 As Object
List1.Attach GetDlgItem("List1") : List1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14
'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
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 i As Long
Var Ret As Long
PrinterServer = ""
Level = 5
List1.ResetContent
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 i = 0 To Returned - 1
CopyMemory pi5(i), Buffer(i * Len(pi5(i))), Len(pi5(i))
'プリンタ名取得
PrinterName = String$(MAX_DEVICENAME, Chr$(0))
CopyMemory PrinterName, ByVal pi5(i).pPrinterName, Len(PrinterName)
PrinterName = Left$(PrinterName, InStr(PrinterName, Chr$(0)) - 1)
List1.AddString PrinterName
'ポート名取得
PortName = String$(MAX_DEVICENAME, Chr$(0))
CopyMemory PortName, ByVal pi5(i).pPortName, Len(PortName)
PortName = Left$(PortName, InStr(PortName, Chr$(0)) - 1)
List1.AddString " " & PortName
Next
End Sub
'================================================================
'=
'================================================================
Declare Sub List1_Change edecl ()
Sub List1_Change()
Var hPrinter As Long
Var Ret As Long
PrinterName$ = List1.GetText(List1.GetCursel)
Ret = Api_SetDefaultPrinter(PrinterName$)
Ret = Api_OpenPrinter(PrinterName$, hPrinter, ByVal 0)
Ret = Api_PrinterProperties(GethWnd, hPrinter)
Ret = Api_ClosePrinter(hPrinter)
End Sub
'================================================================
'=
'================================================================
While 1
WaitEvent
Wend
Stop
End