下面這些執行個體都可以拷下直接用
總體思路:儲存列印設定資訊到本地檔案,下次列印的時候直接讀取檔案資訊,通過序列化與還原序列化來擷取值。本例只是針對列印的橫縱向進行設定,讀者也可以增加其他設定資訊進行儲存讀取。主方法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); } } }}