• 大小: 41KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: C#
  • 标签: 打印  

资源简介

C#直接调用USB接口斑马打印机,使打印速度更快,更方便。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Management;

using System.Drawing.Printing;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading; 

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            //USB打印支持属于Win32_USBHub类
            SelectQuery selectQuery = new SelectQuery(“Win32_USBHub“);
            ManagementobjectSearcher searcher = new ManagementobjectSearcher(selectQuery);
            foreach (Managementobject disk in searcher.Get())
            {
                string PNPDeviceID = disk[“PNPDeviceID“] as String;
                BeginPrint(this.txtNum.Text.Trim() PNPDeviceID);
            }  
            MessageBox.Show(“OK“);
        }
        public class Printer
        {
            [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
            private struct OVERLAPPED { int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent;      }
            [System.Runtime.InteropServices.DllImport(“kernel32.dll“)]
            private static extern int CreateFile(string lpFileName uint dwDesiredAccess int dwShareMode int lpSecurityAttributes int dwCreationDisposition int dwFlagsAndAttributes int hTemplateFile);
            [System.Runtime.InteropServices.DllImport(“kernel32.dll“)]
            private static extern bool WriteFile(int hFile byte[] lpBuffer int nNumberOfBytesToWrite out   int lpNumberOfBytesWritten out   OVERLAPPED lpOverlapped);
            [System.Runtime.InteropServices.DllImport(“kernel32.dll“)]
            private static extern bool CloseHandle(int hobject);
            private int iHandle; 

            public bool Open(string PNPDeviceID)
            {
                //{A5DCBF10-6530-11D2-901F-00C04FB951ED}是USB设备类的classID
                iHandle = CreateFile(“\\\\.\\“ + PNPDeviceID.Replace(‘\\‘ ‘#‘) + “#{A5DCBF10-6530-11D2-901F-00C04FB951ED}“
                     (uint)FileAccess.ReadWrite 0 0 (int)FileMode.Open 0 0);  
                if (iHandle != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public bool Write(string Mystring)
            {
                if (iHandle != -1)
                {
                    int i; OVERLAPPED x; 
                    byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                    return WriteFile(iHandle mybyte mybyte.Length out i out x);
               

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10752  2013-08-20 15:53  WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe

     文件      28160  2013-08-20 15:53  WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb

     文件      11600  2013-08-20 15:55  WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe

     文件        490  2010-03-17 22:39  WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest

     文件       3549  2013-08-20 16:59  WindowsFormsApplication2\Form1.cs

     文件       2554  2013-08-20 15:53  WindowsFormsApplication2\Form1.Designer.cs

     文件       5817  2013-08-20 15:53  WindowsFormsApplication2\Form1.resx

     文件       4501  2013-08-19 09:22  WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6436  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        264  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        694  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\GenerateResource.write.1.tlog

     文件        835  2013-08-20 15:55  WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt

     文件      10752  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.exe

     文件        180  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Form1.resources

     文件      28160  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.pdb

     文件        180  2013-08-20 13:21  WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Properties.Resources.resources

     文件        505  2013-08-16 10:16  WindowsFormsApplication2\Program.cs

     文件       1398  2013-08-16 10:16  WindowsFormsApplication2\Properties\AssemblyInfo.cs

     文件       2898  2013-08-16 10:16  WindowsFormsApplication2\Properties\Resources.Designer.cs

     文件       5612  2013-08-16 10:16  WindowsFormsApplication2\Properties\Resources.resx

     文件       1109  2013-08-16 10:16  WindowsFormsApplication2\Properties\Settings.Designer.cs

     文件        249  2013-08-16 10:16  WindowsFormsApplication2\Properties\Settings.settings

     文件       3754  2013-08-19 09:53  WindowsFormsApplication2\WindowsFormsApplication2.csproj

     文件        889  2013-08-16 10:16  WindowsFormsApplication2\WindowsFormsApplication2.sln

    I.A..H.      8192  2016-01-04 09:46  WindowsFormsApplication2\WindowsFormsApplication2.suo

     目录          0  2013-08-16 10:16  WindowsFormsApplication2\obj\x86\Debug\TempPE

     目录          0  2013-08-20 15:53  WindowsFormsApplication2\obj\x86\Debug

     目录          0  2013-08-20 13:21  WindowsFormsApplication2\bin\Debug

     目录          0  2013-08-16 10:16  WindowsFormsApplication2\obj\x86

     目录          0  2013-08-16 10:16  WindowsFormsApplication2\bin

............此处省略6个文件信息

评论

共有 条评论