一岸柳  
在编程的过程中有些知识和大家分享,便随笔记下来。

直接打开端口往打印机里写资料就行了.
 Const GENERIC_READ = &H80000000
    Const GENERIC_WRITE = &H40000000
    Const OPEN_EXISTING = 3
    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As IntPtr

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                Dim iHandle As IntPtr
                iHandle = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
                If (iHandle.ToInt32 = -1) Then
                    MsgBox("没有连接打印机或者打印机端口不是LPT1")
                    Exit Sub
                Else
                    If i = 0 Then MsgBox("打印机连接成功!")

                    Dim fs As New FileStream(iHandle, FileAccess.ReadWrite)
                    Dim sr As New StreamReader(fs) '读数据
                    Dim sw As New StreamWriter(fs, System.Text.Encoding.Default) '写数据
 
                            sw.WriteLine()
                         
                     
                    End If
                    sw.Close()
                End If
            Next
        End If
    End Sub

    

    End Function

posted on 2004-12-01 22:55  一岸柳  阅读(853)  评论(0)    收藏  举报