c#打印记忆功能

时间:2024-01-17 16:48:14

下面这些实例都可以拷下直接用

总体思路:保存打印设置信息到本地文件,下次打印的时候直接读取文件信息,通过序列化与反序列化来获取值。本例只是针对打印的横纵向进行设置,读者也可以增加其他设置信息进行保存读取。


主方法MemoryPrint


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
using System.Drawing; namespace VistaRenderer
{
class MemoryPrint
{
public MemoryPrint() { }
/// <summary>
/// 系统当前路径
/// </summary>
public static string GLOBALPATH = Application.StartupPath; /// <summary>
/// 是否横向
/// </summary>
public bool pagesetIsHorizon; /// <summary>
/// 是否第一次打印
/// </summary>
public bool isFirstP=true; /// <summary>
/// 主方法
/// </summary>
public void memoryPrint()
{
this.GetDate();
PrintDocument fPrintDocument = new PrintDocument();
fPrintDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);// 打印内容
try
{
/** 第一次打印弹出设置框,以后都是从配置文件中读取 */
if (!isFirstP)
{
fPrintDocument.DefaultPageSettings.Landscape = pagesetIsHorizon;
}
else
{
//申明并实例化PrintDialog
PrintDialog pDlg = new PrintDialog();
//可以选定页
pDlg.AllowSomePages = true;
//指定打印文档
pDlg.Document = fPrintDocument;
//显示对话框
DialogResult result = pDlg.ShowDialog();
if (result == DialogResult.OK)
{
//保存打印设置
fPrintDocument = pDlg.Document;
pagesetIsHorizon = fPrintDocument.DefaultPageSettings.Landscape; }
}
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = fPrintDocument;
ppd.ShowDialog();
//打印
fPrintDocument.Print();
//保存设置到本地
this.SaveDate();
}
catch (System.Drawing.Printing.InvalidPrinterException e1)
{
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} } /// <summary>
/// 打印内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics; //获得绘图对象
e.Graphics.DrawString("打印啦。。。。",
new Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125);
} /// <summary>
/// 保存用户选择信息到文件data/data.dat中
/// </summary>
public void SaveDate()
{ ClsSaveData csd = null;
if (File.Exists(GLOBALPATH + "\\data\\data.dat"))
{
csd = ClsSaveData.deSerialize("data.dat", GLOBALPATH + "\\data\\");
}
else
{
csd = new ClsSaveData();
}
csd.pagesetIsHorizon = pagesetIsHorizon;
csd.isFirstP = false;
ClsSaveData.serialize("data.dat", GLOBALPATH + "\\data\\", csd);
} /// <summary>
/// 从data.dat文件取出用户选择项
/// </summary>
public void GetDate()
{
if (File.Exists(GLOBALPATH + "\\data\\data.dat"))
{
ClsSaveData csd = ClsSaveData.deSerialize("data.dat", GLOBALPATH + "\\data\\");
if (csd == null || csd.pagesetIsHorizon == null)
{ }
else
{
isFirstP = csd.isFirstP;
pagesetIsHorizon = csd.pagesetIsHorizon;
} } }
}
}

序列化的对象,保存纵横向设置信息


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections; namespace VistaRenderer
{
[Serializable]
class ClsSaveData
{
public bool pagesetIsHorizon;//是否横向 /// <summary>
/// 是否第一次打印
/// </summary>
public bool isFirstP = true; /// <summary>
/// 序列化该对象
/// </summary>
/// <param name="fileName"></param>
/// <param name="path"></param>
/// <param name="obj"></param>
public static void serialize(string fileName, string path, object obj)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
ClsSerial serial = new ClsSerial(path + fileName, obj);
serial.SerializeNow();
} /// <summary>
/// 反序列化
/// </summary>
/// <param name="fileName"></param>
/// <param name="path"></param>
/// <returns></returns>
public static ClsSaveData deSerialize(string fileName, string path)
{
ClsSaveData csd = null;
if (File.Exists(path + fileName))
{
ClsSerial serial = new ClsSerial();
csd = (ClsSaveData)serial.DeSerializeNow(path + fileName);
}
return csd;
}
} }

序列化工具类


using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization; namespace VistaRenderer
{
class ClsSerial
{
/// <summary>
/// 路径
/// </summary>
private string path = "";
/// <summary>
/// 对象
/// </summary>
private object obj = null;
/// <summary>
/// 类型
/// </summary>
private Type type = null; public ClsSerial()
{
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public ClsSerial(string path, object obj)
{
this.path = path;
this.obj = obj;
}
/// <summary>
/// 检查类型
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool checkObj(object obj)
{
bool check = false;
type = obj.GetType();
if (type.IsClass || type.Name == "struct" || type.IsEnum || type.Name == "delegate")
check = true;
return check;
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public void SerializeNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
SerializeNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 序列化
/// </summary>
public void SerializeNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可序列化!", "系统错误");
return;
}
FileStream fileStream = new FileStream(path, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, obj);
fileStream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public object DeSerializeNow(string path)
{
try
{
this.path = path;
return DeSerializeNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public object DeSerializeNow()
{
try
{
object obj = null;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
obj = b.Deserialize(fileStream);
fileStream.Close();
return obj;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
public void SerializeXMLNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
SerializeXMLNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml序列化
/// </summary>
public void SerializeXMLNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可序列化!", "系统错误");
return;
}
FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer xmlFormatter = new XmlSerializer(type);
xmlFormatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml反序列化
/// </summary>
/// <param name="path"></param>
/// <param name="obj"></param>
/// <returns></returns>
public object DeSerializeXMLNow(string path, object obj)
{
try
{
this.path = path;
this.obj = obj;
return DeSerializeXMLNow();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// xml反序列化
/// </summary>
/// <returns></returns>
public object DeSerializeXMLNow()
{
try
{
if (!checkObj(obj))
{
MessageBox.Show("该对象不可反序列化!", "系统错误");
return null;
}
object objXml = null;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer formatter = new XmlSerializer(type);
obj = formatter.Deserialize(fileStream);
fileStream.Close();
return objXml;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
}