Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for capslockicon.cs

(view source code of capslockicon.cs as plain text)

  1. using System.Linq;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	public class CapsLockIcon : System.Windows.Forms.Form
  7. 	{
  8. 		static string progver = "1.03";
  9. 		static string copyrightsyear = "2021";
  10.  
  11.  
  12. 		#region Global Variables
  13.  
  14. 		private System.Windows.Forms.NotifyIcon capslockicon;
  15. 		private System.Windows.Forms.ContextMenu contextmenu;
  16. 		private System.Windows.Forms.MenuItem menuitemexit;
  17. 		private System.Windows.Forms.MenuItem menuitemsettings;
  18. 		private System.Windows.Forms.Form formsettings;
  19. 		private System.Windows.Forms.TextBox textbox;
  20. 		private System.Windows.Forms.ComboBox dropdowncoloroff;
  21. 		private System.Windows.Forms.ComboBox dropdowncoloron;
  22. 		private System.Windows.Forms.CheckBox checkboxflashwhenoff;
  23. 		private System.Windows.Forms.CheckBox checkboxflashwhenon;
  24. 		private System.Windows.Forms.CheckBox checkboxhidewhenoff;
  25. 		private System.Collections.Generic.List<System.Drawing.Icon> iconsoff;
  26. 		private System.Collections.Generic.List<System.Drawing.Icon> iconson;
  27. 		private System.Windows.Forms.Timer timer;
  28. 		private System.Drawing.Brush black = System.Drawing.Brushes.Black;
  29. 		private bool capslock;
  30.  
  31. 		#endregion Global Variables
  32.  
  33.  
  34. 		/// <summary>
  35. 		/// The main entry point for the application.
  36. 		/// </summary>
  37. 		[System.STAThread]
  38. 		static void Main( )
  39. 		{
  40. 			System.Windows.Forms.Application.EnableVisualStyles( );
  41. 			System.Windows.Forms.Application.SetCompatibleTextRenderingDefault( false );
  42. 			System.Windows.Forms.Application.Run( new CapsLockIcon( ) );
  43. 		}
  44.  
  45.  
  46. 		public CapsLockIcon( )
  47. 		{
  48. 			ReadSettings( );
  49. 			capslockicon = new System.Windows.Forms.NotifyIcon( );
  50. 			// Determine which icon should be displayed
  51. 			iconsoff = new System.Collections.Generic.List<System.Drawing.Icon>( ) { CreateIcon( GlobalSettings.IndicatorText, black, GlobalSettings.IndicatorColorOFF ), CreateIcon( GlobalSettings.IndicatorText, GlobalSettings.IndicatorColorOFF, black ) };
  52. 			iconson = new System.Collections.Generic.List<System.Drawing.Icon>( ) { CreateIcon( GlobalSettings.IndicatorText, black, GlobalSettings.IndicatorColorON ), CreateIcon( GlobalSettings.IndicatorText, GlobalSettings.IndicatorColorON, black ) };
  53. 			capslock = System.Console.CapsLock;
  54. 			this.capslockicon.Icon = iconson[0];
  55. 			// Context Menu
  56. 			this.contextmenu = new System.Windows.Forms.ContextMenu( );
  57. 			// Context Menu: Settings
  58. 			this.menuitemsettings = new System.Windows.Forms.MenuItem( "&Settings", new System.EventHandler( MenuItemSettings_Click ) );
  59. 			this.contextmenu.MenuItems.Add( menuitemsettings );
  60. 			// Context Menu: Exit
  61. 			this.menuitemexit = new System.Windows.Forms.MenuItem( "E&xit", new System.EventHandler( MenuItemExit_Click ) );
  62. 			this.contextmenu.MenuItems.Add( menuitemexit );
  63. 			this.capslockicon.ContextMenu = this.contextmenu;
  64. 			this.capslockicon.Visible = true;
  65. 			// Timer for key monitoring interval
  66. 			this.timer = new System.Windows.Forms.Timer( );
  67. 			this.timer.Interval = 1000;
  68. 			this.timer.Tick += new System.EventHandler( Timer_Tick );
  69. 			this.timer.Start( );
  70. 			// Store version information in the registry
  71. 			if ( ReadRegValue( "Version", string.Empty ) != progver )
  72. 			{
  73. 				WriteRegValue( "Version", progver );
  74. 				WriteRegValue( "URL", "https://www.robvanderwoude.com/csharpexamples.php#CapsLockIcon" );
  75. 				WriteRegValue( "Requirement", RequiredNetVersion( ) );
  76. 			}
  77. 		}
  78.  
  79.  
  80. 		/// <summary>
  81. 		/// Code to dynamically generate icons by Joshua Flanagan on CodeProject.com
  82. 		/// https://www.codeproject.com/Articles/7122/Dynamically-Generating-Icons-safely
  83. 		/// </summary>
  84. 		public System.Drawing.Icon CreateIcon( string text, System.Drawing.Brush fgcolor, System.Drawing.Brush bgcolor )
  85. 		{
  86. 			System.Drawing.Icon icon = null;
  87. 			System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( 16, 16 );
  88. 			System.Drawing.Font font = new System.Drawing.Font( System.Drawing.FontFamily.GenericSansSerif, 8F, System.Drawing.FontStyle.Bold );
  89. 			using ( System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage( bitmap ) )
  90. 			{
  91. 				graphic.FillEllipse( bgcolor, 0, 0, 16, 16 );
  92. 				System.Drawing.SizeF textsize = graphic.MeasureString( text, font );
  93. 				System.Single x = System.Convert.ToSingle( System.Math.Floor( ( bitmap.Width - textsize.Width ) / 2 ) );
  94. 				System.Single y = System.Convert.ToSingle( System.Math.Ceiling( ( bitmap.Height - textsize.Height ) / 2 ) );
  95. 				graphic.DrawString( text, font, fgcolor, x, y, System.Drawing.StringFormat.GenericDefault );
  96. 				icon = System.Drawing.Icon.FromHandle( bitmap.GetHicon( ) );
  97. 			}
  98. 			return icon;
  99. 		}
  100.  
  101.  
  102. 		public System.Drawing.Size GetTextSize( string text )
  103. 		{
  104. 			return System.Windows.Forms.TextRenderer.MeasureText( text, formsettings.Font );
  105.  
  106. 		}
  107.  
  108.  
  109. 		public void OpenURL( string url )
  110. 		{
  111. 			System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo( url );
  112. 			System.Diagnostics.Process.Start( startinfo );
  113. 		}
  114.  
  115.  
  116. 		public void Quit( )
  117. 		{
  118. 			this.timer.Stop( );
  119. 			this.timer.Dispose( );
  120. 			this.capslockicon.Visible = false;
  121. 			this.capslockicon.Dispose( );
  122. 			System.Windows.Forms.Application.Exit( );
  123. 		}
  124.  
  125.  
  126. 		public void ReadSettings( )
  127. 		{
  128. 			GlobalSettings.FlashIfOFF = ReadRegValue( "FlashIfOFF", GlobalSettings.FlashIfOFF );
  129. 			GlobalSettings.FlashIfON = ReadRegValue( "FlashIfON", GlobalSettings.FlashIfON );
  130. 			GlobalSettings.HideIfOFF = ReadRegValue( "HideIfOFF", GlobalSettings.HideIfOFF );
  131. 			string currentcolor = GlobalSettings.Colors.Where( c => c.Value.Equals( GlobalSettings.IndicatorColorOFF ) ).First( ).Key; // requires System.Linq
  132. 			GlobalSettings.IndicatorColorOFF = GlobalSettings.Colors[ReadRegValue( "IndicatorColorOFF", currentcolor )];
  133. 			currentcolor = GlobalSettings.Colors.Where( c => c.Value.Equals( GlobalSettings.IndicatorColorON ) ).First( ).Key; // requires System.Linq
  134. 			GlobalSettings.IndicatorColorON = GlobalSettings.Colors[ReadRegValue( "IndicatorColorON", currentcolor )];
  135. 			GlobalSettings.IndicatorText = ReadRegValue( "IndicatorText", GlobalSettings.IndicatorText );
  136. 		}
  137.  
  138.  
  139. 		/// <summary>
  140. 		/// Code to get the required .NET Framework version by Fernando Gonzalez Sanchez on StackOverflow.com
  141. 		/// https://stackoverflow.com/a/18623516
  142. 		/// </summary>
  143. 		static string RequiredNetVersion( )
  144. 		{
  145. 			object[] list = System.Reflection.Assembly.GetExecutingAssembly( ).GetCustomAttributes( true );
  146. 			var attribute = list.OfType<System.Runtime.Versioning.TargetFrameworkAttribute>( ).First( ); // requires Linq
  147. 			string frameworkname = attribute.FrameworkName;
  148. 			string frameworkdisplayname = attribute.FrameworkDisplayName;
  149. 			return frameworkdisplayname;
  150. 		}
  151.  
  152.  
  153. 		public void SaveSettings( )
  154. 		{
  155. 			// Adjust global settings
  156. 			GlobalSettings.FlashIfOFF = checkboxflashwhenoff.Checked && !checkboxhidewhenoff.Checked;
  157. 			GlobalSettings.FlashIfON = checkboxflashwhenon.Checked;
  158. 			GlobalSettings.HideIfOFF = checkboxhidewhenoff.Checked;
  159. 			GlobalSettings.IndicatorColorOFF = GlobalSettings.Colors[dropdowncoloroff.Text];
  160. 			GlobalSettings.IndicatorColorON = GlobalSettings.Colors[dropdowncoloron.Text];
  161. 			if ( !string.IsNullOrWhiteSpace( textbox.Text ) )
  162. 			{
  163. 				GlobalSettings.IndicatorText = textbox.Text.Trim( );
  164. 			}
  165. 			// Update program status
  166. 			iconsoff = new System.Collections.Generic.List<System.Drawing.Icon>( ) { CreateIcon( GlobalSettings.IndicatorText, black, GlobalSettings.IndicatorColorOFF ), CreateIcon( GlobalSettings.IndicatorText, GlobalSettings.IndicatorColorOFF, black ) };
  167. 			iconson = new System.Collections.Generic.List<System.Drawing.Icon>( ) { CreateIcon( GlobalSettings.IndicatorText, black, GlobalSettings.IndicatorColorON ), CreateIcon( GlobalSettings.IndicatorText, GlobalSettings.IndicatorColorON, black ) };
  168. 			// Save settings to registry
  169. 			bool success = true;
  170. 			success = success && WriteRegValue( "FlashIfOFF", GlobalSettings.FlashIfOFF );
  171. 			success = success && WriteRegValue( "FlashIfON", GlobalSettings.FlashIfON );
  172. 			success = success && WriteRegValue( "HideIfOFF", GlobalSettings.HideIfOFF );
  173. 			success = success && WriteRegValue( "IndicatorColorOFF", GlobalSettings.Colors.Where( c => c.Value.Equals( GlobalSettings.IndicatorColorOFF ) ).First( ).Key );
  174. 			success = success && WriteRegValue( "IndicatorColorON", GlobalSettings.Colors.Where( c => c.Value.Equals( GlobalSettings.IndicatorColorON ) ).First( ).Key );
  175. 			success = success && WriteRegValue( "IndicatorText", GlobalSettings.IndicatorText );
  176. 			if ( success )
  177. 			{
  178. 				System.Windows.Forms.MessageBox.Show( "Settings were successfully stored in the registry", "Settings Saved", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
  179. 			}
  180. 			else
  181. 			{
  182. 				System.Windows.Forms.MessageBox.Show( "Unable to store the settings in the registry", "Error Saving Settings", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
  183. 			}
  184. 			// Close settings window
  185. 			formsettings.Close( );
  186. 		}
  187.  
  188.  
  189. 		public void Settings( )
  190. 		{
  191. 			ReadSettings( );
  192.  
  193. 			System.Int32 column1width = 0;
  194. 			System.Int32 column2width = 0;
  195. 			System.Int32 rowheight = 30;
  196.  
  197. 			formsettings = new System.Windows.Forms.Form( );
  198. 			formsettings.Text = string.Format( "CapsLockIcon {0} Settings \u00A9 {1} Rob van der Woude", progver, copyrightsyear );
  199. 			formsettings.Size = new System.Drawing.Size( 500, 425 );
  200. 			formsettings.Font = new System.Drawing.Font( System.Drawing.FontFamily.GenericSansSerif, 10F );
  201.  
  202. 			// Column 1 Row 1
  203. 			System.Windows.Forms.Label labeltext = new System.Windows.Forms.Label( );
  204. 			labeltext.Text = "Indicator text";
  205. 			column1width = System.Math.Max( column1width, GetTextSize( labeltext.Text ).Width );
  206. 			labeltext.Location = new System.Drawing.Point( 20, 20 );
  207. 			formsettings.Controls.Add( labeltext );
  208.  
  209. 			// Column 1 Row 2
  210. 			System.Windows.Forms.Label labelcoloroff = new System.Windows.Forms.Label( );
  211. 			labelcoloroff.Text = "Indicator color when OFF";
  212. 			labelcoloroff.AutoSize = true;
  213. 			column1width = System.Math.Max( column1width, GetTextSize( labelcoloroff.Text ).Width );
  214. 			labelcoloroff.Location = new System.Drawing.Point( labeltext.Location.X, labeltext.Location.Y + labeltext.Height + rowheight );
  215. 			formsettings.Controls.Add( labelcoloroff );
  216.  
  217. 			// Column 1 Row 3
  218. 			System.Windows.Forms.Label labelcoloron = new System.Windows.Forms.Label( );
  219. 			labelcoloron.Text = "Indicator color when ON";
  220. 			labelcoloron.AutoSize = true;
  221. 			column1width = System.Math.Max( column1width, GetTextSize( labelcoloron.Text ).Width );
  222. 			labelcoloron.Location = new System.Drawing.Point( labelcoloroff.Location.X, labelcoloroff.Location.Y + labelcoloroff.Height + rowheight );
  223. 			formsettings.Controls.Add( labelcoloron );
  224.  
  225. 			// Column 2 Row 1
  226. 			textbox = new System.Windows.Forms.TextBox( );
  227. 			textbox.Text = GlobalSettings.IndicatorText;
  228. 			textbox.MaxLength = 2;
  229. 			textbox.SelectionStart = textbox.Text.Length;
  230. 			textbox.SelectionLength = 0;
  231. 			column2width = System.Math.Max( column2width, textbox.Width );
  232. 			textbox.Location = new System.Drawing.Point( column1width + 30, labeltext.Location.Y );
  233. 			formsettings.Controls.Add( textbox );
  234.  
  235. 			// Column 2 Row 2
  236. 			dropdowncoloroff = new System.Windows.Forms.ComboBox( );
  237. 			ComboboxItem item;
  238. 			foreach ( string color in GlobalSettings.Colors.Keys )
  239. 			{
  240. 				item = new ComboboxItem( );
  241. 				item.Text = color;
  242. 				item.Value = GlobalSettings.Colors[color];
  243. 				dropdowncoloroff.Items.Add( item );
  244. 			}
  245. 			dropdowncoloroff.SelectedIndex = GlobalSettings.Colors.Values.ToList<System.Drawing.Brush>( ).IndexOf( GlobalSettings.IndicatorColorOFF ); // requires System.Linq
  246. 			column2width = System.Math.Max( column2width, dropdowncoloroff.Width );
  247. 			dropdowncoloroff.Enabled = !GlobalSettings.HideIfOFF;
  248. 			dropdowncoloroff.Location = new System.Drawing.Point( column1width + 30, labelcoloroff.Location.Y );
  249. 			formsettings.Controls.Add( dropdowncoloroff );
  250.  
  251. 			// Column 2 Row 3
  252. 			dropdowncoloron = new System.Windows.Forms.ComboBox( );
  253. 			foreach ( string color in GlobalSettings.Colors.Keys )
  254. 			{
  255. 				item = new ComboboxItem( );
  256. 				item.Text = color;
  257. 				item.Value = GlobalSettings.Colors[color];
  258. 				dropdowncoloron.Items.Add( item );
  259. 			}
  260. 			dropdowncoloron.SelectedIndex = GlobalSettings.Colors.Values.ToList<System.Drawing.Brush>( ).IndexOf( GlobalSettings.IndicatorColorON ); // requires System.Linq
  261. 			column2width = System.Math.Max( column2width, dropdowncoloron.Width );
  262. 			dropdowncoloron.Location = new System.Drawing.Point( column1width + 30, labelcoloron.Location.Y );
  263. 			formsettings.Controls.Add( dropdowncoloron );
  264.  
  265. 			// Column 3 Row 1
  266. 			checkboxhidewhenoff = new System.Windows.Forms.CheckBox( );
  267. 			checkboxhidewhenoff.Text = " Hide when OFF";
  268. 			checkboxhidewhenoff.AutoSize = true;
  269. 			checkboxhidewhenoff.Checked = GlobalSettings.HideIfOFF;
  270. 			checkboxhidewhenoff.Location = new System.Drawing.Point( column1width + column2width + 50, textbox.Location.Y );
  271. 			checkboxhidewhenoff.Click += Checkboxhidewhenoff_Click;
  272. 			checkboxhidewhenoff.KeyPress += Checkboxhidewhenoff_KeyPress;
  273. 			formsettings.Controls.Add( checkboxhidewhenoff );
  274.  
  275. 			// Column 3 Row 2
  276. 			checkboxflashwhenoff = new System.Windows.Forms.CheckBox( );
  277. 			checkboxflashwhenoff.Text = " Flash when OFF";
  278. 			checkboxflashwhenoff.AutoSize = true;
  279. 			checkboxflashwhenoff.Checked = GlobalSettings.FlashIfOFF && !GlobalSettings.HideIfOFF;
  280. 			checkboxflashwhenoff.Enabled = !GlobalSettings.HideIfOFF;
  281. 			checkboxflashwhenoff.Location = new System.Drawing.Point( column1width + column2width + 50, dropdowncoloroff.Location.Y );
  282. 			formsettings.Controls.Add( checkboxflashwhenoff );
  283.  
  284. 			// Column 3 Row 3
  285. 			checkboxflashwhenon = new System.Windows.Forms.CheckBox( );
  286. 			checkboxflashwhenon.Text = " Flash when ON";
  287. 			checkboxflashwhenon.AutoSize = true;
  288. 			checkboxflashwhenon.Checked = GlobalSettings.FlashIfON;
  289. 			checkboxflashwhenon.Location = new System.Drawing.Point( column1width + column2width + 50, dropdowncoloron.Location.Y );
  290. 			formsettings.Controls.Add( checkboxflashwhenon );
  291.  
  292. 			// Buttons
  293. 			System.Windows.Forms.Button buttonsave = new System.Windows.Forms.Button( );
  294. 			buttonsave.Text = "Save";
  295. 			buttonsave.Click += new System.EventHandler( ButtonSave_Click );
  296. 			buttonsave.Size = new System.Drawing.Size( 100, 32 );
  297. 			buttonsave.Location = new System.Drawing.Point( formsettings.ClientSize.Width / 2 - buttonsave.Width - 20, labelcoloron.Location.Y + 2 * rowheight );
  298. 			formsettings.Controls.Add( buttonsave );
  299.  
  300. 			System.Windows.Forms.Button buttoncancel = new System.Windows.Forms.Button( );
  301. 			buttoncancel.Text = "Cancel";
  302. 			buttoncancel.Click += new System.EventHandler( ButtonCancel_Click );
  303. 			buttoncancel.Size = new System.Drawing.Size( 100, 32 );
  304. 			buttoncancel.Location = new System.Drawing.Point( formsettings.ClientSize.Width / 2 + 20, buttonsave.Location.Y );
  305. 			formsettings.Controls.Add( buttoncancel );
  306.  
  307. 			// URLs
  308. 			System.Windows.Forms.Label labelurlrvdw = new System.Windows.Forms.Label( );
  309. 			labelurlrvdw.Text = "Written by Rob van der Woude\nhttps://www.robvanderwoude.com";
  310. 			labelurlrvdw.Click += new System.EventHandler( LabelUrlRvdw_Click );
  311. 			labelurlrvdw.AutoSize = true;
  312. 			labelurlrvdw.Location = new System.Drawing.Point( 20, buttonsave.Location.Y + 2 * rowheight );
  313. 			formsettings.Controls.Add( labelurlrvdw );
  314.  
  315. 			System.Windows.Forms.Label labelurljficos = new System.Windows.Forms.Label( );
  316. 			labelurljficos.Text = "Code to dynamically generate icons by Joshua Flanagan on CodeProject.com\nhttps://www.codeproject.com/Articles/7122/Dynamically-Generating-Icons-safely";
  317. 			labelurljficos.Font = new System.Drawing.Font( labelurljficos.Font.FontFamily, labelurljficos.Font.Size * 0.9F, labelurljficos.Font.Style );
  318. 			labelurljficos.Click += new System.EventHandler( LabelUrlJfIcos_Click );
  319. 			labelurljficos.AutoSize = true;
  320. 			labelurljficos.Location = new System.Drawing.Point( 20, System.Convert.ToInt32( labelurlrvdw.Location.Y + 1.5 * rowheight ) );
  321. 			formsettings.Controls.Add( labelurljficos );
  322.  
  323. 			System.Windows.Forms.Label labelurlchw = new System.Windows.Forms.Label( );
  324. 			labelurlchw.Text = "Code to hide main form by Chriz on StackOverflow.com\nhttps://stackoverflow.com/a/11831856";
  325. 			labelurlchw.Font = new System.Drawing.Font( labelurlchw.Font.FontFamily, labelurlchw.Font.Size * 0.9F, labelurlchw.Font.Style );
  326. 			labelurlchw.Click += new System.EventHandler( LabelUrlChw_Click );
  327. 			labelurlchw.AutoSize = true;
  328. 			labelurlchw.Location = new System.Drawing.Point( 20, System.Convert.ToInt32( labelurljficos.Location.Y + 1.1 * rowheight ) );
  329. 			formsettings.Controls.Add( labelurlchw );
  330.  
  331. 			System.Windows.Forms.Label labelurlfgs = new System.Windows.Forms.Label( );
  332. 			labelurlfgs.Text = "Code to get the required .NET Framework version by Fernando Gonzalez Sanchez\nhttps://stackoverflow.com/a/18623516";
  333. 			labelurlfgs.Font = new System.Drawing.Font( labelurlfgs.Font.FontFamily, labelurlfgs.Font.Size * 0.9F, labelurlfgs.Font.Style );
  334. 			labelurlfgs.Click += new System.EventHandler( LabelUrlFgs_Click );
  335. 			labelurlfgs.AutoSize = true;
  336. 			labelurlfgs.Location = new System.Drawing.Point( 20, System.Convert.ToInt32( labelurlchw.Location.Y + 1.1 * rowheight ) );
  337. 			formsettings.Controls.Add( labelurlfgs );
  338.  
  339. 			formsettings.Show( );
  340. 		}
  341.  
  342.  
  343. 		public void UpdateSettingsUI( )
  344. 		{
  345. 			dropdowncoloroff.Enabled = !checkboxhidewhenoff.Checked;
  346. 			checkboxflashwhenoff.Enabled = !checkboxhidewhenoff.Checked;
  347. 			checkboxflashwhenoff.Checked = checkboxflashwhenoff.Checked && !checkboxhidewhenoff.Checked;
  348. 		}
  349.  
  350.  
  351. 		#region Registry
  352.  
  353. 		static bool ReadRegValue( string name, bool current )
  354. 		{
  355. 			bool value = false;
  356. 			Microsoft.Win32.RegistryKey regkey = null;
  357. 			try
  358. 			{
  359. 				regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "SOFTWARE\\RobvanderWoude\\CapsLockIcon", Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree );
  360. 				value = ( regkey.GetValue( name, ( current ? 1 : 0 ) ).ToString( ) == "1" );
  361. 				regkey.Close( );
  362. 				return value;
  363. 			}
  364. 			catch ( System.Exception )
  365. 			{
  366. 				if ( regkey != null )
  367. 				{
  368. 					regkey.Close( );
  369. 				}
  370. 				return false;
  371. 			}
  372. 		}
  373.  
  374.  
  375. 		static string ReadRegValue( string name, string current )
  376. 		{
  377. 			string value = current;
  378. 			Microsoft.Win32.RegistryKey regkey = null;
  379. 			try
  380. 			{
  381. 				regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "SOFTWARE\\RobvanderWoude\\CapsLockIcon", Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree );
  382. 				value = regkey.GetValue( name, current ).ToString( );
  383. 				regkey.Close( );
  384. 			}
  385. 			catch ( System.Exception )
  386. 			{
  387. 				if ( regkey != null )
  388. 				{
  389. 					regkey.Close( );
  390. 				}
  391. 			}
  392. 			return value;
  393. 		}
  394.  
  395.  
  396. 		static bool WriteRegValue( string name, bool value )
  397. 		{
  398. 			Microsoft.Win32.RegistryKey regkey = null;
  399. 			try
  400. 			{
  401. 				regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "SOFTWARE\\RobvanderWoude\\CapsLockIcon", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree );
  402. 				regkey.SetValue( name, ( value ? 1 : 0 ), Microsoft.Win32.RegistryValueKind.DWord );
  403. 				regkey.Close( );
  404. 				return true;
  405. 			}
  406. 			catch ( System.Exception )
  407. 			{
  408. 				if ( regkey != null )
  409. 				{
  410. 					regkey.Close( );
  411. 				}
  412. 				return false;
  413. 			}
  414. 		}
  415.  
  416.  
  417. 		static bool WriteRegValue( string name, string value )
  418. 		{
  419. 			Microsoft.Win32.RegistryKey regkey = null;
  420. 			try
  421. 			{
  422. 				regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( "SOFTWARE\\RobvanderWoude\\CapsLockIcon", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree );
  423. 				regkey.SetValue( name, value, Microsoft.Win32.RegistryValueKind.String );
  424. 				regkey.Close( );
  425. 				return true;
  426. 			}
  427. 			catch ( System.Exception )
  428. 			{
  429. 				if ( regkey != null )
  430. 				{
  431. 					regkey.Close( );
  432. 				}
  433. 				return false;
  434. 			}
  435. 		}
  436.  
  437. 		#endregion Registry
  438.  
  439.  
  440. 		#region Event Handlers
  441.  
  442. 		private void ButtonCancel_Click( object sender, System.EventArgs e )
  443. 		{
  444. 			formsettings.Close( );
  445. 		}
  446.  
  447.  
  448. 		private void ButtonSave_Click( object sender, System.EventArgs e )
  449. 		{
  450. 			SaveSettings( );
  451. 		}
  452.  
  453.  
  454. 		private void Checkboxhidewhenoff_KeyPress( object sender, System.Windows.Forms.KeyPressEventArgs e )
  455. 		{
  456. 			UpdateSettingsUI( );
  457. 		}
  458.  
  459.  
  460. 		private void Checkboxhidewhenoff_Click( object sender, System.EventArgs e )
  461. 		{
  462. 			UpdateSettingsUI( );
  463. 		}
  464.  
  465.  
  466. 		private void LabelUrlChw_Click( object sender, System.EventArgs e )
  467. 		{
  468. 			OpenURL( "https://stackoverflow.com/a/11831856" );
  469. 		}
  470.  
  471.  
  472. 		private void LabelUrlFgs_Click( object sender, System.EventArgs e )
  473. 		{
  474. 			OpenURL( "https://stackoverflow.com/a/18623516" );
  475. 		}
  476.  
  477.  
  478. 		private void LabelUrlJfIcos_Click( object sender, System.EventArgs e )
  479. 		{
  480. 			OpenURL( "https://www.codeproject.com/Articles/7122/Dynamically-Generating-Icons-safely" );
  481. 		}
  482.  
  483.  
  484. 		private void LabelUrlRvdw_Click( object sender, System.EventArgs e )
  485. 		{
  486. 			OpenURL( "https://www.robvanderwoude.com/" );
  487. 		}
  488.  
  489.  
  490. 		private void MenuItemExit_Click( System.Object Sender, System.EventArgs e )
  491. 		{
  492. 			Quit( );
  493. 		}
  494.  
  495.  
  496. 		private void MenuItemSettings_Click( System.Object Sender, System.EventArgs e )
  497. 		{
  498. 			Settings( );
  499. 		}
  500.  
  501.  
  502. 		private void Timer_Tick( System.Object sender, System.EventArgs e )
  503. 		{
  504. 			capslock = System.Console.CapsLock;
  505. 			if ( capslock )
  506. 			{
  507. 				this.capslockicon.Visible = true;
  508. 				if ( GlobalSettings.FlashIfON )
  509. 				{
  510. 					if ( this.capslockicon.Icon.Equals( iconson[0] ) )
  511. 					{
  512. 						this.capslockicon.Icon = iconson[1];
  513. 					}
  514. 					else
  515. 					{
  516. 						this.capslockicon.Icon = iconson[0];
  517. 					}
  518. 				}
  519. 				else
  520. 				{
  521. 					this.capslockicon.Icon = iconson[0];
  522. 				}
  523. 			}
  524. 			else
  525. 			{
  526. 				if ( GlobalSettings.HideIfOFF )
  527. 				{
  528. 					this.capslockicon.Visible = false;
  529. 				}
  530. 				else
  531. 				{
  532. 					this.capslockicon.Visible = true;
  533. 					if ( GlobalSettings.FlashIfOFF )
  534. 					{
  535. 						if ( this.capslockicon.Icon.Equals( iconsoff[0] ) )
  536. 						{
  537. 							this.capslockicon.Icon = iconsoff[1];
  538. 						}
  539. 						else
  540. 						{
  541. 							this.capslockicon.Icon = iconsoff[0];
  542. 						}
  543. 					}
  544. 					else
  545. 					{
  546. 						this.capslockicon.Icon = iconsoff[0];
  547. 					}
  548. 				}
  549. 			}
  550. 		}
  551.  
  552. 		#endregion Event Handlers
  553.  
  554.  
  555. 		#region Overrides
  556.  
  557. 		/// <summary>
  558. 		/// Code to hide main form by Chriz on StackOverflow.com
  559. 		/// https://stackoverflow.com/a/11831856
  560. 		/// </summary>
  561. 		protected override void OnLoad( System.EventArgs e )
  562. 		{
  563. 			Visible = false; // Hide form window.
  564. 			ShowInTaskbar = false; // Remove from taskbar.
  565. 			Opacity = 0;
  566. 			base.OnLoad( e );
  567. 		}
  568.  
  569.  
  570. 		protected override void OnClosed( System.EventArgs e )
  571. 		{
  572. 			Quit( );
  573. 			base.OnClosed( e );
  574. 		}
  575.  
  576. 		#endregion Overrides
  577.  
  578.  
  579. 		public static class GlobalSettings
  580. 		{
  581. 			private static System.Collections.Generic.Dictionary<string, System.Drawing.Brush> _colors = new System.Collections.Generic.Dictionary<string, System.Drawing.Brush>( ) { { "LightCyan", System.Drawing.Brushes.LightCyan }, { "LightGreen", System.Drawing.Brushes.LightGreen }, { "Orange", System.Drawing.Brushes.Orange }, { "Red", System.Drawing.Brushes.Red }, { "Yellow", System.Drawing.Brushes.Yellow } };
  582. 			public static System.Collections.Generic.Dictionary<string, System.Drawing.Brush> Colors
  583. 			{
  584. 				get
  585. 				{
  586. 					return _colors;
  587. 				}
  588. 			}
  589.  
  590. 			private static string _indicatortext = "C";
  591. 			public static string IndicatorText
  592. 			{
  593. 				get
  594. 				{
  595. 					return _indicatortext;
  596. 				}
  597. 				set
  598. 				{
  599. 					_indicatortext = value;
  600. 				}
  601. 			}
  602.  
  603. 			private static bool _flashifoff = false;
  604. 			public static bool FlashIfOFF
  605. 			{
  606. 				get
  607. 				{
  608. 					return _flashifoff;
  609. 				}
  610. 				set
  611. 				{
  612. 					_flashifoff = value;
  613. 				}
  614. 			}
  615.  
  616. 			private static bool _flashifon = true;
  617. 			public static bool FlashIfON
  618. 			{
  619. 				get
  620. 				{
  621. 					return _flashifon;
  622. 				}
  623. 				set
  624. 				{
  625. 					_flashifon = value;
  626. 				}
  627. 			}
  628.  
  629. 			private static bool _hideifoff = false;
  630. 			public static bool HideIfOFF
  631. 			{
  632. 				get
  633. 				{
  634. 					return _hideifoff;
  635. 				}
  636. 				set
  637. 				{
  638. 					_hideifoff = value;
  639. 				}
  640. 			}
  641.  
  642. 			private static System.Drawing.Brush _indicatorcoloroff = System.Drawing.Brushes.LightGreen;
  643. 			public static System.Drawing.Brush IndicatorColorOFF
  644. 			{
  645. 				get
  646. 				{
  647. 					return _indicatorcoloroff;
  648. 				}
  649. 				set
  650. 				{
  651. 					_indicatorcoloroff = value;
  652. 				}
  653. 			}
  654.  
  655. 			private static System.Drawing.Brush _indicatorcoloron = System.Drawing.Brushes.Red;
  656. 			public static System.Drawing.Brush IndicatorColorON
  657. 			{
  658. 				get
  659. 				{
  660. 					return _indicatorcoloron;
  661. 				}
  662. 				set
  663. 				{
  664. 					_indicatorcoloron = value;
  665. 				}
  666. 			}
  667. 		}
  668. 	}
  669.  
  670.  
  671. 	public class ComboboxItem
  672. 	{
  673. 		public string Text
  674. 		{
  675. 			get; set;
  676. 		}
  677.  
  678. 		public System.Object Value
  679. 		{
  680. 			get; set;
  681. 		}
  682.  
  683. 		public override string ToString( )
  684. 		{
  685. 			return Text;
  686. 		}
  687. 	}
  688. }
  689.  

page last uploaded: 2021-01-27, 16:12