#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. System.exit(0)和System.exit(1)区别(转)

    转:http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html 1.参考文献 http://hi.baidu.com/accpzh ...

  2. sql find_in_set在oracle下的解决方案

    比如一张表: artile (id,type,content); type:1表示文艺类,2表示小说类,3表示传记,4表示传说,等等5,6,7,8 表数据: id type content 1 3,1 ...

  3. linux命令行光标移动技巧

    看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下面工作.但是事情也不是那么简单,还是需要知道“如何去做” ...

  4. mysql 个人博客应用的建表和相关查询

    一.建表 用户表tb_user create table if not exists tb_user( user_id int auto_increment, ) not null, user_pas ...

  5. 使用element-ui 组件动态合并table的行/列(第二次修改)

    这是第二次修改,在通过调用后台接口返回来的时候,发现了代码中的问题:现在将博客中错误的地方改过来,添加备注 文章需求:动态实现table表格中行/列的自动合并 使用框架及UI类库:Vue+Elemen ...

  6. 硬RAID与软RAID的区别

    什么是RAID? RAID是英文Redundant Array of Independent Disks的缩写,翻译成中文即为独立磁盘冗余阵列,或简称磁盘阵列.简单的说,RAID是一种把多块独立的硬盘 ...

  7. Django中的缓存机制

    概述       对于中等流量网站来说,尽可能的减少开销是必要的.缓存数据就是为了保存那些需要很多计算资源大的结果,这样的的话就不必在下次重复消耗计算资源.     Django自带了一个健壮的缓存系 ...

  8. linux 下安装chrome的rpm包

    1. 下载chrome的rpm包,假设叫cho. 2.执行命令 rpm -ivh cho 3.报错提示需要lsb>=4.0,执行命令 yum install lsb 等待安装完毕. 4. 重新执 ...

  9. python 数据压缩

    zlib 压缩 import zlib import this s = this.s.encode('utf8')*10 for i in range(10): data = zlib.compres ...

  10. Java技术专区-虚拟机系列-虚拟机参数(常用)

    基础参数系类(内存分配) -server:一定要作为第一个参数,在多个CPU时性能佳 -Xmn:young generation的heap大小,一般设置为Xmx的3.4分之一-Xms:初始Heap大小 ...