戏说 .NET GDI+系列学习教程(三、Graphics类的应用_打印收银小票)
#region 打印 /// <summary>
/// 打印字符串内容
/// </summary>
/// <returns></returns>
public string GetPrintStr()
{
StringBuilder sb = new StringBuilder();
int intLenMax = ;
if (!isScan)
{
intLenMax = ;
}
sb.Append("名称:" + CommonFunction.AutomaticLine(printGoods.goodsName.Trim(), , intLenMax));
sb.Append("价格:" + printGoods.retailPrice + "元\r\n");
return sb.ToString();
} /// <summary>
/// 打印条形码图片
/// 在生成条形码时,大小是不可以任意放大的,那么我们如果想任意改变条形码大小
/// 思路:
/// 1.把条形码生成为图片
/// 2.改变条形码图片的大小就可以了
/// </summary>
/// <returns></returns>
public System.Drawing.Image GetPrintImage()
{
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
//设置条形码宽度和高度
int W = Convert.ToInt32();
int H = Convert.ToInt32();
//对齐方式:默认居中
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
//条码格式
BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;
//显示生成条形码的内容
b.IncludeLabel = true; b.RotateFlipType = RotateFlipType.RotateNoneFlipNone;
//生成条形码内容的位置
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; System.Drawing.Font font = new System.Drawing.Font("verdana", 8f);//字体设置
b.LabelFont = font; //b.Encode(条形码格式,要生成条形码的内容,条形码颜色,条形码背景色,宽度,高度)
System.Drawing.Image image = b.Encode(type, printGoods.goodsShopId, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
return image;
} private void PrintStat()
{
PrintDocument pd = new PrintDocument();
//设置去除打印提示
PrintController printController = new StandardPrintController();
pd.PrintController = printController; //设置默认打印机
if (pd.PrinterSettings.PrinterName != PrinterName.BarCode)
Printer.ChangeDefaultPrinter(PrinterName.BarCode);
//设置边距
Margins margin = new Margins(, , , );
pd.DefaultPageSettings.Margins = margin; //纸张设置默认
PaperSize pageSize = new PaperSize("First custom size", getYc(), );
pd.DefaultPageSettings.PaperSize = pageSize; //打印事件设置
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); try
{
pd.Print();
}
catch (Exception ex)
{
InformaticaDialog information = new InformaticaDialog("打印失败", ex.Message);
information.Owner = this;
information.ShowDialog();
}
} private int getYc(double cm)
{
return (int)(cm / 25.4) * ;
} private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
SetInvoiceData(e.Graphics);
} private void SetInvoiceData(Graphics g)
{
SolidBrush GrayBrush = new SolidBrush(System.Drawing.Color.Black);
Font InvoiceFont = new Font("Arial", , System.Drawing.FontStyle.Regular);
g.DrawString(GetPrintStr(), InvoiceFont, GrayBrush, , );
//这个主要目的是要放到条形
g.DrawImage(GetPrintImage(), , , , );
g.Dispose();
}
#endregion
打印
#region 打印字符串自动换行 /// <summary>
/// 处理字符串自动换行问题。最短为intLenMin,最长为intLenMax。
/// </summary>
/// <param name="strOldText">原字符串</param>
/// <param name="intLenMin">最短字节长度</param>
/// <param name="intLenMax">最长字节长度</param>
/// <returns>string</returns>
/// <remarks></remarks>
public static string AutomaticLine(string strOldText, int intLenMin, int intLenMax)
{
int intLength = ;
string strResult = ""; //获取原字符串的字节长度
intLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(strOldText); if (intLength > intLenMax)
{
//总字节数> 最长截取的最长字节数,
//则截取最长字节数, 然后对剩余字符串再处理 //获取字符串的UCS2码
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(strOldText);
//获取字符的实际截取位置
int intCutPos = RealCutPos(bytes, intLenMax);
//采用递归调用
strResult = System.Text.Encoding.Unicode.GetString(bytes, , intCutPos * ) + "\r\n" + AutomaticLine(strOldText.Substring(intCutPos), intLenMin, intLenMax);
}
else if (intLength > intLenMin)
{
//如果 最长字节数 >总字节数 > 最短字节数,则 换行,并补齐空格到最短字节数位置
strResult = strOldText + "\r\n";// +"".PadRight(intLenMin);
}
else
{
//如果 总字节数 < 最短字节数,则直接补齐空格到最短字节数的位置
strResult = strOldText;// +"".PadRight(intLenMin - intLength);
}
return strResult;
} /// <summary>
/// 返回字符的实际截取位置
/// </summary>
/// <param name="bytes">UCS2码</param>
/// <param name="intLength">要截取的字节长度</param>
/// <returns></returns>
/// <remarks></remarks>
private static int RealCutPos(byte[] bytes, int intLength)
{
//获取UCS2编码
int intCountB = ;
// 统计当前的字节数
int intCutPos = ;
//记录要截取字节的位置 while ((intCutPos < bytes.GetLength() && intCountB < intLength))
{
// 偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
if (intCutPos % == )
{
// 在UCS2第一个字节时,字节数加1
intCountB += ;
}
else
{
// 当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节
if (bytes[intCutPos] > )
{
intCountB += ;
}
}
intCutPos += ;
} // 如果intCutPos为奇数时,处理成偶数
if (intCutPos % == )
{
// 该UCS2字符是汉字时,去掉这个截一半的汉字
if (bytes[intCutPos] > )
{
intCutPos = intCutPos - ;
}
else
{
// 该UCS2字符是字母或数字,则保留该字符
intCutPos = intCutPos + ;
}
} return intCutPos / ;
}
#endregion
打印字符串自动换行
注意:在生成条形码时,大小是不可以任意放大的,那么我们如果想任意改变条形码大小
思路:
1.把条形码生成为图片
2.改变条形码图片的大小就可以了
戏说 .NET GDI+系列学习教程(三、Graphics类的应用_打印收银小票)的更多相关文章
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码扩展)
从别人那拷贝的 #region 定义和初始化配置字段 //用户存取验证码字符串 public string validationCode = String.Empty; //生成的验证码字符串 pub ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码)
关于Graphics也有了基本了解下面想说的的是学这个东东干什么呢,到底如何应用目前常见应用1.验证码(参照网上的)2.打印排版(会提到关于条形码大小设置)3.自定义控件 一.验证码 class Va ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)
如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)
- 戏说 .NET GDI+系列学习教程(一、Graphics类--纸)
Graphics类(纸) Graphics类封装一个GDI+绘图图面,提供将对象绘制到显示设备的方法,Graphics与特定的设备上下文关联. 画图方法都被包括在Graphics类中,在画任何对象时, ...
- 戏说 .NET GDI+系列学习教程(二、Graphics类的方法)
一.DrawBezier 画立体的贝尔塞曲线 private void frmGraphics_Paint(object sender, PaintEventArgs e) { Graphics g ...
- VB6 GDI+ 入门教程[7] Graphics 其他内容
http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[7] Graphics 其他内容 2009 年 9 月 ...
- Win32中GDI+应用(三)---Graphics类
在我理解看来,Graphics是一个device context和你的drawing conetent之间的一个中介.它存储了device context的相关属性,以及drawing content ...
- C# GDI+之Graphics类 z
GDI+是GDI的后继者,它是.NET Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘制各种图形图像,可以用于绘制各种数据图像.数学仿真等. Graphics类是GDI+的核心, ...
随机推荐
- webservice 应用
一直以来,dashboard就会面临一个非常难堪的问题.就是刷新速度太慢了.它要连接query 来获取数据.而query每刷一次都需要时间.这是无可避免的结果.尽管它也是结果集,可还是比较慢.最近实践 ...
- kafka 简单安装以及java小demo
文章目录 第1步,下载解压 kafka: 第2步,运行 kafka: 第3步,创建topic 第4步,生产者发送消息 第5步,消费者接收消息 使用 java 客户端 kafka 0.8.0版本demo ...
- HBase 入门之数据刷写(Memstore Flush)详细说明
接触过 HBase 的同学应该对 HBase 写数据的过程比较熟悉(不熟悉也没关系).HBase 写数据(比如 put.delete)的时候,都是写 WAL(假设 WAL 没有被关闭) ,然后将数据写 ...
- jQuery层次选择器再探究(原创)
关于层次选择器的详解: 1)可以选取某一个元素的所有的后代元素,得到一个jQuery对象的集合--->$('prev descendant') 2)可以选取某一个元素的子辈的所有的元素,得到一个 ...
- mysql 密码
http://www.cnblogs.com/jonsea/p/5510219.html character-set-server=utf8 mysql 修改密码: ALTER USER 'root' ...
- CSS3新增(选择器{属性选择器,结构伪类选择器,伪元素选择器})
1.属性选择器 属性选择器,可以根据元素特定的属性来选择元素,这样就不用借助 类 或者 id选择器. E [ att ] 选择具有 att 属性的 E 元素 例如:input [ value ...
- Alpha版本后的心得体会
Alpha版本后的心得体会 在我们一系列的努力之下,我们团队打造的校园互助式快递代取APP——U-Help的α版本终于能够问世了.尽管这个版本存在着这样那样的问题,但是我们还是对此抱有充足的信心.另一 ...
- stdin stdout stderr - 标准 I/O 流
Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr; D ...
- JNI原理与静态、动态注册
前言 JNI不仅仅在NDK开发中应用,它更是Android系统中Java与Native交互的桥梁,不理解JNI的话,你就只能停留在Java Framework层.这一个系列我们来一起深入学习JNI. ...
- linux调用本地镜像
首先先让系统显示出来 iso 已经挂载 然后#mkdir /mnt/cdrom #mount /dev/cdrom /mnt/cdrom #df -H 查看是否已经挂载上 #cd /etc/ ...