SIL.FieldWorks.Common.Controls.Persistence.LoadWindowPosition C# (CSharp) Method

LoadWindowPosition() public method

Load the top, left, width, and height of the window from the registry, use default application parameters if not present in registry. This should be called from OnLayout; it takes effect in the first call AFTER EndInit.
public LoadWindowPosition ( ) : void
return void
		public void LoadWindowPosition()
		{
			if (m_fInInit)
				return;
			if (m_fHaveLoadedPosition)
				return;
			m_fHaveLoadedPosition = true;
			CheckDisposed();

			RegistryKey key = SettingsKey;

			int iLeft = (int)key.GetValue(Parent.GetType().Name + "Left", (Parent is Form ?
				((Form)Parent).DesktopBounds.Left : Parent.Left));

			int iTop = (int)key.GetValue(Parent.GetType().Name + "Top", (Parent is Form ?
				((Form)Parent).DesktopBounds.Top : Parent.Top));

			int iWidth = (int)key.GetValue(Parent.GetType().Name + "Width", (Parent is Form ?
				((Form)Parent).DesktopBounds.Width : Parent.Width));

			int iHeight = (int)key.GetValue(Parent.GetType().Name + "Height", (Parent is Form ?
				((Form)Parent).DesktopBounds.Height : Parent.Height));

			Rectangle rect = new Rectangle(iLeft, iTop, iWidth, iHeight);

			if (Parent is Form)
			{
				Form parent = Parent as Form;
				ScreenUtils.EnsureVisibleRect(ref rect);
				if (rect != parent.DesktopBounds)
				{
					// this means we loaded values from the registry - or the form is to big
					parent.StartPosition = FormStartPosition.Manual;
				}
				parent.DesktopLocation = new Point(rect.X, rect.Y);

				// we can't set the width and height on the form yet - if we do it won't
				// resize our child controls
				m_normalLeft = rect.X;
				m_normalTop = rect.Y;
				parent.Width = m_normalWidth = rect.Width;
				parent.Height = m_normalHeight = rect.Height;
				parent.WindowState = (FormWindowState)SettingsKey.GetValue(
					parent.GetType().Name + sWindowState, parent.WindowState);
			}
			else
			{
				// Set parent dimensions based upon possible adjustments in EnsureVisibleRect
				Parent.Top = rect.Top;
				Parent.Left = rect.Left;
				Parent.Width = rect.Width;
				Parent.Height = rect.Height;
			}
		}

Usage Example

 /// <summary>
 /// Override to restore position.
 /// </summary>
 protected override void OnLayout(LayoutEventArgs levent)
 {
     base.OnLayout(levent);
     m_persistence.LoadWindowPosition();
 }