概述
源码路径为
E:WGYC#学习一套开源的企业业务系统框架Winform586570 31天学会CRM项目开发pdf+源代码31天学会CRM项目开发-源代码 03 实例及拓展练习PrintDemo
设计的WMI操作参考https://blog.csdn.net/u011854789/article/details/52792083
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace PrintDemo
{
public partial class Form1 : Form
{
// 引用系统API的SetDefaultPrinter()方法,设置默认打印机
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//设置初始值
barEditItem3.EditValue = "无";//使用其上一层级的控件EditValue方法
//指定打印文档
this.printDialog1.Document = this.printDocument1;
this.pageSetupDialog1.Document = printDocument1;
this.printPreviewDialog1.Document = this.printDocument1;
}
/// <summary>
/// 执行打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//设置选定的默认打印机
SetDefaultPrinter(barEditItem3.EditValue.ToString());
//执行打印
this.printDocument1.Print();
}
/// <summary>
/// 页面设置的单击事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPageSet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//页面设置
//页边距设置
this.pageSetupDialog1.PageSettings.Margins.Left = 1;
this.pageSetupDialog1.PageSettings.Margins.Top = 1;
this.pageSetupDialog1.PageSettings.Margins.Bottom = 1;
this.pageSetupDialog1.PageSettings.Margins.Right = 1;
//确定后直接打印
if (this.pageSetupDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
this.printDocument1.Print();
}
/// <summary>
/// 打印机设置的单击事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrinterSet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//打印机设置
//确定后直接打印
if (this.printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
this.printDocument1.Print();
}
/// <summary>
/// 打印预览的单击事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintPreview_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.printDocument1.Print();
}
/// <summary>
/// 打印文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//当有多页需要打印时,需要该事件加入e.HasMorePages = true来告诉打印机还有更多页
//需继续打印。
//e.HasMorePages = true;
//设置打印机质量
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Font drawFont = new Font("Arial",14);
SolidBrush drawBrush = new SolidBrush(Color.Black);
float x = 3;float y = 3;
StringBuilder sb = new StringBuilder();
sb.Append("青岛**有限公司江山路店rn");
sb.Append("--------------------------rn");
sb.Append("品名 t单价t数量t金额rn");
sb.Append("打火机 t2.00t1.00t2.00rn");
sb.Append("青岛啤酒t10.00t30.00t300.00rn");
sb.Append("瓜子 t9.00t2.00t18.00rn");
sb.Append("橙汁 t12.00t1.00t12.00rn");
sb.Append("合计 t t t332.00rn");
sb.Append("--------------------------rn");
sb.Append(string.Format("打印日期: {0}rn 操作员:{1}rnrn",DateTime.Now.ToString("yyyy-MM-dd HH:mm"),"001"));
#if false
//打印文字
e.Graphics.DrawString(sb.ToString(), drawFont, drawBrush, x, y);
#else
//打印图片
this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
//创建一个空白图像
Bitmap b = new Bitmap(400,600);
//设置pictureBox1的图像
this.pictureBox1.Image = b;
Graphics gra = Graphics.FromImage(this.pictureBox1.Image);
gra.DrawString(sb.ToString(),drawFont,drawBrush,x,y);
//设置pictureBox1的图像
this.pictureBox1.Refresh();
e.Graphics.DrawImage(this.pictureBox1.Image, new Point(3, 3));
#endif
}
/// <summary>
/// 设置默认打印机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//需要引用System.Management命名空间
//首先遍历全部打印机,使用ManagementObjectSearcher对象
ManagementScope ms = new ManagementScope(ManagementPath.DefaultPath);
ms.Connect();
SelectQuery sq = new SelectQuery();
sq.QueryString = @"SELECT Name FROM WIN32_Printer";
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms,sq);
foreach (ManagementObject mo in mos.Get())
repositoryItemComboBox1.Items.Add(mo["Name"].ToString());
}
}
}
工具条使用了DevExpress16.2
最后
以上就是完美黄蜂为你收集整理的(打印机Printer)小票打印的全部内容,希望文章能够帮你解决(打印机Printer)小票打印所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复