关于【pd_PrintPage】的错误。
本帖最后由 mucmucll 于 2013-10-15 20:06:49 编辑 最近准备做个POS打印的程序,借鉴了一下这个网页的内容:
http://www.cnblogs.com/weixing/p/3283182.html
新建了一个windows窗口项目,然后在默认创建的Form1上面拖了两个button控件,然后就直接粘贴下面的代码,无法编译,总是提示:
错误 CS1061: “Pos_Test.Form1”不包含“pd_PrintPage”的定义,并且找不到可接受类型为“Pos_Test.Form1”的第一个参数的扩展方法“pd_PrintPage”(是否缺少 using 指令或程序集引用?)
难道是忘了加命名空间了?请高手指点一下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespace Pos_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
//设置边距
Margins margin = new Margins(20, 20, 20, 20);
pd.DefaultPageSettings.Margins = margin;
////纸张设置默认
PaperSize pageSize = new PaperSize("First custom size", getYc(58), 600);
pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
ppd.Document = pd;
ppd.ShowDialog();
try
{
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
}
}
private int getYc(double cm)
{
return (int)(cm / 25.4) * 100;
}
public string GetPrintStr()
{
StringBuilder sb = new StringBuilder();
string tou = "伊尹餐饮公司";
string address = "深圳市罗湖区东门老街29号";
string saleID = "2010930233330";
string item = "项目";
decimal price = 25.00M;
int count = 5;
decimal total = 0.00M;
decimal fukuan = 500.00M;
sb.Append(" " + tou + " /n");
sb.Append("-----------------------------/n");
sb.Append("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID + "/n");
sb.Append("-----------------------------/n");
sb.Append("项目" + "/t/t" + "数量" + "/t" + "单价" + "/t" + "小计" + "/n");
for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;
sb.Append(item + (i + 1) + "/t/t" + (i + 1) + "/t" + price + "/t" + xiaoji);
total += xiaoji;
if (i != (count))
sb.Append("/n");
}
sb.Append("-----------------------------/n");
sb.Append("数量: " + count + " 合计: " + total + "/n");
sb.Append("付款: 现金" + " " + fukuan);
sb.Append(" 现金找零:" + " " + (fukuan - total) + "/n");
sb.Append("-----------------------------/n");
sb.Append("地址:" + address + "/n");
sb.Append("电话:123456789 123456789/n");
sb.Append(" 谢谢惠顾欢迎下次光临 ");
return sb.ToString();
}
}
}