Menentukan Default Printer Windows dengan VB6

Menenentukan Default Printer Windows dengan VB6

Membuat sebuah aplikasi dengan fasilitas yang memudahkan kepada operator untuk melakukan setting dari kebutuhan sistem program sangatlah penting, sehingga operator tidak perlu keluar masuk program untuk melakukan setting dengan fasilitas windows yang ada. Salah satu fasitilas tersebut adalah menentupkan default printer windows. Pada koding program tersebut saya menggunakan VB6 untuk melakukan hal tersebut diatas.

Membaca Data Printer Windows

Langkah pertama yang harus anda lakukan adalah membaca daftar printer yang terinstall dalam Windows. Cara yang dilakukan sangat mudah, yaitu dengan menggunakan object Printer yang sudah tersedia dalam VB6. Dengan menggunakan perulangan atau looping each dari object Printer, maka anda akan mendapatkan daftar printer yang terintasll dalam windows, seperti contoh kode program berikut ini:

'Mengambil Daftar Printer
Private Sub AmbilDataPrinter()
    Dim objPrinter As Printer
    
    cboPrinter.Clear
    For Each objPrinter In Printers
        cboPrinter.AddItem objPrinter.DeviceName
    Next objPrinter
End Sub

Daftar printer yang terbaca tersebut kita masukkan dalam komponen ComboBox dengan membaca property DeviceName, dan selanjutnya dapat kita pilih printer yang akan disetting sebagai default.

Setting Default Printer

Dan untuk menentukan printer yang kita pilih menjadi default printer windows adalah dengan membuat kode program fungsi SetDefaultPrinter dengan menggunakan API "WriteProfileString" dan "SendMessage" seperti tampak dalam contoh kode program berikut ini:

'Menentukan Default Printer Windows
Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
    ByVal DriverName As String, ByVal PrinterPort As String)
    Dim DeviceLine As String
    Dim r As Long
    Dim l As Long
    
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
    
    r = WriteProfileString("windows", "Device", DeviceLine)
    
    l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub

Berikut contoh kode program lengkap dalam Menentukan Default Printer Windows dengan VB6, seperti tampak dalam contoh kode program berikut ini:

Option Explicit

'------------------------------------------------------------------------------
'Form Name             : frmPrinterSetup
'Development Tools     : Microsoft Visual Basic 6.0
'Author                : Nurdin Budi Mustofa
'------------------------------------------------------------------------------

Private Const HWND_BROADCAST = &HFFFF
Private Const WM_WININICHANGE = &H1A

Private Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, ByVal dev As Long) As Long
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 String) As Long

Private Type POINTAPI
 x As Long
 y As Long
End Type
Private Const DC_PAPERNAMES = 16
Private Const DC_PAPERS = 2
Private Const DC_PAPERSIZE = 3
Private Const DC_BINS = 6
Private Const DC_BINNAMES = 12

Dim PaperSource() As Integer
Dim counter As Long

Private Const SW_SHOWNORMAL = 1
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
 
Dim lngRet As Long

Private Sub Form_Load()
    AmbilDataPrinter
End Sub

'Mengambil Daftar Printer
Private Sub AmbilDataPrinter()
    Dim objPrinter As Printer
    
    cboPrinter.Clear
    For Each objPrinter In Printers
        cboPrinter.AddItem objPrinter.DeviceName
    Next objPrinter
End Sub

