Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在行中心打印_C#_Char - Fatal编程技术网

C# 在行中心打印

C# 在行中心打印,c#,char,C#,Char,我正在尝试打印对齐。虽然我知道测线中心的字符代码是(27,97,1),但我无法实现它。在以下代码中,调试时eInit是“\n”,eCenter是“125”):(。我需要将测线与中心对齐。有人能帮忙吗 private string PrinterName = "Printer1"; public string eInit = Convert.ToString((char)10); public string eCentre = Convert.ToS

我正在尝试打印对齐。虽然我知道测线中心的字符代码是(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

原始代码是添加字符值而不是连接字符。我通过替换
公共字符串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;
}
更多信息请点击此处: