PDA

View Full Version : تشخیص و تغییر default printer



ghanbar1343M
شنبه 17 آبان 1382, 10:40 صبح
سلام

بچه ها کی میدونه در vb چه جوری میشه default پرینتر رو تشخیص داده و پرینتر مورد نظر رو تعریف کرد ?? :?:

کم حوصله
شنبه 17 آبان 1382, 23:53 عصر
می تونی بیشتر توضیح بدی می خوای چه کار بکنی ؟؟؟ :wink: :mrgreen:

ghanbar1343M
یک شنبه 18 آبان 1382, 09:11 صبح
اینم سورسش :



Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize 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 Any) As Long



Private Sub Form_Load()

ProfileLoadWinIniList List1, "PrinterPorts"
Command1.Enabled = False

End Sub


Private Sub Command1_Click()

Call SetDefaultPrinterWinNT

End Sub


Private Sub List1_Click()

Command1.Enabled = List1.ListIndex > -1

End Sub


Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
ByVal DriverName As String, _
ByVal PrinterPort As String)
Dim DeviceLine As String

'rebuild a valid device line string
DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort

'Store the new printer information in the
'[WINDOWS] section of the WIN.INI file for
'the DEVICE= item
Call WriteProfileString("windows", "Device", DeviceLine)

'Cause all applications to reload the INI file
Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")

End Sub

Private Sub GetDriverAndPort(ByVal Buffer As String, _
DriverName As String, _
PrinterPort As String)

Dim posDriver As Long
Dim posPort As Long
DriverName = ""
PrinterPort = ""

posDriver = InStr(Buffer, ",")

If posDriver > 0 Then

DriverName = Left(Buffer, posDriver - 1)

'The port name is the second entry after
'the driver name separated by commas.
posPort = InStr(posDriver + 1, Buffer, ",")

If posPort > 0 Then

'Strip out the port name
PrinterPort = Mid(Buffer, posDriver + 1, posPort - posDriver - 1)

End If
End If

End Sub


Public Function ProfileLoadWinIniList(lst As ListBox, _
lpSectionName As String) As Long

'Load the listbox data from win.ini.

Dim success As Long
Dim nSize As Long
Dim lpKeyName As String
Dim ret As String

'call the API passing null as the parameter
'for the lpKeyName parameter. This causes
'the API to return a list of all keys under
'that section. Pad the passed string large
'enough to hold the data. Adjust to suit.
ret = Space$(8102)
nSize = Len(ret)
success = GetProfileString(lpSectionName, _
vbNullString, _
"", _
ret, _
nSize)

'The returned string is a null-separated
'list terminated by a pair of null characters.
If success Then

'trim terminating null and trailing spaces
ret = Left$(ret, success)

'with the resulting string,
'extract each element
Do Until ret = ""

'strip off an item and
'add the item to the listbox
lpKeyName = StripNulls(ret)
lst.AddItem lpKeyName

Loop

End If

'return the number of items as an
'indicator of success
ProfileLoadWinIniList = lst.ListCount

End Function


Private Function StripNulls(startstr As String) As String

'Take a string separated by chr$(0)
'and split off 1 item, shortening the
'string so next item is ready for removal.
Dim pos As Long

pos = InStr(startstr$, Chr$(0))

If pos Then

StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))

End If

End Function


Private Sub SetDefaultPrinterWinNT()

Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
Dim r As Long

If List1.ListIndex > -1 Then

'Get the printer information for the currently selected
'printer in the list. The information is taken from the
'WIN.INI file.
Buffer = Space(1024)
PrinterName = List1.Text

Call GetProfileString("PrinterPorts", _
PrinterName, "", _
Buffer, Len(Buffer))

'Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort

If DriverName <> "" And PrinterPort <> "" Then
SetDefaultPrinter List1.Text, DriverName, PrinterPort
End If

End If

End Sub

S.Azish
دوشنبه 19 آبان 1382, 18:18 عصر
شیئ Printer در VB همیشه به Dfeault Printer اشاره میکنه.

Developer Programmer
شنبه 24 آبان 1382, 19:06 عصر
عزیز دلم به جای اینهمه سر در گمی برو MSDN 2003 CD رو بخر و هر چی خواستی توش پیدا کن
این مشکل شما هم با یه مثال نمونه تو MSDN بود خود من هم دیدمش!!!