/// <summary> /// Load Event handler to receive event notifications raised by the component's container. /// </summary> /// <param name="sender">The Form object that is the container of this component.</param> /// <param name="e">Event specific data.</param> private void ContainerFormLoad(object sender, EventArgs e) { if (_containerForm != null && String.IsNullOrEmpty(_applicationName) == false && String.IsNullOrEmpty(_formName) == false) { try { PersistWindowSection ConfigSection = PersistWindowSection.GetSection(); if (ConfigSection != null) { PersistWindowFormElement PersistedLayout = ConfigSection.Forms[this.FormName]; if (PersistedLayout != null) { UpdateFormPositionAndSize(PersistedLayout.X, PersistedLayout.Y, PersistedLayout.Width, PersistedLayout.Height, PersistedLayout.State); } } else if (String.IsNullOrEmpty(this.ApplicationName) == false && String.IsNullOrEmpty(this.FormName) == false) { string KeyName = Path.Combine(this.ApplicationName, Path.Combine(RegistrySubKeyName, this.FormName)); using (RegistryKey WekRegistry = Registry.CurrentUser.CreateSubKey(KeyName)) { if (WekRegistry != null) { UpdateFormPositionAndSize(Convert.ToInt32(WekRegistry.GetValue(PersistWindow.SettingItemWindowX, -1)), Convert.ToInt32(WekRegistry.GetValue(PersistWindow.SettingItemWindowY, -1)), Convert.ToInt32(WekRegistry.GetValue(PersistWindow.SettingItemWindowWidth, -1)), Convert.ToInt32(WekRegistry.GetValue(PersistWindow.SettingItemWindowHeight, -1)), ParseFormWindowStateString(WekRegistry.GetValue(PersistWindow.SettingItemWindowHeight, "Normal"))); } } } } catch { #if DEBUG throw; #endif } } }
/// <summary> /// Closing Event handler to receive event notifications raised by the component's container. /// </summary> /// <param name="sender">The Form object that is the container of this component.</param> /// <param name="e">Event specific data.</param> private void ContainerFormClosing(object sender, FormClosingEventArgs e) { try { Rectangle Bounds = (_containerForm.WindowState == FormWindowState.Normal) ? _containerForm.Bounds : _containerForm.RestoreBounds; // Attempt to persist the current form layout to a roaming configuration file. FxConfig.Configuration ConfigFile = FxConfig.ConfigurationManager.OpenExeConfiguration(FxConfig.ConfigurationUserLevel.PerUserRoamingAndLocal); if (ConfigFile != null) { PersistWindowSection Section = ConfigFile.GetSection("persistWindow") as PersistWindowSection; PersistWindowFormElement LayoutStorage = Section.Forms[this.FormName]; if (LayoutStorage != null) { LayoutStorage.X = Bounds.X; LayoutStorage.Y = Bounds.Y; LayoutStorage.Width = Bounds.Width; LayoutStorage.Height = Bounds.Height; LayoutStorage.State = _containerForm.WindowState; } else { PersistWindowFormElement FormElement = new PersistWindowFormElement(); FormElement.Name = this.FormName; FormElement.X = Bounds.X; FormElement.Y = Bounds.Y; FormElement.Width = Bounds.Width; FormElement.Height = Bounds.Height; FormElement.State = _containerForm.WindowState; FormElement.LockItem = false; Section.Forms.Add(FormElement); } ConfigFile.Save(FxConfig.ConfigurationSaveMode.Modified); } else if (String.IsNullOrEmpty(this.ApplicationName) == false && String.IsNullOrEmpty(this.FormName) == false) { // Attempt to persist to the registry if the configuration file cannot be updated. string KeyName = Path.Combine(this.ApplicationName, Path.Combine(RegistrySubKeyName, this.FormName)); using (RegistryKey WekRegistry = Registry.CurrentUser.CreateSubKey(KeyName)) { if (WekRegistry != null) { WekRegistry.SetValue(PersistWindow.SettingItemWindowX, Bounds.X, RegistryValueKind.DWord); WekRegistry.SetValue(PersistWindow.SettingItemWindowY, Bounds.Y, RegistryValueKind.DWord); WekRegistry.SetValue(PersistWindow.SettingItemWindowWidth, Bounds.Width, RegistryValueKind.DWord); WekRegistry.SetValue(PersistWindow.SettingItemWindowHeight, Bounds.Height, RegistryValueKind.DWord); WekRegistry.SetValue(PersistWindow.SettingItemWindowState, _containerForm.WindowState, RegistryValueKind.String); } } } } catch { #if DEBUG throw; #endif } }