thongkorn โพสต์ 2019-2-12 19:33:34

[VB6] แสดงรายชื่อเครื่องพิมพ์ทั้งหมด และเลือกกำหนดให้เป็น Default Printer

http://www.g2gnet.com/webboard/images/vb6/getsetprinterVB6.png


บทความตอนนี้ก็จะเป็นการแสดงรายชื่อเครื่องพิมพ์ที่ต่ออยู่ในเครื่องคอมฯออกมาทั้งหมด จากนั้นก็ทำการเลือกให้ตัวใดตัวหนึ่งเป็น Default Printer ซึ่งจะต้องใช้ Win32 API (Application Programming Interface) เข้าช่วย โดยประโยชน์อย่างอื่นก็สามารถนำไปใช้กำหนดการส่งออกไปพิมพ์ยังเครื่องพิมพ์ที่เราระบุ เช่น ระบบร้านอาหาร กำหนดให้เอกสารนี้ส่งไปครัว งานนี้ส่งออกไปเครื่องแคชเชียร์ หรือภายในสำนักงานที่มีเครื่องพิมพ์อยู่เป็นจำนวนมาก ก็นำไปประยุกต์ใช้งานเอาตามที่สะดวกกันน่ะครับ ...

มาดูโค้ดกันเถอะ ...
' / --------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / MORE: http://www.g2gnet.com/webboard
' /
' / Purpose: get all printers and set default printer.
' / Microsoft Visual Basic 6.0 (SP6)
' /
' / This is open source code under @CopyLeft by Thongkorn/Common Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------

Option Explicit

Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" ( _
    ByVal lpszSection As String, _
    ByVal lpszKeyName As String, _
    ByVal lpszString As String _
    ) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any _
    ) As Long
Private Const HWND_BROADCAST = &HFFFF&
Private Const WM_WININICHANGE = &H1A

Private Sub Form_Load()
    '// list all printers.
    Call ListPrinters(lstPrinters)
End Sub

Private Sub cmdSetDefault_Click()
    Dim DeviceName As String
    DeviceName = lstPrinters.List(lstPrinters.ListIndex)
    If Trim(DeviceName) <> "" Then
      '// Set Default Printer
      Call SetPrinter(DeviceName)
      MsgBox DeviceName & " has finish to be set as the default printer.", vbOKOnly + vbInformation, "Report Status"
    End If
End Sub

'// Set Default Printer
Private Sub SetPrinter(PrnName As String)
    Dim x As Long, sztemp As String
    Dim prn As VB.Printer
    If Printers.Count > 0 Then
      For Each prn In VB.Printers
            If prn.DeviceName = PrnName Then
                '// Set Default Printer with API
                sztemp = prn.DeviceName & "," & prn.DriverName & "," & prn.Port
                x = WriteProfileString("windows", "device", sztemp)
                x = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0&, "windows")
                Exit For
            End If
      Next prn
    End If
End Sub

'// List all Printers
Private Sub ListPrinters(ListControl As Object)
    On Error GoTo ErrHandler:
    Dim l As Integer
    Dim lCount As Integer
    ListControl.Clear
   
    lCount = Printers.Count
   
    If lCount = 0 Then
      ListControl.AddItem "(No Printer Installed)"
    Else
      For l = 0 To lCount - 1
            ListControl.AddItem Printers(l).DeviceName
      Next
    End If
    Exit Sub
   
ErrHandler:
    MsgBox "Error: " & Err.Number & vbCrLf & Err.Description
    Exit Sub
End Sub

ดาวน์โหลดโค้ดต้นฉบับ VB6 ได้ที่นี่ ...




หน้า: [1]
ดูในรูปแบบกติ: [VB6] แสดงรายชื่อเครื่องพิมพ์ทั้งหมด และเลือกกำหนดให้เป็น Default Printer