#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类的应用_打印收银小票)的更多相关文章

  1. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码扩展)

    从别人那拷贝的 #region 定义和初始化配置字段 //用户存取验证码字符串 public string validationCode = String.Empty; //生成的验证码字符串 pub ...

  2. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码)

    关于Graphics也有了基本了解下面想说的的是学这个东东干什么呢,到底如何应用目前常见应用1.验证码(参照网上的)2.打印排版(会提到关于条形码大小设置)3.自定义控件 一.验证码 class Va ...

  3. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)

    如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...

  4. 戏说 .NET GDI+系列学习教程(三、Graphics类的方法的总结)

  5. 戏说 .NET GDI+系列学习教程(一、Graphics类--纸)

    Graphics类(纸) Graphics类封装一个GDI+绘图图面,提供将对象绘制到显示设备的方法,Graphics与特定的设备上下文关联. 画图方法都被包括在Graphics类中,在画任何对象时, ...

  6. 戏说 .NET GDI+系列学习教程(二、Graphics类的方法)

    一.DrawBezier 画立体的贝尔塞曲线 private void frmGraphics_Paint(object sender, PaintEventArgs e) { Graphics g ...

  7. VB6 GDI+ 入门教程[7] Graphics 其他内容

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[7] Graphics 其他内容 2009 年 9 月 ...

  8. Win32中GDI+应用(三)---Graphics类

    在我理解看来,Graphics是一个device context和你的drawing conetent之间的一个中介.它存储了device context的相关属性,以及drawing content ...

  9. C# GDI+之Graphics类 z

    GDI+是GDI的后继者,它是.NET Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘制各种图形图像,可以用于绘制各种数据图像.数学仿真等. Graphics类是GDI+的核心, ...

随机推荐

  1. 团队冲刺DAY4

    DES算法 算法概要 在DES.java当中创立两个方法分别用作加密和解密 通过 `public static byte[] encrypt(byte[] data, String sKey) 创建方 ...

  2. 57、saleforce学习笔记(四)

    List类 List在这里就是一个类 List<String> lists = new String[]{'1','3'}; List<String> list1 = new ...

  3. Linux / Unix Command: rz

    yum install lrzsz Most communications programs invoke rz and sz automatically. You can also connect ...

  4. Git 设置和取消代理(SOCKS5代理)

    设置代理 git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'socks ...

  5. 转 JMeter基础之--元件的作用域与执行顺序

    前面有介绍过jmeter的元件类别,对于新手来说,jmeter的元件是还是不少的,如果我们按照每一个元件的每一个参数的含义去学习,无疑会降低学习性能测试的热情,就算我们熟悉了所有元件以及元件上的参数了 ...

  6. springCloud的使用01-----服务的注册和发现

    1 搭建eureka注册服务器 1.1 创建springboot项目,导入相应的jar包依赖 <project xmlns="http://maven.apache.org/POM/4 ...

  7. redis 入门之列表

    lpush 将一个或多个值 value 插入到列表 key 的表头如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表头: 比如说,对空列表 mylist 执行命令 LPU ...

  8. UltraEdit常用快捷键

    UltraEdit是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII码,可以取代记事本,内建英文单字检查.C++及VB指令突显,可同时编辑多个文件,而且即使开启很大的文件速度也不会慢. 说 ...

  9. JQuery 全选 反选 获取Table 中指定td的元素值

    //全选 function initTableCheckbox() { var $thr = $('table thead tr'); var $checkAllTh = $('<th>& ...

  10. swagger2.0与spring结合

    官方文档: http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api   swagger是一个前后端api统一文档和测试框 ...