Save and Restore Form Position and Size

前端 未结 8 2148
说谎
说谎 2020-12-14 04:36

In a WinForms 2.0 C# application, what is the typical method used for saving and restoring form position and size in an application?

Related, is it possible to add n

相关标签:
8条回答
  • 2020-12-14 05:18

    Here are some relevant links to check out:

    Saving out a Form's Size and Location using the Application Settings feature

    Any good examples of how to use Applications settings

    Exploring Secrets of Persistent Application Settings

    0 讨论(0)
  • 2020-12-14 05:24

    You could create a base form class with common functionality such as remembering the position and size and inherit from that base class.

    public class myForm : Form {
    protected override void OnLoad(){
        //load the settings and apply them
        base.OnLoad();
    }
    
    protected override void OnClose(){
        //save the settings
        base.OnClose();
    }
    }
    then for the other forms:
    
    public class frmMainScreen : myForm {
    // you get the settings for free ;)
    }

    Well, something like that ;)

    0 讨论(0)
  • 2020-12-14 05:25

    I just stream it out to a separate XML file - quick and dirty and probably not what youre after:

    Dim winRect As String() = util.ConfigFile.GetUserConfigInstance().GetValue("appWindow.rect").Split(",")
    Dim winState As String = util.ConfigFile.GetUserConfigInstance().GetValue("appWindow.state")
    
    Me.WindowState = FormWindowState.Normal
    
    Me.Left = CType(winRect(0), Integer)
    Me.Top = CType(winRect(1), Integer)
    Me.Width = CType(winRect(2), Integer)
    Me.Height = CType(winRect(3), Integer)
    
    If winState = "maximised" Then
        Me.WindowState = FormWindowState.Maximized
    End If
    

    and

    Dim winState As String = "normal"
    If Me.WindowState = FormWindowState.Maximized Then
        winState = "maximised"
    ElseIf Me.WindowState = FormWindowState.Minimized Then
        winState = "minimised"
    End If
    
    If Me.WindowState = FormWindowState.Normal Then
    
        Dim winRect As String = CType(Me.Left, String) & "," & CType(Me.Top, String) & "," & CType(Me.Width, String) & "," & CType(Me.Height, String)
        ' only save window rectangle if its not maximised/minimised
        util.ConfigFile.GetUserConfigInstance().SetValue("appWindow.rect", winRect)
    End If
    
    util.ConfigFile.GetUserConfigInstance().SetValue("appWindow.state", winState)
    
    0 讨论(0)
  • 2020-12-14 05:26

    Here's the code I used.

    private void SaveWindowPosition()
    {
        Rectangle rect = (WindowState == FormWindowState.Normal) ?
            new Rectangle(DesktopBounds.Left, DesktopBounds.Top, DesktopBounds.Width, DesktopBounds.Height) :
            new Rectangle(RestoreBounds.Left, RestoreBounds.Top, RestoreBounds.Width, RestoreBounds.Height);
        RegistrySettings.SetSetting("WindowPosition", String.Format("{0},{1},{2},{3},{4}",
            (int)this.WindowState,
            rect.Left, rect.Top, rect.Width, rect.Height));
    }
    
    private void RestoreWindowPosition()
    {
        try
        {
            string s = RegistrySettings.GetSetting("WindowPosition", String.Empty) as string;
            if (s != null)
            {
                List<int> settings = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                      .Select(v => int.Parse(v)).ToList();
                if (settings.Count == 5)
                {
                    this.SetBounds(
                        settings[1],
                        settings[2],
                        settings[3],
                        settings[4]);
                    this.WindowState = (FormWindowState)settings[0];
                }
            }
        }
        catch { /* Just leave current position if error */ }
    }
    

    I also presented this code in my article Saving and Restoring a Form's Window Position.

    0 讨论(0)
  • 2020-12-14 05:30

    I got this code from somewhere, but unfortunately at the time (long ago) didn't make a comment about where I got it from.

    This saves the form info to the user's HKCU registry:

    using System;
    using System.Windows.Forms;
    using Microsoft.Win32;
    
    /// <summary>Summary description for FormPlacement.</summary>
    public class PersistentForm : System.Windows.Forms.Form
    {
        private const string DIALOGKEY = "Dialogs";
    
        /// <summary></summary>
        protected override void OnCreateControl()
        {
            LoadSettings();
            base.OnCreateControl ();
        }
    
        /// <summary></summary>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            SaveSettings();
            base.OnClosing(e);
        }
    
        /// <summary>Saves the form's settings.</summary>
        public void SaveSettings()
        {
            RegistryKey dialogKey = Application.UserAppDataRegistry.CreateSubKey(DIALOGKEY);
            if (dialogKey != null)
            {
                RegistryKey formKey = dialogKey.CreateSubKey(this.GetType().ToString());
                if (formKey != null)
                {
                    formKey.SetValue("Left", this.Left);
                    formKey.SetValue("Top", this.Top);
                    formKey.Close();
                }
                dialogKey.Close();
            }
        }
    
        /// <summary></summary>
        public void LoadSettings()
        {
            RegistryKey dialogKey = Application.UserAppDataRegistry.OpenSubKey(DIALOGKEY);
            if (dialogKey != null)
            {
                RegistryKey formKey = dialogKey.OpenSubKey(this.GetType().ToString());
                if (formKey != null)
                {
                    this.Left = (int)formKey.GetValue("Left");
                    this.Top = (int)formKey.GetValue("Top");
                    formKey.Close();
                }
                dialogKey.Close();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 05:31

    I'm in the same boat as you, in that I have a number of forms (MDI children, in my case) that I want to preserve the position and size of for each user. From my research, creating application settings at runtime is not supported. (see this blog entry) However, you don't have to stick everything in the main settings file. You can add a Settings file to your project (explained here in the MSDN) and use it via the Properties.Settings object. This won't ease the pain of having to remember to create new settigns for each form, but at least it will keep them together, and not clutter up your main application settings.

    As far as using the base class to retrieve the settings... I don't know if you can do it there. What I would (and probably will) do is name each attribute , then use Me.GetType().ToString() (I'm working in VB) to composite the names of the attributes I want to retrieve in the Load() event of each form.

    0 讨论(0)
提交回复
热议问题