在线中心打印

时间:2015-03-22 07:20:52

标签: c# char

我正在尝试对齐打印。虽然我知道行中心的字符代码是(27,97,1),但是我无法实现它。在下面的代码中,eInit是' \ n'调试和eCenter是“125' :(。我需要将线对齐到中心。任何人都可以帮忙吗?

        private string PrinterName = "Printer1";
        public string eInit = Convert.ToString((char)10);
        public string eCentre = Convert.ToString((char)(27) + (char)(97) + (char)(1));

        private void bnPrint_Click(System.Object sender, System.EventArgs e)
        {

                PrintHeader();

        }
        public void PrintHeader()
        {

            Print(eInit+eCentre+"Hello"); 
            Print(eInit+eCentre+"Hi");              

        }
         public void Print(string Line)
        {
            SendStringToPrinter(PrinterName, Line );
        }

        public bool SendStringToPrinter(string szPrinterName, string szString)
        {
            bool functionReturnValue = false;
            if (PrinterOpen)
            {
                IntPtr pBytes = default(IntPtr);
                Int32 dwCount = default(Int32);
                Int32 dwWritten = 0;

                dwCount = szString.Length;

                pBytes = Marshal.StringToCoTaskMemAnsi(szString);

                functionReturnValue = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);

                Marshal.FreeCoTaskMem(pBytes);
            }
            else
            {
                functionReturnValue = false;
            }
            return functionReturnValue;
        }




        OUTPUT:
        125Hello
        125Hi

1 个答案:

答案 0 :(得分:1)

原始代码是添加字符值而不是连接字符。我通过将public string eCentre = Convert.ToString((char)(27) + (char)(97) + (char)(1));替换为:

来修复代码
public string eCentre = "";
public string x27 = Convert.ToString((char)27);

public string x97 = Convert.ToString((char)97);
public string x1 = Convert.ToString((char)1);

private void Form1_Load(object sender, EventArgs e)
{
    eCentre = eClear+x1+x2;
}