Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Xml.Serialization;
- using System.Xml;
- namespace System.Windows
- {
- /*
- public MainWindow()
- {
- InitializeComponent();
- WindowPositionData.LoadWindowPosision(this);
- this.Closing += (s, e) => WindowPositionData.SaveWindowPosision(this);
- }
- */
- /// <summary>
- /// Class used to persist window's position
- /// </summary>
- [XmlRoot("WindowPositionData")]
- [Serializable]
- public class WindowPositionData
- {
- public double WindowTop { get; set; }
- public double WindowLeft { get; set; }
- public double WindowHeight { get; set; }
- public double WindowWidth { get; set; }
- public System.Windows.WindowState WindowState { get; set; }
- public bool AllValuesZero()
- {
- return WindowTop <= 0 && WindowLeft <= 0 && WindowHeight <= 0 && WindowWidth <= 0;
- }
- #region static methods
- public static void LoadWindowPosision(Window win)
- {
- WindowPositionData data = GetDataFromFile(win);
- SetWindowPosition(win, data);
- }
- public static void SaveWindowPosision(Window win)
- {
- WindowPositionData data = new WindowPositionData();
- GetWindowPosition(win, data);
- SaveDataToFile(win, data);
- }
- public static void GetWindowPosition(Window win, WindowPositionData data)
- {
- data.WindowWidth = win.Width;
- data.WindowHeight = win.Height;
- data.WindowTop = win.Top;
- data.WindowLeft = win.Left;
- data.WindowState = win.WindowState;
- }
- public static void SetWindowPosition(Window win, WindowPositionData data)
- {
- if (data.AllValuesZero())
- {
- win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
- }
- else
- {
- win.WindowStartupLocation = WindowStartupLocation.Manual;
- }
- if (data.WindowWidth > 0)
- win.Width = data.WindowWidth;
- if (data.WindowHeight > 0)
- win.Height = data.WindowHeight;
- if (data.WindowTop > 0)
- win.Top = data.WindowTop;
- else
- {
- if (double.IsNaN(win.Top))
- win.Top = 0;
- }
- if (data.WindowLeft > 0)
- win.Left = data.WindowLeft;
- else
- {
- if (double.IsNaN(win.Left))
- win.Left = 0;
- }
- SizeToFit(win);
- if (data.WindowState != WindowState.Minimized)
- {
- win.WindowState = data.WindowState;
- }
- }
- static WindowPositionData GetDataFromFile(Window win)
- {
- string datafilePath = GetDataFilePath(win);
- try
- {
- if (File.Exists(datafilePath))
- {
- return WindowPositionData.DeserializeFromXml(File.ReadAllText(datafilePath));
- }
- else
- return new WindowPositionData();
- }
- catch
- {
- return new WindowPositionData();
- }
- }
- static void SaveDataToFile(Window win, WindowPositionData data)
- {
- string datafilePath = GetDataFilePath(win);
- string xml = WindowPositionData.SerializeToXml(data);
- File.WriteAllText(datafilePath, xml);
- }
- static string GetDataFilePath(Window win)
- {
- const string DATA_FILE_NAME = "WindowPosisionData.xml";
- const string DATA_FILE_FORMAT = "WindowPosisionData_{0}.xml";
- string filename = DATA_FILE_NAME;
- if (win != null)
- filename = string.Format(DATA_FILE_FORMAT, win.GetType().Name);
- string dirpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
- dirpath = Path.Combine(dirpath, "Company", "App");
- if (!Directory.Exists(dirpath))
- Directory.CreateDirectory(dirpath);
- return Path.Combine(dirpath, filename);
- }
- static string SerializeToXml(WindowPositionData data)
- {
- if (data == null)
- throw new ArgumentNullException("data");
- return XmlSerialization.Serialize<WindowPositionData>(data);
- }
- static WindowPositionData DeserializeFromXml(string xml)
- {
- if (string.IsNullOrEmpty(xml))
- throw new ArgumentNullException("xml");
- return XmlSerialization.Deserialize<WindowPositionData>(xml);
- }
- static void GetWindowPosition(Window win)
- {
- WindowPositionData data = new WindowPositionData();
- GetWindowPosition(win, data);
- }
- /// <summary>
- /// If the window is more than half off of the screen move it up and to the left
- /// so half the height and half the width are visible, and If the saved window dimensions are larger than the current screen shrink the
- /// window to fit.
- /// </summary>
- public static void SizeToFit(Window win)
- {
- if (win.Height > System.Windows.SystemParameters.VirtualScreenHeight)
- {
- win.Height = System.Windows.SystemParameters.VirtualScreenHeight;
- }
- if (win.Width > System.Windows.SystemParameters.VirtualScreenWidth)
- {
- win.Width = System.Windows.SystemParameters.VirtualScreenWidth;
- }
- if (win.Top + win.Height / 2 > System.Windows.SystemParameters.VirtualScreenHeight)
- {
- win.Top = System.Windows.SystemParameters.VirtualScreenHeight - win.Height;
- }
- if (win.Left + win.Width / 2 > System.Windows.SystemParameters.VirtualScreenWidth)
- {
- win.Left = System.Windows.SystemParameters.VirtualScreenWidth - win.Width;
- }
- if (win.Top < 0)
- {
- win.Top = 0;
- }
- if (win.Left < 0)
- {
- win.Left = 0;
- }
- }
- /// <summary>
- /// If the window is more than half off of the screen move it up and to the left
- /// so half the height and half the width are visible, and If the saved window dimensions are larger than the current screen shrink the
- /// window to fit.
- /// </summary>
- public static void SizeToFit(WindowPositionData data)
- {
- if (data.WindowHeight > System.Windows.SystemParameters.VirtualScreenHeight)
- {
- data.WindowHeight = System.Windows.SystemParameters.VirtualScreenHeight;
- }
- if (data.WindowWidth > System.Windows.SystemParameters.VirtualScreenWidth)
- {
- data.WindowWidth = System.Windows.SystemParameters.VirtualScreenWidth;
- }
- if (data.WindowTop + data.WindowHeight / 2 > System.Windows.SystemParameters.VirtualScreenHeight)
- {
- data.WindowTop = System.Windows.SystemParameters.VirtualScreenHeight - data.WindowHeight;
- }
- if (data.WindowLeft + data.WindowWidth / 2 > System.Windows.SystemParameters.VirtualScreenWidth)
- {
- data.WindowLeft = System.Windows.SystemParameters.VirtualScreenWidth - data.WindowWidth;
- }
- if (data.WindowTop < 0)
- {
- data.WindowTop = 0;
- }
- if (data.WindowLeft < 0)
- {
- data.WindowLeft = 0;
- }
- }
- #endregion
- }
- }
- namespace System.Xml
- {
- public class XmlSerialization
- {
- public static string Serialize<T>(T xmlObject)
- {
- return Serialize<T>(xmlObject, false);
- }
- public static string Serialize<T>(T xmlObject, bool encodeInBase64)
- {
- using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
- {
- System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
- System.Xml.Serialization.XmlSerializerNamespaces xmlNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
- xmlNamespace.Add(string.Empty, string.Empty);
- xs.Serialize(memoryStream, xmlObject, xmlNamespace);
- System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
- if (encodeInBase64)
- return Convert.ToBase64String(System.Text.ASCIIEncoding.Unicode.GetBytes(encoding.GetString(memoryStream.ToArray())));
- else
- return encoding.GetString(memoryStream.ToArray());
- }
- }
- public static T Deserialize<T>(string xml)
- {
- return Deserialize<T>(xml, false);
- }
- public static T Deserialize<T>(string xml, bool decodeInBase64)
- {
- string data = xml;
- if (decodeInBase64)
- data = System.Text.ASCIIEncoding.Unicode.GetString(Convert.FromBase64String(xml));
- System.Xml.Serialization.XmlSerializer xs = System.Xml.Serialization.XmlSerializer.FromTypes(new Type[] { typeof(T) })[0];
- System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
- using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encoding.GetBytes(data)))
- {
- System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
- return (T)xs.Deserialize(memoryStream);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment