c#打印小票

test310520 2009-04-01 11:48:38
请问如何用C#打印小票 主要是没做过打印的给列子学学
...全文
1206 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
周药师 2010-08-30
  • 打赏
  • 举报
回复
打印小票?
你先要搞清楚你用什么打印机?
如果是通过串口或者并口的
就不需要报表控件 直接给串口发打印协议 就打印了
aixiuhua 2010-08-30
  • 打赏
  • 举报
回复
忘了加上详细的链接地址了:http://du.lovekq.com/archives/51
aixiuhua 2010-08-21
  • 打赏
  • 举报
回复
刚写了一篇关于打印小票的问题,可以去看看du.lovekq.com
test310520 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tabbycat 的回复:]
直接打字符串,要对纸张的,调整很费时间

用ReportViewer控件也可以打,不过要打印机驱动支持
[/Quote]
帮我看看这段代码怎么打不出来
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
Font contentFont = new Font("宋体", 9);
SolidBrush myBrush = new SolidBrush(Color.Black);
linesPerPage = e.MarginBounds.Height / contentFont.GetHeight(e.Graphics);
while (count<linesPerPage&&icount<iDataCount)
{
line =lmenuview.Items[icount].SubItems[1].ToString()+" "+lmenuview.Items[icount].SubItems[4].ToString()+" "+lmenuview.Items[icount].SubItems[5].ToString();
yPosition = topMargin+(count*contentFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, contentFont, Brushes.Black,
leftMargin, yPosition, new StringFormat());
count++;
}
if(line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;

}
liujiayu10 2009-04-01
  • 打赏
  • 举报
回复

什么打印控件不重要,关键是要不断的调整位置,设置好进纸高度
tabbycat 2009-04-01
  • 打赏
  • 举报
回复
直接打字符串,要对纸张的,调整很费时间

用ReportViewer控件也可以打,不过要打印机驱动支持
javacaspnet 2009-04-01
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace wsTaoPrint
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class First : System.Web.UI.Page
{
public DataRow dr;
private void Page_Load(object sender, System.EventArgs e)
{
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("piaoju.mdb"));
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from contents where id=1",myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds,"Contents");
dr=ds.Tables["Contents"].Rows[0];
myConnection.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

hzysoft 2009-04-01
  • 打赏
  • 举报
回复
这个打印方法对你有用。直接传数据和座标值进去就可以了,主要应用是定点打印功能.坐标值以CM为单位(本来是以像数为单位的,转了)

using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace WinUI.BusinessManage
{
/// <summary>
/// 打印方法
/// </summary>
public class Printf
{
public Printf()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

private string[] printString =null;

private string imagePath = @"";

private Font _font = new Font("宋体", 10);

private int xPoint=0;

private int yPoint=0;

private float[] x=null;

private float[] y=null;

private Color _color= Color.Black;

private int pageCount = 1;

private int pagePos=0;//分页计数器

private int iPos=0;//内容计数器

private int iCount=0;//每一页打印内容数

// private float _top=0;
// private float _left=0;
private float _top=float.Parse(ConfigurationSettings.AppSettings.Get("PrintTop"));
private float _left=float.Parse(ConfigurationSettings.AppSettings.Get("PrintLeft"));

private float drawImageWidth=20;

private float drawImageHeight=28;

/// <summary>
/// 图片路径
/// </summary>
public string ImagePath
{
set{imagePath=value;}
get{return imagePath;}
}

/// <summary>
/// 字体
/// </summary>
public Font Fonts
{
set{_font=value;}
get{return _font;}
}

/// <summary>
/// 字体颜色
/// </summary>
public string[] PrintString
{
set{ printString=value;}
get{return printString;}
}

/// <summary>
/// 定点打印X坐标
/// </summary>
public float[] X
{
set{ x=value;}
get{return x;}
}

/// <summary>
/// 定点打印Y坐标
/// </summary>
public float[] Y
{
set{ y=value;}
get{return y;}
}

/// <summary>
/// 字体颜色
/// </summary>
public Color FontColor
{
set{ _color=value;}
get{return _color;}
}

/// <summary>
/// 打印页数
/// </summary>
public int PageCount
{
set{ pageCount=value;}
get{return pageCount;}
}

/// <summary>
/// 上边距
/// </summary>
public float Top
{
set{ _top=value;}
get{return _top;}
}

/// <summary>
/// 左边距
/// </summary>
public float Left
{
set{ _left=value;}
get{return _left;}
}

/// <summary>
/// 打印图片宽度
/// </summary>
public float DrawImageWidth
{
set{ drawImageWidth=value;}
get{return drawImageWidth;}
}

/// <summary>
/// 打印图片高度
/// </summary>
public float DrawImageHeight
{
set{ drawImageHeight=value;}
get{return drawImageHeight;}
}

/// <summary>
/// 定点打印
/// </summary>
public void PrintFixedPoint()
{
PrintDocument MyPrintDC = new PrintDocument();
PrintDialog MyPrintDg = new PrintDialog(); //创建打印对话框
MyPrintDg.Document = MyPrintDC;
if (MyPrintDg.ShowDialog() == DialogResult.OK)
{
try
{
iCount=printString.Length/pageCount;

MyPrintDC.PrintPage += new PrintPageEventHandler(PrintFixedPointEvent);
MyPrintDC.Print();
}
catch
{ //停止打印
MyPrintDC.PrintController.OnEndPrint(MyPrintDC, new PrintEventArgs());
}
}
}

/// <summary>
/// 执行定点打印操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PrintFixedPointEvent(object sender, PrintPageEventArgs e)
{
if(imagePath!="")
DrawImagePoint(sender, e);
Graphics MyGraphics = e.Graphics;
float x, y;
if((pageCount>=1) && (printString!=null) )
{
if(printString.Length>0)
if(printString[0]!=null)
{
pagePos++;

for(int i=iPos;i<iCount;i++)
{
x = _left*40 + X[i]*40;
y = _top*40 + Y[i]*40;
MyGraphics.DrawString(printString[i], _font, new SolidBrush(_color), x, y, new StringFormat());
}

if(pagePos<pageCount)
{
iPos=iCount;
iCount=iCount+(printString.Length/pageCount);
e.HasMorePages = true;
}
else
e.HasMorePages=false;
}
}
}

/// <summary>
/// 画图事件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DrawImagePoint(object sender, PrintPageEventArgs e)
{
if(!System.IO.File.Exists(imagePath))
return;
Image newImage = Image.FromFile(imagePath);
//Point ulCorner = new Point(xPoint, yPoint);
e.Graphics.DrawImage(newImage, xPoint, yPoint,drawImageWidth*40,drawImageHeight*40);
}

/// <summary>
///打印设置
/// </summary>
public void PrintSetup()
{
PrintDocument MyPrintDC = new PrintDocument();
PageSetupDialog MyPageSetupDg = new PageSetupDialog();
MyPageSetupDg.Document = MyPrintDC;
try
{
MyPageSetupDg.ShowDialog();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}

}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