Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for scrolllockicon.cs

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

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

page last modified: 2024-04-16; loaded in 0.0160 seconds