首页 新闻 会员 周边 捐助

几句delphi转成C# 帮帮忙

0
悬赏园豆:100 [已解决问题] 解决于 2011-04-28 19:52

最近在弄一个打印机,别人给我的示例是delphi的,想转成C#。关键的函数如下:

f_iofile:textfile;

assignfile(f_iofile,'LPT1');

rewrite(f_iofile);
writeln(f_iofile,'{D0220,0860,0200|}'); //标签尺寸设定

writeln(f_iofile,'{C|}'); //清除缓冲

closefile(f_iofile);

bin糖葫芦的主页 bin糖葫芦 | 初学一级 | 园豆:100
提问于:2011-03-30 10:05
< >
分享
最佳答案
0
#region 串口打印
//打印函数,参数为打印机的命令或者其他文本!
public bool COMWrite(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, string myString)
{
try
{
SerialPort ComPort
= new SerialPort(portName, baudRate, parity, dataBits, stopBits);
if (ComPort.IsOpen)
{
ComPort.Close();
}
ComPort.Open();
ComPort.WriteLine(myString);
ComPort.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion 并口打印


#region 并口打印

[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 nNumberOfBytesToWriter, out int lpNumberOfBytesWriten, out OVERLAPPED lpOverLapped);

[DllImport(
"kernel32.dll")]
private static extern bool CloseHandle(int hObject);

private int iHandle;

//打开LPT 端口
public bool LPTOpen()
{
iHandle
= CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
//打印函数,参数为打印机的命令或者其他文本!
public bool LPTWrite(string myString)
{
if (!LPTOpen()) { return false; }
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);
if (WriteFile(iHandle, mybyte, mybyte.Length, out i, out x))
{
LPTClose();
return true;
}
else
{
return false;
}

}
else
{
throw new Exception("端口未打开~!");
return false;
}
}
//关闭打印端口
public bool LPTClose()
{
return CloseHandle(iHandle);
}
#endregion 并口打印
收获园豆:100
青 蛙 | 小虾三级 |园豆:655 | 2011-03-30 16:16
Right
.NET快速开发框架 | 园豆:946 (小虾三级) | 2011-04-06 11:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册