Untuk
membuat proyek, ikuti langkah-langkah berikut:
1. Mulai Visual Studio .NET. Pada File
menu, klik baru, dan kemudian klik Project. Pada Jenis proyek,
klik folder Proyek Visual Basic . Pada daftar pola dasar , klik Aplikasi
Windows, dan kemudian klik OK. Secara default, Form1 dibuat.
2. Pada menu tampilan , klik Toolbox
untuk menampilkan Toolbox, dan kemudian tambahkan tombol pada Form1.
Tombol ini diberi nama Button1.
3. Tambahkan tombol lain untuk
Form1. Tombol ini diberi nama Button2.
4. Klik dua kali Button1.
Jendela kode untuk bentuk yang muncul.
5. Ganti sub rutin Button1_Click
dengan kode berikut:
6. ' Click
event handler for a button - designed to show how to use the
7. '
SendFileToPrinter and SendBytesToPrinter functions.
8. Private Sub
Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
9. ' Allow
the user to select a file.
10. Dim ofd
As New OpenFileDialog()
11. If
ofd.ShowDialog(Me) Then
12. '
Allow the user to select a printer.
13. Dim
pd As New PrintDialog()
14.
pd.PrinterSettings = New PrinterSettings()
15. If (pd.ShowDialog() = DialogResult.OK)
Then
16.
' Print the file to the printer.
17.
RawPrinterHelper.SendFileToPrinter(pd.PrinterSettings.PrinterName,
ofd.FileName)
18. End
If
19. End If
20. End Sub '
Button1_Click()
21. Ganti sub rutin Button2_Click
dengan kode berikut:
22. ' Click
event handler for a button - designed to show how to use the
23. '
SendBytesToPrinter function to send a string to the printer.
24. Private Sub
Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
25. Dim s As
String
26. Dim pd
As New PrintDialog()
27.
28. ' You
need a string to send.
29. s =
"Hello, this is a test"
30. ' Open
the printer dialog box, and then allow the user to select a printer.
31.
pd.PrinterSettings = New PrinterSettings()
32. If
(pd.ShowDialog() = DialogResult.OK) Then
33.
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s)
34. End If
35. End Sub '
Button2_Click()
36. Masukkan kode berikut di bagian atas
dari file:
37.Imports System.IO
38.Imports System.Drawing.Printing
39.Imports System.Runtime.InteropServices
40. Tambahkan kode berikut dalam ruang
nama aplikasi utama tetapi di luar definisi kelas apa pun:
41.Public Class RawPrinterHelper
42. ' Structure
and API declarions:
43.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
44. Structure
DOCINFOW
45.
<MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
46.
<MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
47.
<MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
48. End
Structure
49.
50.
<DllImport("winspool.Drv",
EntryPoint:="OpenPrinterW", _
51.
SetLastError:=True, CharSet:=CharSet.Unicode, _
52.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
53. Public
Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr,
ByVal pd As Long) As Boolean
54. End Function
55.
<DllImport("winspool.Drv",
EntryPoint:="ClosePrinter", _
56.
SetLastError:=True, CharSet:=CharSet.Unicode, _
57.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
58. Public
Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
59. End Function
60.
<DllImport("winspool.Drv",
EntryPoint:="StartDocPrinterW", _
61.
SetLastError:=True, CharSet:=CharSet.Unicode, _
62.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
63. Public
Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32,
ByRef pDI As DOCINFOW) As Boolean
64. End Function
65. <DllImport("winspool.Drv",
EntryPoint:="EndDocPrinter", _
66.
SetLastError:=True, CharSet:=CharSet.Unicode, _
67.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
68. Public
Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
69. End Function
70.
<DllImport("winspool.Drv",
EntryPoint:="StartPagePrinter", _
71.
SetLastError:=True, CharSet:=CharSet.Unicode, _
72.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
73. Public
Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
74. End Function
75.
<DllImport("winspool.Drv",
EntryPoint:="EndPagePrinter", _
76.
SetLastError:=True, CharSet:=CharSet.Unicode, _
77.
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
78. Public
Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
79. End Function
80.
<DllImport("winspool.Drv",
EntryPoint:="WritePrinter", _
81.
SetLastError:=True, CharSet:=CharSet.Unicode, _
82. ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
83. Public
Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr,
ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
84. End Function
85.
86. '
SendBytesToPrinter()
87. ' When the
function is given a printer name and an unmanaged array of
88. ' bytes, the
function sends those bytes to the print queue.
89. ' Returns
True on success or False on failure.
90. Public
Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes
As IntPtr, ByVal dwCount As Int32) As Boolean
91. Dim
hPrinter As IntPtr ' The printer
handle.
92. Dim
dwError As Int32 ' Last error - in
case there was trouble.
93. Dim di
As DOCINFOW ' Describes your
document (name, port, data type).
94. Dim
dwWritten As Int32 ' The number of
bytes written by WritePrinter().
95. Dim
bSuccess As Boolean ' Your success
code.
96.
97. ' Set up
the DOCINFO structure.
98. With di
99.
.pDocName = "My Visual Basic .NET RAW Document"
100. .pDataType = "RAW"
101. End With
102. ' Assume
failure unless you specifically succeed.
103. bSuccess
= False
104. If
OpenPrinter(szPrinterName, hPrinter, 0) Then
105. If
StartDocPrinter(hPrinter, 1, di) Then
106. If StartPagePrinter(hPrinter)
Then
107. ' Write your
printer-specific bytes to the printer.
108. bSuccess =
WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
109. EndPagePrinter(hPrinter)
110. End If
111. EndDocPrinter(hPrinter)
112. End
If
113. ClosePrinter(hPrinter)
114. End If
115. ' If you
did not succeed, GetLastError may give more information
116. ' about
why not.
117. If
bSuccess = False Then
118. dwError =
Marshal.GetLastWin32Error()
119. End If
120. Return
bSuccess
121. End Function
' SendBytesToPrinter()
122.
123. '
SendFileToPrinter()
124. ' When the
function is given a file name and a printer name,
125. ' the
function reads the contents of the file and sends the
126. ' contents
to the printer.
127. ' Presumes
that the file contains printer-ready data.
128. ' Shows how
to use the SendBytesToPrinter function.
129. ' Returns
True on success or False on failure.
130. Public
Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal
szFileName As String) As Boolean
131. ' Open the file.
132. Dim fs
As New FileStream(szFileName, FileMode.Open)
133. ' Create
a BinaryReader on the file.
134. Dim br
As New BinaryReader(fs)
135. ' Dim an
array of bytes large enough to hold the file's contents.
136. Dim
bytes(fs.Length) As Byte
137. Dim
bSuccess As Boolean
138. ' Your
unmanaged pointer.
139. Dim
pUnmanagedBytes As IntPtr
140.
141. ' Read
the contents of the file into the array.
142. bytes =
br.ReadBytes(fs.Length)
143. '
Allocate some unmanaged memory for those bytes.
144. pUnmanagedBytes =
Marshal.AllocCoTaskMem(fs.Length)
145. ' Copy
the managed byte array into the unmanaged array.
146. Marshal.Copy(bytes, 0, pUnmanagedBytes,
fs.Length)
147. ' Send the
unmanaged bytes to the printer.
148. bSuccess
= SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
149. ' Free
the unmanaged memory that you allocated earlier.
150. Marshal.FreeCoTaskMem(pUnmanagedBytes)
151. Return
bSuccess
152. End Function
' SendFileToPrinter()
153.
154. ' When the
function is given a string and a printer name,
155. ' the
function sends the string to the printer as raw bytes.
156. Public
Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString
As String)
157. Dim
pBytes As IntPtr
158. Dim
dwCount As Int32
159. ' How
many characters are in the string?
160. dwCount
= szString.Length()
161. ' Assume
that the printer is expecting ANSI text, and then convert
162. ' the
string to ANSI text.
163. pBytes =
Marshal.StringToCoTaskMemAnsi(szString)
164. ' Send
the converted ANSI string to the printer.
165. SendBytesToPrinter(szPrinterName,
pBytes, dwCount)
166. Marshal.FreeCoTaskMem(pBytes)
167. End Function
168. End Class
No comments:
Post a Comment