VS2005的serialPort是不是只支持串口?

解决方案 »

  1.   

    参看
    http://www.codeproject.com/csharp/prntjobcontrollerusingwmi.asp是否对你有帮助
      

  2.   

    Knight94(愚翁) :
      谢谢您的回复。但您提供的资料不是我想要的,这篇资料并没有操纵并口来打印。其实我是想让针式打印机实现POS打印机那样的功能,能够直接往并口上发打印指令。
      

  3.   

    PrinterSettings类不太好用,看看下面这个,或许对有帮助事实上我觉得直接用回API来控制打印机效果比较理想http://www.dwww.cn/new/200621720141098.html
      

  4.   

    用上面的方法取得打印机后
    再配合WritePrinter来用,基本上就OK了^_^http://www.pinvoke.net/default.aspx/winspool.WritePrinter
      

  5.   

    楼主,问题解决了吗?我也遇到和你同样的问题了。找了好多资料都没有针对性。。
    要是问题解决了麻烦告诉我怎么解决的。 我的邮箱:[email protected]
      

  6.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Drawing.Printing;
    using System.Diagnostics;namespace PrintText
    {
    /// <summary>
    /// Summary description for frmPrintText.
    /// </summary>
    public class frmPrintText : System.Windows.Forms.Form
    {
            private System.Windows.Forms.Button cmdPrint;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
            private System.Windows.Forms.TextBox tbText;        private PrintDocument m_pd;
            private ArrayList m_al;        private Font font;        private float fTopMargin;
            private float fLeftMargin;
            private float fBottomMargin;
            private float fRightMargin;
            private float fPosition;
            private float fHeight;        private int m_nLineCount;
            private int m_nCurrentLine;
            private int m_nRowCount; public frmPrintText()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //            m_pd = new PrintDocument();            m_pd.PrintPage += new PrintPageEventHandler(PrintPageHandler);            m_al = new ArrayList();            font = new Font("Courier New",12,FontStyle.Regular,GraphicsUnit.Pixel);            fTopMargin = 50;
                fLeftMargin = 0;
                fBottomMargin = (float)1037.5;
                fRightMargin = 800;
                fPosition = 0;
                fHeight = 0; } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
                this.cmdPrint = new System.Windows.Forms.Button();
                this.tbText = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // cmdPrint
                // 
                this.cmdPrint.Location = new System.Drawing.Point(16, 16);
                this.cmdPrint.Name = "cmdPrint";
                this.cmdPrint.TabIndex = 0;
                this.cmdPrint.Text = "&Print";
                this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
                // 
                // tbText
                // 
                this.tbText.Location = new System.Drawing.Point(16, 48);
                this.tbText.MaxLength = 2000;
                this.tbText.Multiline = true;
                this.tbText.Name = "tbText";
                this.tbText.Size = new System.Drawing.Size(264, 208);
                this.tbText.TabIndex = 1;
                this.tbText.Text = "";
                // 
                // frmPrintText
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.tbText);
                this.Controls.Add(this.cmdPrint);
                this.Name = "frmPrintText";
                this.Text = "Print Text";
                this.ResumeLayout(false);        }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new frmPrintText());
    }        private void frmPrintText_Load(object sender, System.EventArgs e)
            {
            
            }
      

  7.   

    private void SplitUpText(string strText,int nMaxCount)
            {            int nLength = 0;
                int nWhereAt = 0;            char nChar;
                char[] characters = strText.ToCharArray();            string strCurrentLine = "";
                string strTrimmedText = "";
                string strCurrentWord = "";            int nWordLength = 0;            m_al.Clear();
                
                nLength = characters.Length;            while (nWhereAt < nLength)
                {                nChar = characters[nWhereAt];                if ((nChar == 32) || (nChar == '\r'))
                    {                    strTrimmedText = strCurrentWord.Trim();
                        nWordLength = strTrimmedText.Length;// add to string here                    if ((strCurrentLine.Length + nWordLength + 1) > nMaxCount)
                        {                        m_al.Add(strCurrentLine);
                            strCurrentLine = strTrimmedText + " ";                    } // if ((strCurrentLine.Length + nWordLength + 1) > nMaxCount)
                        else
                        {                        strCurrentLine = strCurrentLine + strTrimmedText + " ";                    } // else (strCurrentLine.Length + nWordLength + 1) < nMaxCount                    strCurrentWord = "";                } // if ((nChar == 32) || (nChar == '\r'))
                    else
                    {                    if ((nChar != 10) && (nChar != 13))
                        {
                            
                            strCurrentWord = strCurrentWord + nChar;
                            
                        } // if ((nChar != 10) && (nChar != 13))                } // else nChar != 32 && nChar == '\r'                nWhereAt++;            } // while (nWhereAt < nLength)            if (strCurrentLine.Length > 0)
                {                strTrimmedText = strCurrentLine.Trim();
                    m_al.Add(strTrimmedText);            } // if (strCurrentLine.Length > 0)        } // private void SplitUpText(string strText,int nMaxCount)        private void cmdPrint_Click(object sender, System.EventArgs e)
            {            PrintDialog pDlg = new PrintDialog();
                string strText = "";
                
                try
                {                strText = tbText.Text;
                    if (strText.Length > 0)
                    {                    SplitUpText(strText,80);                    m_nRowCount = m_al.Count;
                        m_nLineCount = 72;
                        m_nCurrentLine = 0;                    m_pd.DocumentName = "Print Text";
                        pDlg.Document = m_pd;
                        pDlg.ShowDialog();
                        m_pd.Print();                } // if (strText.Length > 0)
                }
                catch(Exception ex)
                {
                    MessageBox.Show(this,ex.Message,"Print Text");
                } // catch(Exception ex)        } // private void cmdPrint_Click(object sender, System.EventArgs e)        private void PrintPageHandler(object sender, PrintPageEventArgs e)
            {            int nLinesPrinted = 0;
                string strLine = "";            while ((nLinesPrinted < m_nLineCount) && (m_nCurrentLine < m_nRowCount))
                {                strLine = m_al[m_nCurrentLine].ToString();                fHeight = font.GetHeight(e.Graphics);
                    
                    fPosition = fTopMargin + (nLinesPrinted * fHeight);                e.Graphics.DrawString(strLine,
                                          font,
                                          Brushes.Black,
                                          fLeftMargin,
                                          fPosition,
                                          new StringFormat());                m_nCurrentLine++;
                    nLinesPrinted++;            } // while ((nLinesPrinted < m_nLineCount) && (m_nCurrentLine < m_nRowCount))            if (m_nCurrentLine < m_nRowCount)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;        } // private void PrintPageHandler(object sender, PrintPageEventArgs e)    } // public class frmPrintText : System.Windows.Forms.Form} // namespace PrintText
      

  8.   

    我也在写POS系统,上面这个打印方式你改一下就可以用了。