#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. python四种方法实现去除列表中的重复元素

    转载:https://blog.csdn.net/together_cz/article/details/76201975 def func1(one_list): ''''' 使用集合,个人最常用 ...

  2. java线程池监控

    原因 最近在完善公司的基础发布平台的时候,使用到了一线程去做一些异步的事情,在开发环境和测试环境验证没有任何问题,但是在程序在生产运行一段时间后,发现没有得到自己想要的结果,为此开始了漫长的排查bug ...

  3. MySQL高级学习笔记(三):Mysql逻辑架构介绍、mysql存储引擎

    文章目录 Mysql逻辑架构介绍 总体概览 总体概览 mysql存储引擎 查看命令 看你的 mysql 现在已提供什么存储引擎 : 看你的 mysql 当前默认的存储引擎 : 各个引擎简介 MyISA ...

  4. ldap认证服务的搭建

    1. Ldap服务介绍 LDAP 全称轻量级目录访问协议(英文:Lightweight Directory Access Protocol),是一个运行在 TCP/IP 上的目录访问协议.目录是一个特 ...

  5. Example of dynamic programmatic description

    - Suppose there are some itineraries in the Itinerary page, and you want to check on all of them. Se ...

  6. 20140923 cin.get() getline cin

    #include<iostream> #include<string> using namespace std; int main() {     string title; ...

  7. Spring JAR下载地址

    包含3.2版本及以上 http://repo.spring.io/libs-release-local/org/springframework/spring/ 包含从2.0开始的所有版本 http:/ ...

  8. spark优化——依赖包传入HDFS_spark.yarn.jar和spark.yarn.archive的使用

    一.参数说明 启动Spark任务时,在没有配置spark.yarn.archive或者spark.yarn.jars时, 会看到不停地上传jar,非常耗时:使用spark.yarn.archive可以 ...

  9. 关于document.body.scrollTop用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  10. android中的ContentProvider实现数据共享

    为了在应用程序之间交换数据,android中提供了ContentProvider,ContentProvider是不同应用程序之间进行数据交换的标准API.当一个应用程序需要把自己的数据暴露给其他程序 ...