如何在.net中实现下面的操作
    下面是一段是VB代码
        Open "lpt1" For Output As #1
        Print #1, "^XA"                                 '编程开始
        Print #1, "^LH" & txtX.Text & "," & txtY.Text   '标签起始位置
        Print #1, "^FW" & TxtWay                        '打印方向
        Print #1, "^FS"                                 '指令分隔符
        Print #1, "^FO" & dataX.Text & "," & dataY.Text '置数据区起点位置
        'Print #1, "^BY1.5,2,150"                        '默认参数
        Print #1, "^BY2,2,20"
        
就是如何控制针式打印机一行行的输出

解决方案 »

  1.   

    VB中能实现的东西到了DOT NET 中反而不行了,是微软的问题还是CSDN没有高手?
      

  2.   

    这是写端口方式实现的,可加入pos控制指令using System;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Text;
    namespace LRCY
    {

    /// <summary>
    /// POSPrinter 的摘要说明。
    /// </summary>
    public class POSPrinter
    {
    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    private static extern IntPtr CreateFile(string lpFileName,
    int dwDesiredAccess, 
    int dwShareMode, 
    int lpSecurityAttributes,
    int dwCreationDisposition ,
    int dwFlagsAndAttributes ,
    int hTemplateFile); const int OPEN_EXISTING = 3;
    string prnPort ="LPT1";
    //IntPtr iHandle;
    FileStream fs = null;
    StreamWriter sw = null;
    public POSPrinter()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public POSPrinter(string prnPort)
    {
    this.prnPort=prnPort;//打印机端口

    } //打开端口
    public string OpenPort()
    {
    string ret =null;
    try
    {
    IntPtr iHandle = CreateFile(prnPort,  0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
    if(iHandle.ToInt32() == -1)
    {
    ret= "没有连接打印机或者打印机端口不是" + prnPort;
    }
    else
    {
    fs = new FileStream(iHandle, FileAccess.ReadWrite);
    sw = new StreamWriter(fs, Encoding.Default); //写数据
    }
    }
    catch(Exception e)
    {
    ret = e.Message;
    }
    return ret;
    } //关闭端口
    public string ClosePort()
    {
    try
    {
    if( sw != null )
    sw.Close();
    if( fs != null )
    fs.Close();
    return null;
    }
    catch(Exception e)
    {
    return e.Message;
    }
    }

    /// <summary>
    /// 向打印机发送数据
    /// </summary>
    /// <param name="str">要打印的内容</param>
    /// <returns></returns>
    public  string PrintLine(string str)
    {

    try
    {
    sw.WriteLine(str);
    return null;

    }
    catch(Exception e)
    {
    return e.Message;
    }
    }

    /// <summary>
    /// 向打印机发送数据,不换行
    /// </summary>
    /// <param name="str">要打印的内容</param>
    /// <returns></returns>
    public  string Print(string str)
    {
    try
    {
    sw.Write(str);
    return null;

    }
    catch(Exception e)
    {
    return e.Message;
    }
    }

    }
    }