'Menentukan Jenis Printer yang Dipilih
Private Sub cboPrinter_Click()
    Dim objPrinter As Printer
    Dim oPaperSize As Printer
    
    Dim lPaperCount As Long, lCounter As Long
    Dim hprinter As Long, sDeviceName As String
    Dim sDevicePort As String, sPaperNamesList As String
    Dim sNextString As String, numPaper() As Long
    Dim paperNumbers() As Integer, paperSizes() As POINTAPI
    
    If cboPrinter.ListIndex <> -1 Then
        For Each objPrinter In Printers
            If objPrinter.DeviceName = cboPrinter.Text Then
               Set Printer = objPrinter
               lblPrinterDriver.Caption = Printer.DriverName
               Exit For
            End If
        Next
    End If
    
    
    cboPaperSize.Clear
    lPaperCount = DeviceCapabilities(Printer.DeviceName, Printer.Port, DC_PAPERNAMES, ByVal vbNullString, 0)
    ReDim numPaper(1 To lPaperCount)
    sPaperNamesList = String(64 * lPaperCount, 0)
    ' Get paper names
    lPaperCount = DeviceCapabilities(Printer.DeviceName, _
       Printer.Port, DC_PAPERNAMES, ByVal sPaperNamesList, 0)
    ' Get matching paper numbers
    ReDim paperNumbers(1 To lPaperCount)
    lPaperCount = DeviceCapabilities(Printer.DeviceName, _
       Printer.Port, DC_PAPERS, paperNumbers(1), 0)
    ReDim paperSizes(1 To lPaperCount)
    lPaperCount = DeviceCapabilities(Printer.DeviceName, _
       Printer.Port, DC_PAPERSIZE, paperSizes(1), 0)
    For lCounter = 1 To lPaperCount
        sNextString = Mid(sPaperNamesList, 64 * (lCounter - 1) + 1, 64)
        sNextString = Left(sNextString, InStr(1, sNextString, Chr(0)) - 1)
        cboPaperSize.AddItem paperNumbers(lCounter) & " = " & Format(paperSizes(lCounter).x / 254, "0.00") & " x " _
                             & Format(paperSizes(lCounter).y / 254, "0.00") & " inch" & " - " & sNextString
    Next lCounter
    
    Call DataPaperSource
End Sub

'Membaca Data Source Papaer (Ukuran Kertas) Berdasar Printer yang Dipilih
Private Sub DataPaperSource()
    Dim NumberOfBin As Long
    Dim BinNameList As String
    Dim BinName As String
    
    On Error GoTo FillBinErr:
    
    cbSource.Clear
    
    NumberOfBin = DeviceCapabilities(Printer.DeviceName, Printer.Port, _
                    DC_BINS, ByVal vbNullString, 0)
    ReDim PaperSource(1 To NumberOfBin)
    
    NumberOfBin = DeviceCapabilities(Printer.DeviceName, Printer.Port, _
                    DC_BINS, PaperSource(1), 0)
    
    If NumberOfBin <> -1 Then
        
        BinNameList = String(24 * NumberOfBin, 0)
        NumberOfBin = DeviceCapabilities(Printer.DeviceName, Printer.Port, _
                        DC_BINNAMES, ByVal BinNameList, 0)
        
        For counter = 1 To NumberOfBin
            BinName = Mid(BinNameList, 24 * (counter - 1) + 1, 24)
            BinName = Left(BinName, InStr(1, BinName, Chr(0)) - 1)
            cbSource.AddItem BinName
        Next counter
                
        If cbSource.ListIndex = -1 Then cbSource.ListIndex = 0
    End If
    
    Exit Sub
    
FillBinErr:
    MsgBox Err.Description, vbExclamation
End Sub

'Click Command Button Apply Default Printer yang Dipilih
Private Sub cmdDefault_Click()
    SetDefaultPrinter cboPrinter.Text, lblPrinterDriver.Caption, cbSource.Text
End Sub

'Menentukan Default Printer Windows
Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
    ByVal DriverName As String, ByVal PrinterPort As String)
    Dim DeviceLine As String
    Dim r As Long
    Dim l As Long
    
    DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
    
    r = WriteProfileString("windows", "Device", DeviceLine)
    
    l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub

'Menampilkan Daftar Printer Windows
Private Sub cmdWindowsPrinterSetting_Click()
    Shell "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL PrintersFolder", vbNormalFocus
End Sub

'---------------------------------------------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------------------------------------------
'Blog/Article
'---------------------------------------------------------------------------------------------------------------------------------------
Private Sub lblFacebook_Click()
    lngRet = ShellExecute(0, vbNullString, "https://www.facebook.com/Kodiing-279643708757204/", vbNullString, "C:\", SW_SHOWNORMAL)
End Sub

Private Sub lblKodiing_Click()
    lngRet = ShellExecute(0, vbNullString, "www.kodiing.com", vbNullString, "C:\", SW_SHOWNORMAL)
End Sub

Private Sub lblKontak_Click()
    lngRet = ShellExecute(0, vbNullString, "www.kodiing.com/p/kontak.html", vbNullString, "C:\", SW_SHOWNORMAL)
End Sub
'---------------------------------------------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------------------------------------------

Berikut source code contoh program menentukan default printer windows, yang bisa anda download disini. Jika anda masih belum jelas tentang program tersebut diatas, anda bisa bertanya pada kami lewat Contact Form atau gunakan dalam Komentar. Dan bagi yang masih awam tentang bahasa pemrograman VB6, anda bisa mengakses Tutorial VB6 yang telah kami posting sebelumnya.

Previous
Next Post »