通常使うプリンタを設定          <TOP>


GetProfileString WIN.INIから文字列を取得
WriteProfileString WIN.INIの指定のセクションの内容を変更
SendNotifyMessage 指定されたメッセージを1つまたは複数のウィンドウへ送信
 

リストボックスで選択し、設定をクリックすると「通常使うプリンタ」に設定されます。

参照

通常使うプリンタ名を取得

 

'================================================================
'= 通常使うプリンタを設定
'=    (WriteProfileString2.bas)
'================================================================
#include "Windows.bi"

#define HWND_BROADCAST &HFFFF           'トップレベルウィンドウに対してメッセージを送る
#define WM_WININICHANGE &H1A            'WIN.INIが変更された

' WIN.INIから文字列を取得
Declare Function Api_GetProfileString& Lib "Kernel32" Alias "GetProfileStringA" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnedString$, ByVal nSize&)

' WIN.INIの指定のセクションの内容を変更
Declare Function Api_WriteProfileString& Lib "Kernel32" Alias "WriteProfileStringA" (ByVal lpszSection$, ByVal lpszKeyName$, ByVal lpszString$)

' 指定されたメッセージを1つまたは複数のウィンドウへ送信
Declare Function Api_SendNotifyMessage& Lib "user32" Alias "SendNotifyMessageA" (ByVal hWnd&, ByVal msg&, ByVal wParam&, lParam As Any)

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 SetDefPrinter(PrinterName As String, DriverName As String, PrinterPort As String)
Sub SetDefPrinter(PrinterName As String, DriverName As String, PrinterPort As String)
    Var DeviceLine As String
    Var Ret As Long
    
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort

    Ret = Api_WriteProfileString("windows", "Device", DeviceLine)
    Ret = Api_SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub

'================================================================
'=
'================================================================
Declare Sub GetDriverAndPort(Buffer As String, DriverName As String, PrinterPort As String)
Sub GetDriverAndPort(Buffer As String, DriverName As String, PrinterPort As String)
    Var posDriver As Long
    Var posPort As Long
    DriverName = ""
    PrinterPort = ""

 
    posDriver = InStr(Buffer, ",")
   
    If posDriver > 0 Then
        DriverName = Left$(Buffer, posDriver - 1)
        posPort = InStr(posDriver + 1, Buffer, ",")

        If posPort > 0 Then
            PrinterPort = Mid$(Buffer, posDriver + 1, posPort - posDriver - 1)
        End If
    End If
End Sub

'================================================================
'=
'================================================================
Declare Function StripNulls(startstr As String) As String
Function StripNulls(startstr As String) As String
    Var epos As Long

    epos = InStr(startstr, Chr$(0))
  
    If epos Then
        StripNulls = Mid$(startstr, 1, epos - 1)
        startstr = Mid$(startstr, epos + 1, Len(startstr))
    End If
End Function

'================================================================
'=
'================================================================
Declare Function ProfileLoadWinIniList(lpSectionName As String) As Long
Function ProfileLoadWinIniList(lpSectionName As String) As Long
    Var success As Long
    Var nSize As Long
    Var lpKeyName As String
    Var Str As String
    Var Ret As Long
  
    Str = Space$(8102)
    nSize = Len(Str)
    success = Api_GetProfileString(lpSectionName, ByVal 0, ByVal 0, Str, nSize)
   
    If success Then
        Str = Left$(Str, success)

        Do Until Str = ""
            lpKeyName = StripNulls(Str)
            List1.AddString lpKeyName
        Loop
    End If
  
    ProfileLoadWinIniList = List1.GetCount
End Function

'================================================================
'=
'================================================================
Declare Sub SetDefaultPrinterWinNT()
Sub SetDefaultPrinterWinNT()
    Var Buffer As String
    Var DeviceName As String
    Var DriverName As String
    Var PrinterPort As String
    Var PrinterName As String
    Var r As Long
   
    If List1.GetCount > -1 Then
        Buffer = Space$(1024)
        PrinterName = List1.GetText(List1.GetCursel)
      
        Ret = Api_GetProfileString("PrinterPorts", PrinterName, "", Buffer, Len(Buffer))
 
        GetDriverAndPort Buffer, DriverName, PrinterPort

        If (Len(DriverName) > 0) And (Len(PrinterPort) > 0) Then
            SetDefPrinter PrinterName, DriverName, PrinterPort
        End If
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var Ret As Long

    Ret = ProfileLoadWinIniList("PrinterPorts")
    Button1.EnableWindow 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
   SetDefaultPrinterWinNT
End Sub

'================================================================
'=
'================================================================
Declare Sub List1_Click edecl ()
Sub List1_Click()
    Button1.EnableWindow -1
End Sub

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