普通的页打印模式因为要走完纸张,不符合要求!实现代码考虑到网络打印机的可能。有代码最好,没代码敬请提供个思路,多谢了。

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Win32.SafeHandles;
    using System.Runtime.InteropServices;
    using System.IO;namespace ConsoleApplication1
    {
    class Program
    {
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
    //[DllImport("kernel32.dll", SetLastError = true)]
    //internal static extern bool CloseHandle(SafeFileHandle handle); const int OPEN_EXISTING = 3;
    const uint GENERIC_READ = (0x80000000);
    const uint GENERIC_WRITE = (0x40000000);
    const uint GENERIC_EXECUTE = (0x20000000);
    const uint GENERIC_ALL = (0x10000000);
    static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); static void Main(string[] args)
    {
    //hFile = CreateFile("LPT1:", GENERIC_READIDENERIC_WRITE, 0,NULL, OPEN_EXISTING, NULL, NULL);   
    SafeFileHandle hFile = CreateFile(@"LPT1", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (!hFile.IsInvalid)
    { using (System.IO.FileStream fs = new System.IO.FileStream(hFile, FileAccess.ReadWrite))
    { fs.WriteByte(0x1b); fs.WriteByte(0x40); byte[] bytes = System.Text.Encoding.Default.GetBytes(string.Format(
    @"
    /----------------------------\
    |                            |
    |    {0}     |
    |                            |
    \----------------------------/
    /----------------------------\
    |                            |
    |    {0}     |
    |                            |
    \----------------------------/
    /----------------------------\
    |                            |
    |    {0}     |
    |                            |
    \----------------------------/
    /----------------------------\
    |                            |
    |    {0}     |
    |                            |
    \----------------------------/", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
    );
    fs.Write(bytes, 0, bytes.Length);
    for (int i = 0; i < 5; i++)
    {
    fs.WriteByte(0x0A); //走纸(忘记了可能是这个)
    } } //CloseHandle(hFile);
    }
    }
    }
    }如果是网络打印 好像(是好像啊,很久没用了手头没东西没法给你试验) LPT1 那个换成 \\ip\<打印机共享名>
    代码没东西测试,自己测试一下吧;具体【打印控制码】;每个打印机可能不同,你自己找打印机的说明书看看;
    或者到 google 搜索【打印控制码】;有很多通用的走纸的等;
      

  2.   

    买打印机的时候,有相应的DLL给你的
      

  3.   

    建议你去下一个FASTREPORT STUDIO控件,现在网上有个3.20版本的可以在VS2003上正常使用,但是在VS2005上有些问题(无法让客户自定义打印,只能提前设计好格式)但不影响基本使用.你可以下一个试试,这个原来是DELPHI上使用的比较多的,网络打印机的话你共享到本地就可以了
      

  4.   

    TO FlashElf 你的代码我测试一下,谢谢
    TO limin4506 你的想法我要考虑一下
    TO 天堂的旁边 这个控件能逐行打吗?
    TO HolyPlace 可以共享的话希望能发我一份,重分回报目前找了个微软的RAWPrinterHelper类,但是打印一点没反应,而且听说好象中文会出乱码
      

  5.   

    http://user.qzone.qq.com/67287835/blog/1215249843
      

  6.   

     PrintDocument pd = new PrintDocument();
                        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                        PaperSize ps = new PaperSize("专用打印纸张", 380, 250);
                        pd.DefaultPageSettings.PaperSize = ps;
                        //pd.DefaultPageSettings.Landscape = true;
                        pd.DocumentName = "出入证卡片打印";
                        //printPreviewDialog1.Document = pd;
                        //DialogResult res = printPreviewDialog1.ShowDialog();
                        printDialog1 .Document =pd;
                        DialogResult result = printDialog1.ShowDialog();
                           if (result == DialogResult.OK)
                               pd.Print(); private void pd_PrintPage(object sender, PrintPageEventArgs e)
            {
                Font mainFont = new Font("宋体", 16);
                int count = 0;
                float ypos = 0;
                float leftMargin = 50;//e.MarginBounds.Left; 
                float topMargin = 50;// e.MarginBounds.Top;  
                int lineperpage =e.MarginBounds.Heigth/mainFont.GetHeight(e.Graphics); 
                //算一下,这种纸一页能打多少行  
                string line = “*****”;
                 e.Graphics.DrawString(line,.......);
                e.HasMorePages = false;
            }
      

  7.   

    装个热敏打印机,如果要网络打印是要装驱动,如果是本机的话就直接并口输出就行了,具体代码LZ可以上www.diy-pos.com  上面有各种语言的示例
      

  8.   

    谢谢各位,特别是 tianjinldl 
    我回复信息回复不了,代码我晚上测试一下