AdSense

Wednesday 17 July 2013

C# Windows Forms resp. WPF - Save window position and size

(Deutsche Version) It can be very useful when all the windows are at the same position and have same size when restarting an application. Therefor, I wrote an own class in C#: SizeSavedWindow. The source code for the class is below. To save the size and position of a window, you have to add  

SizeSavedWindow.addToSizeSavedWindows(this);

in the InitializeComponent(); method.

This function can be called for many different windows. The windows are saved by name into an xml file, if there are no windows which have the same name, everything works fine. This class also exists for WPF, you simply have to change every "Form" to "Window" and the events have to be modified (window_IsVisibleChanged instead of window_Shown). If anyone is interested in the WPF code i can post this code, too. Now the code for the class SizeSavedWindow:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Runtime.Serialization;
using System.Xml;
using System.Windows.Forms;

namespace KeepSizeTest1
{
    public class SizeSavedWindow
    {
        // To keep a window at the same size and position, just add
        // SizeSavedWindow.addToSizeSavedWindows(this);
        // right after initialieComponent
        public static void addToSizeSavedWindows(Form window)
        {
            window.Shown += window_Shown;
        }

        static void window_Shown(object sender, EventArgs e)
        {
            Form window = (Form)sender;
            if (!window.Visible)
            {
                return;
            }
            if (File.Exists("sizes.xml"))
            {
                var stream = new FileStream("sizes.xml", FileMode.Open);
                var reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
                var deserializer = new DataContractSerializer(typeof(Dictionary<string, int[]>));
                windows = (Dictionary<string, int[]>)deserializer.ReadObject(reader, true);
                stream.Close();

                foreach (KeyValuePair<string, int[]> pair in windows)
                {
                    if (pair.Key == window.Name)
                    {
                        window.Height = pair.Value[0];
                        window.Width = pair.Value[1];
                        window.Top = pair.Value[2];
                        window.Left = pair.Value[3];
                        break;
                    }
                }
            }
            int[] sizes = new int[4];
            sizes[0] = window.Height;
            sizes[1] = window.Width;
            sizes[2] = window.Top;
            sizes[3] = window.Left;
            if (windows.ContainsKey(window.Name))
            {
                windows.Remove(window.Name);
            }
            windows.Add(window.Name, sizes);
            window.SizeChanged += window_SizeChanged;
            window.LocationChanged += window_LocationChanged;
        }

        static void window_SizeChanged(object sender, EventArgs e)
        {
            Form realSender = (Form)sender;
            if (windows.ContainsKey(realSender.Name))
            {
                windows[realSender.Name][0] = realSender.Height;
                windows[realSender.Name][1] = realSender.Width;

                var writer = new FileStream("sizes.xml", FileMode.Create);
                Type type = windows.GetType();
                var serializer = new DataContractSerializer(type);
                serializer.WriteObject(writer, windows);
                writer.Close();
            }
        }

        static void window_LocationChanged(object sender, EventArgs e)
        {
            Form realSender = (Form)sender;
            if (windows.ContainsKey(realSender.Name))
            {
                windows[realSender.Name][2] = realSender.Top;
                windows[realSender.Name][3] = realSender.Left;

                var writer = new FileStream("sizes.xml", FileMode.Create);
                Type type = windows.GetType();
                var serializer = new DataContractSerializer(type);
                serializer.WriteObject(writer, windows);
                writer.Close();
            }
        }

        static Dictionary<string, int[]> windows = new Dictionary<string, int[]>();
    }
}

No comments:

Post a Comment