基本信息
源码名称:c#LPT打印程序
源码大小:0.01M
文件格式:.rar
开发语言:C#
更新时间:2015-08-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
C#控制LPT并口打印,直接写并口,打印机不需要安装驱动程序.

using System;
using System.Runtime.InteropServices;

namespace LptPrint_test
{
	/// <summary>
	/// LPTControl 的摘要说明。
	/// </summary>
	public class LPTControl
	{
		private string LptStr="lpt1";
		public LPTControl(string l_LPT_Str)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			LptStr=l_LPT_Str;
		}
		[StructLayout(LayoutKind.Sequential)]
			private struct OVERLAPPED
		{
			int Internal;
			int InternalHigh;
			int Offset;
			int OffSetHigh;
			int hEvent;
		}
		[DllImport("kernel32.dll")]
		private static extern int CreateFile(
			string lpFileName,
			uint dwDesiredAccess,
			int dwShareMode,
			int lpSecurityAttributes,
			int dwCreationDisposition,
			int dwFlagsAndAttributes,
			int hTemplateFile
			);
		[DllImport("kernel32.dll")]
		private static extern bool WriteFile(
			int hFile,
			byte[] lpBuffer,
			int nNumberOfBytesToWrite,
			ref int lpNumberOfBytesWritten,
			ref OVERLAPPED lpOverlapped
			);
		[DllImport("kernel32.dll")]
		private static extern bool CloseHandle(
			int hObject
			);
		private int iHandle;
		public bool Open()
		{
			iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0);
			if (iHandle != -1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		public bool Write(String Mystring)
		{
			if (iHandle != -1)
			{
				OVERLAPPED x = new OVERLAPPED();
				int i = 0;

				byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
				bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
				return b;
			}
			else
			{
				throw new Exception("不能连接到打印机!");
			}
		}

		public bool Write(byte[] mybyte)
		{
			if (iHandle != -1)
			{
				OVERLAPPED x = new OVERLAPPED();
				int i = 0;
				WriteFile(iHandle, mybyte, mybyte.Length,
					ref i, ref x);
				return true;
			}
			else
			{
				throw new Exception("不能连接到打印机!");
			}
		}
		public bool Close()
		{
			return CloseHandle(iHandle);
		}
        

	}
}