andrew4582

WindowPositionData

Nov 9th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.00 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4. using System.Xml;
  5.  
  6. namespace System.Windows
  7. {
  8.     /*
  9.     public MainWindow()
  10.     {
  11.         InitializeComponent();
  12.         WindowPositionData.LoadWindowPosision(this);
  13.         this.Closing += (s, e) => WindowPositionData.SaveWindowPosision(this);
  14.     }
  15.      */
  16.     /// <summary>
  17.     /// Class used to persist window's position
  18.     /// </summary>
  19.     [XmlRoot("WindowPositionData")]
  20.     [Serializable]
  21.     public class WindowPositionData
  22.     {
  23.         public double WindowTop { get; set; }
  24.  
  25.         public double WindowLeft { get; set; }
  26.  
  27.         public double WindowHeight { get; set; }
  28.  
  29.         public double WindowWidth { get; set; }
  30.  
  31.         public System.Windows.WindowState WindowState { get; set; }
  32.  
  33.         public bool AllValuesZero()
  34.         {
  35.             return WindowTop <= 0 && WindowLeft <= 0 && WindowHeight <= 0 && WindowWidth <= 0;
  36.         }
  37.  
  38.         #region static methods
  39.  
  40.         public static void LoadWindowPosision(Window win)
  41.         {
  42.             WindowPositionData data = GetDataFromFile(win);
  43.             SetWindowPosition(win, data);
  44.         }
  45.  
  46.         public static void SaveWindowPosision(Window win)
  47.         {
  48.             WindowPositionData data = new WindowPositionData();
  49.             GetWindowPosition(win, data);
  50.             SaveDataToFile(win, data);
  51.         }
  52.  
  53.         public static void GetWindowPosition(Window win, WindowPositionData data)
  54.         {
  55.             data.WindowWidth = win.Width;
  56.             data.WindowHeight = win.Height;
  57.             data.WindowTop = win.Top;
  58.             data.WindowLeft = win.Left;
  59.             data.WindowState = win.WindowState;
  60.         }
  61.  
  62.         public static void SetWindowPosition(Window win, WindowPositionData data)
  63.         {
  64.             if (data.AllValuesZero())
  65.             {
  66.                 win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  67.             }
  68.             else
  69.             {
  70.                 win.WindowStartupLocation = WindowStartupLocation.Manual;
  71.             }
  72.  
  73.             if (data.WindowWidth > 0)
  74.                 win.Width = data.WindowWidth;
  75.             if (data.WindowHeight > 0)
  76.                 win.Height = data.WindowHeight;
  77.             if (data.WindowTop > 0)
  78.                 win.Top = data.WindowTop;
  79.             else
  80.             {
  81.                 if (double.IsNaN(win.Top))
  82.                     win.Top = 0;
  83.             }
  84.             if (data.WindowLeft > 0)
  85.                 win.Left = data.WindowLeft;
  86.             else
  87.             {
  88.                 if (double.IsNaN(win.Left))
  89.                     win.Left = 0;
  90.             }
  91.  
  92.             SizeToFit(win);
  93.  
  94.             if (data.WindowState != WindowState.Minimized)
  95.             {
  96.                 win.WindowState = data.WindowState;
  97.             }
  98.         }
  99.  
  100.         static WindowPositionData GetDataFromFile(Window win)
  101.         {
  102.             string datafilePath = GetDataFilePath(win);
  103.             try
  104.             {
  105.                 if (File.Exists(datafilePath))
  106.                 {
  107.                     return WindowPositionData.DeserializeFromXml(File.ReadAllText(datafilePath));
  108.                 }
  109.                 else
  110.                     return new WindowPositionData();
  111.             }
  112.             catch
  113.             {
  114.                 return new WindowPositionData();
  115.             }
  116.         }
  117.  
  118.         static void SaveDataToFile(Window win, WindowPositionData data)
  119.         {
  120.             string datafilePath = GetDataFilePath(win);
  121.             string xml = WindowPositionData.SerializeToXml(data);
  122.             File.WriteAllText(datafilePath, xml);
  123.         }
  124.  
  125.         static string GetDataFilePath(Window win)
  126.         {
  127.             const string DATA_FILE_NAME = "WindowPosisionData.xml";
  128.             const string DATA_FILE_FORMAT = "WindowPosisionData_{0}.xml";
  129.  
  130.             string filename = DATA_FILE_NAME;
  131.             if (win != null)
  132.                 filename = string.Format(DATA_FILE_FORMAT, win.GetType().Name);
  133.  
  134.             string dirpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  135.             dirpath = Path.Combine(dirpath, "Company", "App");
  136.             if (!Directory.Exists(dirpath))
  137.                 Directory.CreateDirectory(dirpath);
  138.  
  139.             return Path.Combine(dirpath, filename);
  140.         }
  141.  
  142.         static string SerializeToXml(WindowPositionData data)
  143.         {
  144.             if (data == null)
  145.                 throw new ArgumentNullException("data");
  146.  
  147.             return XmlSerialization.Serialize<WindowPositionData>(data);
  148.         }
  149.  
  150.         static WindowPositionData DeserializeFromXml(string xml)
  151.         {
  152.             if (string.IsNullOrEmpty(xml))
  153.                 throw new ArgumentNullException("xml");
  154.  
  155.             return XmlSerialization.Deserialize<WindowPositionData>(xml);
  156.         }
  157.  
  158.         static void GetWindowPosition(Window win)
  159.         {
  160.             WindowPositionData data = new WindowPositionData();
  161.             GetWindowPosition(win, data);
  162.         }
  163.  
  164.         /// <summary>
  165.         /// If the window is more than half off of the screen move it up and to the left
  166.         /// so half the height and half the width are visible, and If the saved window dimensions are larger than the current screen shrink the
  167.         /// window to fit.
  168.         /// </summary>
  169.         public static void SizeToFit(Window win)
  170.         {
  171.             if (win.Height > System.Windows.SystemParameters.VirtualScreenHeight)
  172.             {
  173.                 win.Height = System.Windows.SystemParameters.VirtualScreenHeight;
  174.             }
  175.  
  176.             if (win.Width > System.Windows.SystemParameters.VirtualScreenWidth)
  177.             {
  178.                 win.Width = System.Windows.SystemParameters.VirtualScreenWidth;
  179.             }
  180.  
  181.             if (win.Top + win.Height / 2 > System.Windows.SystemParameters.VirtualScreenHeight)
  182.             {
  183.                 win.Top = System.Windows.SystemParameters.VirtualScreenHeight - win.Height;
  184.             }
  185.  
  186.             if (win.Left + win.Width / 2 > System.Windows.SystemParameters.VirtualScreenWidth)
  187.             {
  188.                 win.Left = System.Windows.SystemParameters.VirtualScreenWidth - win.Width;
  189.             }
  190.  
  191.             if (win.Top < 0)
  192.             {
  193.                 win.Top = 0;
  194.             }
  195.  
  196.             if (win.Left < 0)
  197.             {
  198.                 win.Left = 0;
  199.             }
  200.         }
  201.  
  202.         /// <summary>
  203.         /// If the window is more than half off of the screen move it up and to the left
  204.         /// so half the height and half the width are visible, and If the saved window dimensions are larger than the current screen shrink the
  205.         /// window to fit.
  206.         /// </summary>
  207.         public static void SizeToFit(WindowPositionData data)
  208.         {
  209.             if (data.WindowHeight > System.Windows.SystemParameters.VirtualScreenHeight)
  210.             {
  211.                 data.WindowHeight = System.Windows.SystemParameters.VirtualScreenHeight;
  212.             }
  213.  
  214.             if (data.WindowWidth > System.Windows.SystemParameters.VirtualScreenWidth)
  215.             {
  216.                 data.WindowWidth = System.Windows.SystemParameters.VirtualScreenWidth;
  217.             }
  218.  
  219.             if (data.WindowTop + data.WindowHeight / 2 > System.Windows.SystemParameters.VirtualScreenHeight)
  220.             {
  221.                 data.WindowTop = System.Windows.SystemParameters.VirtualScreenHeight - data.WindowHeight;
  222.             }
  223.  
  224.             if (data.WindowLeft + data.WindowWidth / 2 > System.Windows.SystemParameters.VirtualScreenWidth)
  225.             {
  226.                 data.WindowLeft = System.Windows.SystemParameters.VirtualScreenWidth - data.WindowWidth;
  227.             }
  228.  
  229.             if (data.WindowTop < 0)
  230.             {
  231.                 data.WindowTop = 0;
  232.             }
  233.  
  234.             if (data.WindowLeft < 0)
  235.             {
  236.                 data.WindowLeft = 0;
  237.             }
  238.         }
  239.  
  240.         #endregion
  241.     }
  242. }
  243.  
  244. namespace System.Xml
  245. {
  246.     public class XmlSerialization
  247.     {
  248.         public static string Serialize<T>(T xmlObject)
  249.         {
  250.             return Serialize<T>(xmlObject, false);
  251.         }
  252.         public static string Serialize<T>(T xmlObject, bool encodeInBase64)
  253.         {
  254.             using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  255.             {
  256.                 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
  257.                 System.Xml.Serialization.XmlSerializerNamespaces xmlNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
  258.                 xmlNamespace.Add(string.Empty, string.Empty);
  259.                 xs.Serialize(memoryStream, xmlObject, xmlNamespace);
  260.                 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  261.                 if (encodeInBase64)
  262.                     return Convert.ToBase64String(System.Text.ASCIIEncoding.Unicode.GetBytes(encoding.GetString(memoryStream.ToArray())));
  263.                 else
  264.                     return encoding.GetString(memoryStream.ToArray());
  265.             }
  266.         }
  267.         public static T Deserialize<T>(string xml)
  268.         {
  269.             return Deserialize<T>(xml, false);
  270.         }
  271.         public static T Deserialize<T>(string xml, bool decodeInBase64)
  272.         {
  273.             string data = xml;
  274.             if (decodeInBase64)
  275.                 data = System.Text.ASCIIEncoding.Unicode.GetString(Convert.FromBase64String(xml));
  276.             System.Xml.Serialization.XmlSerializer xs = System.Xml.Serialization.XmlSerializer.FromTypes(new Type[] { typeof(T) })[0];
  277.             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  278.             using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encoding.GetBytes(data)))
  279.             {
  280.                 System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
  281.                 return (T)xs.Deserialize(memoryStream);
  282.             }
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment