PersistWindowState.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #region Using directives
  2. using System;
  3. using System.Windows.Forms;
  4. using Microsoft.Win32;
  5. using System.Drawing;
  6. #endregion
  7. namespace DocToolkit
  8. {
  9. /// <summary>
  10. /// Class allows to keep last window state in Registry
  11. /// and restore it when form is loaded.
  12. ///
  13. /// Source: Saving and Restoring the Location, Size and
  14. /// Windows State of a .NET Form
  15. /// By Joel Matthias
  16. ///
  17. /// Downloaded from http://www.codeproject.com
  18. ///
  19. /// Using:
  20. /// 1. Add class member to the owner form:
  21. ///
  22. /// private PersistWindowState persistState;
  23. ///
  24. /// 2. Create it in the form constructor:
  25. ///
  26. /// persistState = new PersistWindowState("Software\\MyCompany\\MyProgram", this);
  27. ///
  28. /// </summary>
  29. public class PersistWindowState
  30. {
  31. #region Members
  32. private Form ownerForm; // reference to owner form
  33. private string registryPath; // path in Registry where state information is kept
  34. // Form state parameters:
  35. private int normalLeft;
  36. private int normalTop;
  37. private int normalWidth;
  38. private int normalHeight;
  39. // FormWindowState is enumeration from System.Windows.Forms Namespace
  40. // Contains 3 members: Maximized, Minimized and Normal.
  41. private FormWindowState windowState = FormWindowState.Normal;
  42. // if allowSaveMinimized is true, form closed in minimal state
  43. // is loaded next time in minimal state.
  44. private bool allowSaveMinimized = false;
  45. #endregion
  46. #region Constructor
  47. /// <summary>
  48. /// Initialization
  49. /// </summary>
  50. /// <param name="sRegPath"></param>
  51. /// <param name="owner"></param>
  52. public PersistWindowState(string path, Form owner)
  53. {
  54. if (path == null ||
  55. path.Length == 0)
  56. {
  57. registryPath = "Software\\Unknown";
  58. }
  59. else
  60. {
  61. registryPath = path;
  62. }
  63. if (!registryPath.EndsWith("\\"))
  64. registryPath += "\\";
  65. registryPath += "MainForm";
  66. ownerForm = owner;
  67. // subscribe to parent form's events
  68. ownerForm.Closing += OnClosing;
  69. ownerForm.Resize += OnResize;
  70. ownerForm.Move += OnMove;
  71. ownerForm.Load += OnLoad;
  72. // get initial width and height in case form is never resized
  73. normalWidth = ownerForm.Width;
  74. normalHeight = ownerForm.Height;
  75. }
  76. #endregion
  77. #region Properties
  78. /// <summary>
  79. /// AllowSaveMinimized property (default value false)
  80. /// </summary>
  81. public bool AllowSaveMinimized
  82. {
  83. get
  84. {
  85. return allowSaveMinimized;
  86. }
  87. set
  88. {
  89. allowSaveMinimized = value;
  90. }
  91. }
  92. #endregion
  93. #region Event Handlers
  94. /// <summary>
  95. /// Parent form is resized.
  96. /// Keep current size.
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. private void OnResize(object sender, System.EventArgs e)
  101. {
  102. // save width and height
  103. if (ownerForm.WindowState == FormWindowState.Normal)
  104. {
  105. normalWidth = ownerForm.Width;
  106. normalHeight = ownerForm.Height;
  107. }
  108. }
  109. /// <summary>
  110. /// Parent form is moved.
  111. /// Keep current window position.
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void OnMove(object sender, System.EventArgs e)
  116. {
  117. // save position
  118. if (ownerForm.WindowState == FormWindowState.Normal)
  119. {
  120. normalLeft = ownerForm.Left;
  121. normalTop = ownerForm.Top;
  122. }
  123. // save state
  124. windowState = ownerForm.WindowState;
  125. }
  126. /// <summary>
  127. /// Parent form is closing.
  128. /// Keep last state in Registry.
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
  133. {
  134. // save position, size and state
  135. RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath);
  136. key.SetValue("Left", normalLeft);
  137. key.SetValue("Top", normalTop);
  138. key.SetValue("Width", normalWidth);
  139. key.SetValue("Height", normalHeight);
  140. // check if we are allowed to save the state as minimized (not normally)
  141. if (!allowSaveMinimized)
  142. {
  143. if (windowState == FormWindowState.Minimized)
  144. windowState = FormWindowState.Normal;
  145. }
  146. key.SetValue("WindowState", (int)windowState);
  147. }
  148. /// <summary>
  149. /// Parent form is loaded.
  150. /// Read last state from Registry and set it to form.
  151. /// </summary>
  152. /// <param name="sender"></param>
  153. /// <param name="e"></param>
  154. private void OnLoad(object sender, System.EventArgs e)
  155. {
  156. // attempt to read state from registry
  157. RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath);
  158. if (key != null)
  159. {
  160. int left = (int)key.GetValue("Left", ownerForm.Left);
  161. int top = (int)key.GetValue("Top", ownerForm.Top);
  162. int width = (int)key.GetValue("Width", ownerForm.Width);
  163. int height = (int)key.GetValue("Height", ownerForm.Height);
  164. FormWindowState windowState = (FormWindowState)key.GetValue("WindowState", (int)ownerForm.WindowState);
  165. ownerForm.Location = new Point(left, top);
  166. ownerForm.Size = new Size(width, height);
  167. ownerForm.WindowState = windowState;
  168. }
  169. }
  170. #endregion
  171. }
  172. }