如何用c#操纵并口针式打印机打印? VS2005的serialPort是不是只支持串口? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参看http://www.codeproject.com/csharp/prntjobcontrollerusingwmi.asp是否对你有帮助 Knight94(愚翁) : 谢谢您的回复。但您提供的资料不是我想要的,这篇资料并没有操纵并口来打印。其实我是想让针式打印机实现POS打印机那样的功能,能够直接往并口上发打印指令。 PrinterSettings类不太好用,看看下面这个,或许对有帮助事实上我觉得直接用回API来控制打印机效果比较理想http://www.dwww.cn/new/200621720141098.html 用上面的方法取得打印机后再配合WritePrinter来用,基本上就OK了^_^http://www.pinvoke.net/default.aspx/winspool.WritePrinter 楼主,问题解决了吗?我也遇到和你同样的问题了。找了好多资料都没有针对性。。要是问题解决了麻烦告诉我怎么解决的。 我的邮箱:[email protected] 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) { } 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 我也在写POS系统,上面这个打印方式你改一下就可以用了。 急问个C#的算法问题 大侠们 帮助啊 从指定字符后开始录入? 有关C#的WinForm程式中引用WebService问题(急) SQLCE 有条件求和的语句 datagridview中checkBox数据无法获取 winForm 中如何使用正则表达式对输入内容进行及时的验证 C#中的问题,关于十六进制 做WinForm时遇到的怪事 请教一个比较高级的问题:如何动态加载动态库中的窗口在容器内显示 考考您的c基礎 如何能获得实现同一接口的panel中的控件(附代码) 得到类的名称
http://www.codeproject.com/csharp/prntjobcontrollerusingwmi.asp是否对你有帮助
谢谢您的回复。但您提供的资料不是我想要的,这篇资料并没有操纵并口来打印。其实我是想让针式打印机实现POS打印机那样的功能,能够直接往并口上发打印指令。
再配合WritePrinter来用,基本上就OK了^_^http://www.pinvoke.net/default.aspx/winspool.WritePrinter
要是问题解决了麻烦告诉我怎么解决的。 我的邮箱:[email protected]
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)
{
}
{ 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