總體思路:保存打印設置信息到本地文件,下次打印的時候直接讀取文件信息,通過序列化與反序列化來獲取值。本例只是針對打印的橫縱向進行設置,讀者也可以增加其他設置信息進行保存讀取。
主方法MemoryPrint
[csharp] 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.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;
}
}
}
}
}
序列化的對象,保存縱橫向設置信息
[csharp] 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.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;
}
}
}
序列化工具類
[csharp] view plaincopyprint?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);
}
}
}
}
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);
}
}
}
}