The following instances can be copied and used directly.
Overall idea: Save the print settings to a local file, read the file information directly at the next printing, and get the value through serialization and deserialization. In this example, only horizontal and vertical settings are set for printing. You can also add other settings to save and read them. Main method 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> /// Current System Path /// </Summary> Public static string globalpath = application. startuppath; /// <summary> /// whether to landscape // </Summary> Public bool pagesetishorizon; // <s Ummary> /// whether to print for the first time /// </Summary> Public bool isfirstp = true; /// <summary> /// master method /// </Summary> Public void memoryprint () {This. getdate (); printdocument fprintdocument = new printdocument (); fprintdocument. printpage + = new printpageeventhandler (printdocument_printpage); // print the content. Try {/** the setting box is displayed for the first printing and will be read from the configuration file */If (! Isfirstp) {fprintdocument. defaultpagesettings. landscape = pagesetishorizon;} else {// declare and instantiate printdialog pdlg = new printdialog (); // you can select the page pdlg. allowsomepages = true; // specify to print the document pdlg. document = fprintdocument; // displayed dialog box dialogresult result = pdlg. showdialog (); If (result = dialogresult. OK) {// Save the print settings fprintdocument = pdlg. document; pagesetishorizon = fprintdocument. defaultpagesettings. lan Dscape ;}// print preview printpreviewdialog PPD = new printpreviewdialog (); PPD. document = fprintdocument; PPD. showdialog (); // print fprintdocument. print (); // Save settings to local this. savedate ();} catch (system. drawing. printing. invalidprinterexception E1) {MessageBox. show ("no printer is installed. Please go to the System Control Panel to add a printer! "," Print ", messageboxbuttons. OK, messageboxicon. warning);} catch (exception ex) {MessageBox. show (ex. message, "print", messageboxbuttons. OK, messageboxicon. warning );}} /// <summary> /// print content /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void printdocument_printpage (Object sender, printpageeventargs e) {graphics G = E. graphics; // obtain the Drawing Object E. graphics. drawstring (" Printed .... ", New font (" Arial ", 20, fontstyle. bold), brushes. black, 150,125) ;}/// <summary> // Save the user-selected information to the file data/data. in dat /// </Summary> Public void savedate () {clssavedata CSD = NULL; If (file. exists (globalpath + "\ 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> // from data. DAT file removal user selection item /// </Summary> Public void getdate () {If (file. exists (globalpath + "\ data. dat ") {clssavedata CSD = clssavedata. deserialize ("data. dat ", globalpath +" \ data \ "); If (CSD = NULL | CSD. pagesetishorizon = NULL) {} else {isfirstp = CSD. isfirstp; pagesetishorizon = CSD. pagesetishorizon ;}}}}}Serialized object to save the vertical and horizontal settings
Using system; using system. collections. generic; using system. text; using system. io; using system. collections; namespace vistarenderer {[serializable] class clssavedata {public bool pagesetishorizon; // whether to print horizontally /// <summary> /// whether to print for the first time /// </Summary> Public bool isfirstp = true; /// <summary> /// serialize the object /// </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> /// deserialization /// </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 ;}}}
Serialization Tool
Using system; using system. windows. forms; using system. io; using system. runtime. serialization. formatters. binary; using system. XML. serialization; namespace vistarenderer {class clsserial {// <summary> /// path /// </Summary> private string Path = ""; /// <summary> /// object /// </Summary> private object OBJ = NULL; /// <summary> /// type /// </Summary> private type = NULL; Public clsserial () {}// <sum Mary> /// constructor /// </Summary> /// <Param name = "path"> </param> /// <Param name = "OBJ"> </param> Public clsserial (string path, object OBJ) {This. path = path; this. OBJ = OBJ ;} /// <summary> /// check type /// </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> /// serialization // </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> /// serialization // </Summary> Public void serializenow () {try {If (! Checkobj (OBJ) {MessageBox. Show ("this object cannot be serialized! "," System error "); return;} 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> /// deserialization /// </Summary> /// <Param name = "path"> </param> /// <returns> </returns> Public object deserializenow (string path) {try {This. path = P Ath; return deserializenow ();} catch (exception ex) {Throw new exception (ex. message, ex) ;}/// <summary> /// deserialization // </Summary> /// <returns> </returns> Public object deserializenow () {try {object OBJ = NULL; filestream = new filestream (path, filemode. open, fileaccess. read, fileshare. read); binaryformatter B = new binaryformatter (); OBJ = B. deserialize (filestream); filestream. clo Se (); Return OBJ;} catch (exception ex) {Throw new exception (ex. message, ex );}} /// <summary> /// XML serialization // </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 serialization // </su Mmary> Public void serializexmlnow () {try {If (! Checkobj (OBJ) {MessageBox. Show ("this object cannot be serialized! "," System error "); return;} 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 deserialization /// </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 deserialization /// </Summary> /// <returns> </returns> Public object deserializexmlnow () {try {If (! Checkobj (OBJ) {MessageBox. Show ("this object cannot be deserialized! "," System error "); return NULL;} object objxml = NULL; 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 );}}}}