.NET平台下实现打印超市收银票据

问题描述:

    随着经济的发展和人类文明水平的提高,到超市采购日常用品已经是国人司空见惯的事情。首先收银员把客户所购买商品的销售数据存储到数据库,当交易结束后超市必须向客户提供收银票据。本实验要求学员设计并实现一种在.NET平台下打印超市收银票据的系统。

1、          选择字体

2、           

语言及环境:

实现语言:C#

实验环境:Visual Studio 2003(英文版),SQL Server 2000

实验目的:

1、熟练掌握.Net平台下的打印操作

2、复习ADO.NET接口的使用方法

3、提高C#语言编程能力

4、增加学员的项目经验

5、提高WinForm编程能力

问题分析:

界面设计设计如下:

使用PrintDocument类进行打印

PrintDocument类定义一个可再次使用的对象,该对象将输出发送到打印机。通常可以创建 PrintDocument 类的实例,设置描述打印方式的属性,然后调用 Print 方法开始打印进程。通过使用 PrintPageEventArgs 中包含的 Graphics 来处理用于指定打印输出的 PrintPage 事件。这个类包括四个事件:

BeginPrint 事件 在调用 Print 方法时并且在打印文档的第一页之前发生。通常可以处理 BeginPrint 事件以初始化字体、文件流和其他在打印过程中使用的资源。EndPrint 事件打印完文档的最后一页时发生。通常可以处理 EndPrint 事件以释放字体、文件流和其他在打印过程中使用的资源(如字体)。通过在 PrintPage 事件中将 PrintPageEventArgs.HasMorePages 属性设置为 false,可以指示没有更多要打印的页。如果打印进程被取消,或者打印进程中发生异常,也会发生 EndPrint 事件。

PrintPage 事件 当需要为当前页打印的输出时发生。事件处理程序接收一个 PrintPageEventArgs 类型的参数,它包含与此事件相关的数据。QueryPageSettings 事件在 PrintPage 事件的紧面前发生。用不同的页设置来打印文档的每一页是可能的。通过修改 QueryPageSettingsEventArgs.PageSettings 属性的各个属性,或者将该属性设置为 PageSettings,可以设置页设置。对 PageSettings 所做的更改只影响当前页,对文档的默认页设置没有影响。通过将 Cancel 属性设置为 QueryPageSettingsEventArgs 的 true,还可以取消打印作业。

使用FontDialog设置字体

FontDialog 类表示显示当前安装在系统中的字体列表的通用对话框。必须调用继承的成员 ShowDialog 才能创建此特定的通用对话框。在创建 FontDialog 的实例时,一些读/写属性被设置为初始值。Font 包含大小信息,但不包含颜色信息。

使用PrintDialog设置打印机和页面

允许用户选择一台打印机并选择文档中要打印的部分创建 PrintDialog 的实例时,以下读/写属性将设置为初始值。

属性

初始值

AllowSomePages

false

AllowPrintToFile

true

AllowSelection

false

Document

空引用(Visual Basic 中为 Nothing)

PrinterSettings

false

PrintToFile

空引用 (Nothing)

ShowHelp

false

ShowNetwork

true

使用ADO.NET读取数据数据

关键代码解析

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace PrintSellTickets

