Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for numlockicon.cs

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

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

page last uploaded: 2019-01-21, 22:48