Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using Microsoft.Win32;
  8. namespace Wrox.ProCSharp.FilesAndRegistry
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.ListBox listBoxMessages;
  16. private System.Windows.Forms.Button buttonChooseColor;
  17. private ColorDialog chooseColorDialog = new ColorDialog();
  18. /// <summary>
  19. /// Required designer variable.
  20. /// </summary>
  21. private System.ComponentModel.Container components = null;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. buttonChooseColor.Click += new EventHandler(OnClickChooseColor);
  26. try
  27. {
  28. if (ReadSettings() == false)
  29. listBoxMessages.Items.Add("No information in registry");
  30. else
  31. listBoxMessages.Items.Add("Information read in from registry");
  32. StartPosition = FormStartPosition.Manual;
  33. }
  34. catch (Exception e)
  35. {
  36. listBoxMessages.Items.Add("A problem occurred reading in data from registry:");
  37.   listBoxMessages.Items.Add(e.Message);
  38. }
  39. }
  40. /// <summary>
  41. /// Clean up any resources being used.
  42. /// </summary>
  43. protected override void Dispose( bool disposing )
  44. {
  45. if( disposing )
  46. {
  47. if (components != null) 
  48. {
  49. components.Dispose();
  50. }
  51. }
  52. SaveSettings();
  53. base.Dispose( disposing );
  54. }
  55. void OnClickChooseColor(object Sender, EventArgs e)
  56. {
  57. if(chooseColorDialog.ShowDialog() == DialogResult.OK)
  58. BackColor = chooseColorDialog.Color;
  59. }
  60. void SaveSettings()
  61. {
  62. RegistryKey softwareKey = 
  63. Registry.LocalMachine.OpenSubKey("Software", true);
  64. RegistryKey wroxKey = softwareKey.CreateSubKey("WroxPress");
  65. RegistryKey selfPlacingWindowKey = 
  66. wroxKey.CreateSubKey("SelfPlacingWindow");
  67. selfPlacingWindowKey.SetValue("BackColor", 
  68. (object)BackColor.ToKnownColor());
  69. selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R);
  70. selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G);
  71. selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B);
  72. selfPlacingWindowKey.SetValue("Width", (object)Width);
  73. selfPlacingWindowKey.SetValue("Height", (object)Height);
  74. selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X);
  75. selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y);
  76. selfPlacingWindowKey.SetValue("WindowState", 
  77. (object)WindowState.ToString());
  78. }
  79. bool ReadSettings()
  80. RegistryKey softwareKey = 
  81. Registry.LocalMachine.OpenSubKey("Software");
  82. RegistryKey wroxKey = softwareKey.OpenSubKey("WroxPress");
  83. if (wroxKey == null)
  84. return false;
  85. RegistryKey selfPlacingWindowKey = 
  86. wroxKey.OpenSubKey("SelfPlacingWindow");
  87. if (selfPlacingWindowKey == null)
  88. return false;
  89. else
  90. listBoxMessages.Items.Add("Successfully opened key " + 
  91. selfPlacingWindowKey.ToString());
  92. int redComponent = (int)selfPlacingWindowKey.GetValue("Red");
  93. int greenComponent = (int)selfPlacingWindowKey.GetValue("Green");
  94. int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue");
  95. this.BackColor = Color.FromArgb(redComponent, greenComponent, 
  96. blueComponent);
  97. listBoxMessages.Items.Add("Background color: " + BackColor.Name);
  98. int X = (int)selfPlacingWindowKey.GetValue("X");
  99. int Y = (int)selfPlacingWindowKey.GetValue("Y");
  100. this.DesktopLocation = new Point(X, Y);
  101. listBoxMessages.Items.Add("Desktop location: " + 
  102. DesktopLocation.ToString());
  103. this.Height = (int)selfPlacingWindowKey.GetValue("Height");
  104. this.Width = (int)selfPlacingWindowKey.GetValue("Width");
  105. listBoxMessages.Items.Add("Size: " + new 
  106. Size(Width,Height).ToString());
  107. string initialWindowState = 
  108. (string)selfPlacingWindowKey.GetValue("WindowState");
  109. listBoxMessages.Items.Add("Window State: " + initialWindowState);
  110. this.WindowState = (FormWindowState)FormWindowState.Parse
  111. (WindowState.GetType(), initialWindowState);
  112. return true;
  113. }
  114. #region Windows Form Designer generated code
  115. /// <summary>
  116. /// Required method for Designer support - do not modify
  117. /// the contents of this method with the code editor.
  118. /// </summary>
  119. private void InitializeComponent()
  120. {
  121. this.listBoxMessages = new System.Windows.Forms.ListBox();
  122. this.buttonChooseColor = new System.Windows.Forms.Button();
  123. this.SuspendLayout();
  124. // 
  125. // listBoxMessages
  126. // 
  127. this.listBoxMessages.Name = "listBoxMessages";
  128. this.listBoxMessages.Size = new System.Drawing.Size(288, 199);
  129. this.listBoxMessages.TabIndex = 0;
  130. // 
  131. // buttonChooseColor
  132. // 
  133. this.buttonChooseColor.Location = new System.Drawing.Point(0, 208);
  134. this.buttonChooseColor.Name = "buttonChooseColor";
  135. this.buttonChooseColor.Size = new System.Drawing.Size(104, 23);
  136. this.buttonChooseColor.TabIndex = 1;
  137. this.buttonChooseColor.Text = "Choose Color";
  138. // 
  139. // Form1
  140. // 
  141. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  142. this.ClientSize = new System.Drawing.Size(292, 232);
  143. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  144.   this.buttonChooseColor,
  145.   this.listBoxMessages});
  146. this.Name = "Form1";
  147. this.Text = "SelfPlacingWindow";
  148. this.Load += new System.EventHandler(this.Form1_Load);
  149. this.ResumeLayout(false);
  150. }
  151. #endregion
  152. /// <summary>
  153. /// The main entry point for the application.
  154. /// </summary>
  155. [STAThread]
  156. static void Main() 
  157. {
  158. Application.Run(new Form1());
  159. }
  160. private void Form1_Load(object sender, System.EventArgs e)
  161. {
  162. }
  163. }
  164. }