admin 管理员组

文章数量: 887021


2024年2月24日发(作者:0是偶数吗从哪一年规定的)

C#控制打印机(直接发送打印机命令到打印机)

一个打印机控制类,很有用的,其中使用了windows api控制lpt端口,对条码打印机的控制方法如下:将打印机的命令写到一个文件里,再使用之。

**************************************************************************************************************************************

//代码:

//

using System;

using c;

using ;

using pServices;

namespace LPTControls

{

public class LPTControls

{

[StructLayout(tial)]

private struct OVERLAPPED

{

int Internal;

intInternalHigh;

int Offset;

intOffSetHigh;

inthEvent;

}

[DllImport("")]

private static extern intCreateFile(string lpFileName, uintdwDesiredAccess, intdwShareMode,

intlpSecurityAttributes, intdwCreationDisposition, intdwFlagsAndAttributes, inthTemplateFile);

[DllImport("")]

private static extern boolWriteFile(inthFile, byte[] lpBuffer, intnNumberOfBytesToWriter,

out intlpNumberOfBytesWriten, out OVERLAPPED lpOverLapped);

[DllImport("")]

private static extern boolCloseHandle(inthObject);

privateintiHandle;

//打开LPT 端口

public bool Open()

{

iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);

if (iHandle != -1)

{

return true;

}

else

{

return false;

}

}

//打印函数,参数为打印机的命令或者其他文本!

public bool Write(string MyString)

{

if (iHandle != 1)

{

inti;

OVERLAPPED x;

byte[] mybyte = es(MyString);

return WriteFile(iHandle, mybyte, , out i, out x);

}

else

{

throw new Exception("端口未打开~!");

}

}

//关闭打印端口

public bool Close()

{

return CloseHandle(iHandle);

}

}

}

//*************************************************************************************

//使用方法

private void button1_Click(object sender, EventArgs e)

{

trolslpt = new trols();

string mycommanglines = lText("");//里写了条码机的命令

();

(mycommanglines);

();

}


本文标签: 打印机 命令 端口 控制