{

    /// <summary>

    /// Form1 的摘要说明。

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

       private System.Drawing.Printing.PrintDocument printDocument1;

       private System.Windows.Forms.PrintDialog printDialog1;

       private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;

       private System.Windows.Forms.MainMenu mainMenu1;

       private System.Windows.Forms.MenuItem menuItem1;

       private System.Windows.Forms.MenuItem menuItem2;

       private System.Windows.Forms.MenuItem menuItem3;

       private System.Windows.Forms.MenuItem menuItem4;

       private SqlConnection con =null;

       private SqlCommand com =null;

       private SqlDataReader sdr =null;

       private System.Windows.Forms.FontDialog fontDialog1;

        /// <summary>

       /// 必需的设计器变量。

       /// </summary>

       private System.ComponentModel.Container components = null;

       public Form1()

       {

           //

           // Windows 窗体设计器支持所必需的

           //

           InitializeComponent();

           //

           // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

           //

       }

       /// <summary>

       /// 清理所有正在使用的资源。

       /// </summary>

       protected override void Dispose( bool disposing )

       {

           if( disposing )

           {

              if (components != null)

              {

                  components.Dispose();

              }

           }

           base.Dispose( disposing );

       }

       #region Windows 窗体设计器生成的代码

       /// <summary>

       /// 设计器支持所需的方法 - 不要使用代码编辑器修改

       /// 此方法的内容。

       /// </summary>

       private void InitializeComponent()

       {

           System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

           this.printDocument1 = new System.Drawing.Printing.PrintDocument();

           this.printDialog1 = new System.Windows.Forms.PrintDialog();

           this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();

           this.mainMenu1 = new System.Windows.Forms.MainMenu();

           this.menuItem1 = new System.Windows.Forms.MenuItem();

           this.menuItem2 = new System.Windows.Forms.MenuItem();

           this.menuItem3 = new System.Windows.Forms.MenuItem();

           this.menuItem4 = new System.Windows.Forms.MenuItem();

           this.fontDialog1 = new System.Windows.Forms.FontDialog();

           //

           // printDocument1

           //

           this.printDocument1.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.printDocument1_BeginPrint);

           this.printDocument1.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.printDocument1_EndPrint);

           this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);

           //

           // printPreviewDialog1

           //

           this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);

           this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);

           this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);

           this.printPreviewDialog1.Enabled = true;

           this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));

           this.printPreviewDialog1.Location = new System.Drawing.Point(357, 24);

           this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);

           this.printPreviewDialog1.Name = "printPreviewDialog1";

           this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;

           this.printPreviewDialog1.Visible = false;

           //

           // mainMenu1

           //

           this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

                                                                           this.menuItem1});

           //

           // menuItem1

           //

           this.menuItem1.Index = 0;

           this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

                                                                           this.menuItem2,

                                                                           this.menuItem3,

                                                                           this.menuItem4});

           this.menuItem1.Text = "打印";

           //

           // menuItem2

           //

           this.menuItem2.Index = 0;

           this.menuItem2.Text = "打印设置";

           this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);

           //

           // menuItem3

           //

           this.menuItem3.Index = 1;

           this.menuItem3.Text = "打印预览及打印";

           this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);

           //

           // menuItem4

           //

           this.menuItem4.Index = 2;

           this.menuItem4.Text = "打印";

           this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);

           //

           // Form1

           //

           this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

           this.ClientSize = new System.Drawing.Size(712, 494);

           this.Menu = this.mainMenu1;

           this.Name = "Form1";

           this.Text = "Form1";

           this.Load += new System.EventHandler(this.Form1_Load);

       }

       #endregion

       /// <summary>

       /// 应用程序的主入口点。

       /// </summary>

       [STAThread]

       static void Main()

       {

           Application.Run(new Form1());

       }

       private void button1_Click(object sender, System.EventArgs e)

       {

          

       }

       private void menuItem2_Click(object sender, System.EventArgs e)

       {

           this.printDialog1.Document =this.printDocument1;

           this.printDialog1.ShowDialog();

      

       }

       private void menuItem3_Click(object sender, System.EventArgs ev)

       {

           this.printPreviewDialog1.Document = this.printDocument1;

           this.printPreviewDialog1.ShowDialog();

       }

       private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)

       {

           Font printFont;

           if(this.fontDialog1.ShowDialog() ==DialogResult.OK)

           {

               printFont=this.fontDialog1.Font;

           }

           else

           {

              printFont = new Font("宋体",16);

           }

           float linesPerPage = 0;

           float yPos = 0;

           int count =0;

           float leftMargin = ev.MarginBounds.Left;

           float topMargin = ev.MarginBounds.Top;

           string line = null;

           string subs ="                                                                          ";

           string context = null;

           float max=0,min=0;

           // Calculate the number of lines per page.

           linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

           yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

/*

DrawStringDrawString 方法 在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。使用指定 StringFormat 对象的格式化属性,用指定的 Brush 和 Font 对象在指定的位置绘制指定的文本字符串。

public void DrawString(

   string s,

   Font font,

   Brush brush,

   float x,

   float y,

   StringFormat format

);

参数

s

要绘制的字符串。

font

Font 对象,它定义字符串的文本格式。

brush

Brush 对象,它确定所绘制文本的颜色和纹理。

x 所绘制文本的左上角的 x 坐标。

y 所绘制文本的左上角的 y 坐标。

format StringFormat 对象,它指定应用于所绘制文本的格式化属性(如行距和对齐方式)。

返回值

此方法不返回值。

*/

           ev.Graphics.DrawString("欢迎光临北大青鸟沈阳恒基店", printFont, Brushes.Black,

              leftMargin, yPos, new StringFormat());

           count++;

           yPos = topMargin + (count *

              printFont.GetHeight(ev.Graphics));

           ev.Graphics.DrawString("=======================================", printFont, Brushes.Black,

              leftMargin, yPos, new StringFormat());

           count++;

           // Print each line of the file.

           while(sdr.Read())

           {

              line =null;

              min+=(byte)sdr["min_lvl"];

              max+=(byte)sdr["max_lvl"];

              context = sdr["job_desc"].ToString();

              line+=context + subs.Substring(0,30 - context.Length);

              context = sdr["min_lvl"].ToString();

              line+= subs.Substring(0,6 - context.Length)+context ;

              context = sdr["max_lvl"].ToString();

              line+=subs.Substring(0,6 - context.Length)+context ;

              yPos = topMargin + (count *

                  printFont.GetHeight(ev.Graphics));

              ev.Graphics.DrawString(line, printFont, Brushes.Black,

                  leftMargin, yPos, new StringFormat());

              count++;

           }

           count++;

           yPos = topMargin + (count *

              printFont.GetHeight(ev.Graphics));

           ev.Graphics.DrawString("=======================================", printFont, Brushes.Black,

              leftMargin, yPos, new StringFormat());

           count++;

           yPos = topMargin + (count *

              printFont.GetHeight(ev.Graphics));

           ev.Graphics.DrawString("合计:                           "+min.ToString() +"   "+ max.ToString(), printFont, Brushes.Black,

              leftMargin, yPos, new StringFormat());

           count++;

           yPos = topMargin + (count *

              printFont.GetHeight(ev.Graphics));

           ev.Graphics.DrawString("谢谢回顾!", printFont, Brushes.Black,

              leftMargin, yPos, new StringFormat());

           // If more lines exist, print another page.

           ev.HasMorePages = false;

          

       }

       private void menuItem4_Click(object sender, System.EventArgs e)

       {

           this.printDocument1.Print();

       }

       private void Form1_Load(object sender, System.EventArgs e)

       {

      

       }

       private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)

       {

           //该事件在文档打印前发生

           //连接数据库

           con =new SqlConnection("server = .;database = pubs;trusted_connection = sspi");

           try

           {

              con.Open();

              com = con.CreateCommand();

              com.CommandText ="select * from dbo.jobs ";

              sdr = com.ExecuteReader();

           }

           catch

           {

           }

      

       }

       private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)

       {//结束打印,关闭连接

           try

           {

              sdr.Close();

              con.Close();

           }

           catch

           {

           }

      

       }

    }

}

存在问题及解决

       应该把字体设置和打印机设置在系统调用时设置一次。

问题扩充

posted on 2007-11-15 20:10  段静迪  阅读(1412)  评论(0编辑  收藏  举报