/// <summary> /// Save file. /// </summary> /// <param name="type"></param> /// <returns></returns> public bool SaveDocument(SaveType type) { // Get the file name string newFileName = this.fileName; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = fileDlgFilter; if ((type == SaveType.SaveAs) || Empty(newFileName)) { if (!Empty(newFileName)) { saveFileDialog1.InitialDirectory = Path.GetDirectoryName(newFileName); saveFileDialog1.FileName = Path.GetFileName(newFileName); } else { saveFileDialog1.InitialDirectory = fileDlgInitDir; saveFileDialog1.FileName = newDocName; } DialogResult res = saveFileDialog1.ShowDialog(frmOwner); if (res != DialogResult.OK) { return(false); } newFileName = saveFileDialog1.FileName; fileDlgInitDir = new FileInfo(newFileName).DirectoryName; } // Write the data try { using (Stream stream = new FileStream( newFileName, FileMode.Create, FileAccess.Write)) { // Serialize object to text format IFormatter formatter = new BinaryFormatter(); if (SaveEvent != null) // if caller subscribed to this event { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, newFileName); // raise event SaveEvent(this, args); if (args.Error) { return(false); } } } } catch (ArgumentNullException ex) { return(HandleSaveException(ex, newFileName)); } catch (ArgumentOutOfRangeException ex) { return(HandleSaveException(ex, newFileName)); } catch (ArgumentException ex) { return(HandleSaveException(ex, newFileName)); } catch (SecurityException ex) { return(HandleSaveException(ex, newFileName)); } catch (FileNotFoundException ex) { return(HandleSaveException(ex, newFileName)); } catch (DirectoryNotFoundException ex) { return(HandleSaveException(ex, newFileName)); } catch (PathTooLongException ex) { return(HandleSaveException(ex, newFileName)); } catch (IOException ex) { return(HandleSaveException(ex, newFileName)); } // Clear the dirty bit, cache the new file name // and the caption is set automatically Dirty = false; SetFileName(newFileName); // Success return(true); }
/// <summary> /// ����ģ��������ļ� /// </summary> /// <param name="filePath"></param> /// <returns></returns> public bool SaveDocumentTemplate(string filePath) { using (Stream stream = new FileStream( filePath, FileMode.Create, FileAccess.Write)) { // Serialize object to text format IFormatter formatter = new BinaryFormatter(); // IFormatter formatter = new XmlSerializer(); if (SaveTemplateEvent != null) // if caller subscribed to this event { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, filePath); SaveTemplateEvent(this, args); if (args.Error) return false; } } return true; }
/// <summary> /// Open document /// </summary> /// <param name="newFileName"> /// Document file name. Empty - function shows Open File dialog. /// </param> /// <returns></returns> public bool OpenDocument(string newFileName) { // Check if we can close current file if (!CloseDocument()) { return(false); } // Get the file to open if (Empty(newFileName)) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = fileDlgFilter; openFileDialog1.InitialDirectory = fileDlgInitDir; DialogResult res = openFileDialog1.ShowDialog(frmOwner); if (res != DialogResult.OK) { return(false); } newFileName = openFileDialog1.FileName; fileDlgInitDir = new FileInfo(newFileName).DirectoryName; } // Read the data try { using (Stream stream = new FileStream( newFileName, FileMode.Open, FileAccess.Read)) { // Deserialize object from text format IFormatter formatter = new BinaryFormatter(); if (LoadEvent != null) // if caller subscribed to this event { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, newFileName); // raise event to load document from file LoadEvent(this, args); if (args.Error) { // report failure if (OpenEvent != null) { OpenEvent(this, new OpenFileEventArgs(newFileName, false)); } return(false); } // raise event to show document in the window if (DocChangedEvent != null) { DocChangedEvent(this, new EventArgs()); } } } } // Catch all exceptions which may be raised from this code. // Caller is responsible to handle all other exceptions // in the functions invoked by LoadEvent and DocChangedEvent. catch (ArgumentNullException ex) { return(HandleOpenException(ex, newFileName)); } catch (ArgumentOutOfRangeException ex) { return(HandleOpenException(ex, newFileName)); } catch (ArgumentException ex) { return(HandleOpenException(ex, newFileName)); } catch (SecurityException ex) { return(HandleOpenException(ex, newFileName)); } catch (FileNotFoundException ex) { return(HandleOpenException(ex, newFileName)); } catch (DirectoryNotFoundException ex) { return(HandleOpenException(ex, newFileName)); } catch (PathTooLongException ex) { return(HandleOpenException(ex, newFileName)); } catch (IOException ex) { return(HandleOpenException(ex, newFileName)); } // Clear dirty bit, cache the file name and set the caption Dirty = false; SetFileName(newFileName); if (OpenEvent != null) { // report success OpenEvent(this, new OpenFileEventArgs(newFileName, true)); } // Success return(true); }
public object OpenDocumentTemplate(string fileName) { try { using (Stream stream = new FileStream( fileName, FileMode.Open, FileAccess.Read)) { IFormatter formatter = new BinaryFormatter(); if (LoadTemplateEvent != null) { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, fileName); return LoadTemplateEvent(this, args); } } } catch (ArgumentNullException ex) { return HandleOpenException(ex, fileName); } catch (ArgumentOutOfRangeException ex) { return HandleOpenException(ex, fileName); } catch (ArgumentException ex) { return HandleOpenException(ex, fileName); } catch (SecurityException ex) { return HandleOpenException(ex, fileName); } catch (FileNotFoundException ex) { return HandleOpenException(ex, fileName); } catch (DirectoryNotFoundException ex) { return HandleOpenException(ex, fileName); } catch (PathTooLongException ex) { return HandleOpenException(ex, fileName); } catch (IOException ex) { return HandleOpenException(ex, fileName); } catch (Exception ex) { return HandleOpenException(ex, fileName); } return null; }
public bool SaveDocument(SaveType type) { // Get the file name string newFileName = this.fileName; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = fileDlgFilter; if ((type == SaveType.SaveAs) || Empty(newFileName)) { if (!Empty(newFileName)) { saveFileDialog1.InitialDirectory = Path.GetDirectoryName(newFileName); saveFileDialog1.FileName = Path.GetFileName(newFileName); } else { saveFileDialog1.InitialDirectory = fileDlgInitDir; saveFileDialog1.FileName = newDocName; } DialogResult res = saveFileDialog1.ShowDialog(frmOwner); if (res != DialogResult.OK) return false; newFileName = saveFileDialog1.FileName; fileDlgInitDir = new FileInfo(newFileName).DirectoryName; } // Write the data try { using (Stream stream = new FileStream( newFileName, FileMode.Create, FileAccess.Write)) { // Serialize object to text format IFormatter formatter = new BinaryFormatter(); // IFormatter formatter = new XmlSerializer(); if (SaveEvent != null) // if caller subscribed to this event { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, newFileName); // raise event SaveEvent(this, args); if (args.Error) return false; } } } catch (ArgumentNullException ex) { return HandleSaveException(ex, newFileName); } catch (ArgumentOutOfRangeException ex) { return HandleSaveException(ex, newFileName); } catch (ArgumentException ex) { return HandleSaveException(ex, newFileName); } catch (SecurityException ex) { return HandleSaveException(ex, newFileName); } catch (FileNotFoundException ex) { return HandleSaveException(ex, newFileName); } catch (DirectoryNotFoundException ex) { return HandleSaveException(ex, newFileName); } catch (PathTooLongException ex) { return HandleSaveException(ex, newFileName); } catch (IOException ex) { return HandleSaveException(ex, newFileName); } // Clear the dirty bit, cache the new file name // and the caption is set automatically Dirty = false; SetFileName(newFileName); if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("����ɹ���"); } // Success return true; }
/// <summary> /// Save document to stream supplied by DocManager /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void docManager_SaveEvent(object sender, SerializationEventArgs e) { // DocManager asks to save document to supplied stream try { e.Formatter.Serialize(e.SerializationStream, TheLayers); } catch (Exception ex) { } }
public bool OpenDocument(string newFileName) { // Check if we can close current file DialogResult dr = ClearDrawArea(); if (dr == DialogResult.OK) { //SaveSettingsToRegistry(); } else if (dr == DialogResult.Cancel) { } // Get the file to open if (Empty(newFileName)) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = fileDlgFilter; openFileDialog1.InitialDirectory = fileDlgInitDir; DialogResult res = openFileDialog1.ShowDialog(frmOwner); if (res != DialogResult.OK) return false; newFileName = openFileDialog1.FileName; fileDlgInitDir = new FileInfo(newFileName).DirectoryName; } // Read the data try { using (Stream stream = new FileStream( newFileName, FileMode.Open, FileAccess.Read)) { // Deserialize object from text format IFormatter formatter = new BinaryFormatter(); if (LoadEvent != null) // if caller subscribed to this event { SerializationEventArgs args = new SerializationEventArgs( formatter, stream, newFileName); // raise event to load document from file LoadEvent(this, args); if (args.Error) { // report failure if (OpenEvent != null) { OpenEvent(this, new OpenFileEventArgs(newFileName, false)); } return false; } // raise event to show document in the window if (DocChangedEvent != null) { DocChangedEvent(this, new EventArgs()); } } } } catch (ArgumentNullException ex) { return HandleOpenException(ex, newFileName); } catch (ArgumentOutOfRangeException ex) { return HandleOpenException(ex, newFileName); } catch (ArgumentException ex) { return HandleOpenException(ex, newFileName); } catch (SecurityException ex) { return HandleOpenException(ex, newFileName); } catch (FileNotFoundException ex) { return HandleOpenException(ex, newFileName); } catch (DirectoryNotFoundException ex) { return HandleOpenException(ex, newFileName); } catch (PathTooLongException ex) { return HandleOpenException(ex, newFileName); } catch (IOException ex) { return HandleOpenException(ex, newFileName); } // Clear dirty bit, cache the file name and set the caption Dirty = false; SetFileName(newFileName); if (OpenEvent != null) { // report success OpenEvent(this, new OpenFileEventArgs(newFileName, true)); } // Success return true; }
/// <summary> /// Handle exception from docManager_SaveEvent function /// </summary> /// <param name="ex"></param> /// <param name="fileName"></param> private void HandleSaveException(Exception ex, SerializationEventArgs e) { MessageBox.Show(this, "Save File operation failed. File name: " + e.FileName + "\n" + "Reason: " + ex.Message, Application.ProductName); e.Error = true; }
/// <summary> /// Load document from the stream supplied by DocManager /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void docManager_LoadEvent(object sender, SerializationEventArgs e) { try { string fileDlgInitDir = new FileInfo(e.FileName).DirectoryName; TheLayers = (Layers)e.Formatter.Deserialize(e.SerializationStream); } catch (Exception ex) { } }
/// <summary> /// Load document from the stream supplied by DocManager /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void docManager_LoadEvent(object sender, SerializationEventArgs e) { // DocManager asks to load document from supplied stream try { drawArea.GraphicsList = (GraphicsList)e.Formatter.Deserialize(e.SerializationStream); } catch (ArgumentNullException ex) { HandleLoadException(ex, e); } catch (SerializationException ex) { HandleLoadException(ex, e); } catch (SecurityException ex) { HandleLoadException(ex, e); } }
/// <summary> /// Save document to stream supplied by DocManager /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void docManager_SaveEvent(object sender, SerializationEventArgs e) { // DocManager asks to save document to supplied stream try { e.Formatter.Serialize(e.SerializationStream, drawArea.GraphicsList); } catch (ArgumentNullException ex) { HandleSaveException(ex, e); } catch (SerializationException ex) { HandleSaveException(ex, e); } catch (SecurityException ex) { HandleSaveException(ex, e); } }
/// <summary> /// Handle exception from docManager_LoadEvent function /// </summary> /// <param name="ex"></param> /// <param name="e"></param> private void HandleLoadException(Exception ex, SerializationEventArgs e) { MessageBox.Show(this, "Open File operation failed. File name: " + e.FileName + "\n" + "Reason: " + ex.Message, System.Windows.Forms.Application.ProductName); e.Error = true; }
/// <summary> /// ����ģ�� /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void docManager_SaveTemplateEvent(object sender, SerializationEventArgs e) { try { e.Formatter.Serialize(e.SerializationStream, drawArea.TheLayers.ActiveLayer.Graphics); } catch (ArgumentNullException ex) { HandleSaveException(ex, e); } catch (SerializationException ex) { HandleSaveException(ex, e); } catch (SecurityException ex) { HandleSaveException(ex, e); } catch (Exception ex) { HandleSaveException(ex, e); } }
private object docManager_LoadTemplateEvent(object sender, SerializationEventArgs e) { try { GraphicsList graphicsList = (GraphicsList)e.Formatter.Deserialize(e.SerializationStream); return graphicsList; } catch (ArgumentNullException ex) { HandleLoadException(ex, e); } catch (SerializationException ex) { HandleLoadException(ex, e); } catch (SecurityException ex) { HandleLoadException(ex, e); } catch (Exception ex) { HandleLoadException(ex, e); } return null; }
private void docManager_LoadEventText(object sender, SerializationEventArgs e) { // DocManager asks to load document from supplied stream try { string fileDlgInitDir = new FileInfo(e.FileName).DirectoryName; //StaticHelper a = StaticHelper.getinstance(); //a.filedir = fileDlgInitDir; Layers temp = (Layers)e.Formatter.Deserialize(e.SerializationStream); for (int i = 0; i < temp[0].Graphics.Count; i++) { DrawObject o = temp[0].Graphics[i]; drawArea.TheLayers[0].Graphics.Add(o); } drawArea.UndoManager.Layers = drawArea.TheLayers; } catch (ArgumentNullException ex) { HandleLoadException(ex, e); } catch (SerializationException ex) { HandleLoadException(ex, e); } catch (SecurityException ex) { HandleLoadException(ex, e); } }
/// <summary> /// Load document from the stream supplied by DocManager /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void docManager_LoadEvent(object sender, SerializationEventArgs e) { try { string fileDlgInitDir = new FileInfo(e.FileName).DirectoryName; drawArea.TheLayers = (Layers)e.Formatter.Deserialize(e.SerializationStream); int al = this.drawArea.TheLayers.ActiveLayerIndex; drawArea.UndoManager.Layers = drawArea.TheLayers; } catch (ArgumentNullException ex) { HandleLoadException(ex, e); } catch (SerializationException ex) { HandleLoadException(ex, e); } catch (SecurityException ex) { HandleLoadException(ex, e); } }