Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for monitorclipboard.cs

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

  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.Versioning;
  6. using System.Windows.Forms;
  7. using Microsoft.Win32;
  8.  
  9.  
  10. namespace RobvanderWoude
  11. {
  12. 	public partial class FormMonitorClipboard : Form
  13. 	{
  14. 		static string clipboardtext = string.Empty;
  15. 		static string progver = Application.ProductVersion.ToString( );
  16. 		static string title = string.Format( "MonitorClipboard, Version {0}", progver );
  17. 		static string errormessage = string.Empty;
  18. 		static bool debugmode = false;
  19. 		static bool modalwindow = true;
  20. 		static bool compactmode = false;
  21. 		static float fontsizefactor = 1F;
  22. 		static int interval = 1;
  23. 		static int[] highestversion = new int[] { 0, 0, 0, 0 };
  24.  
  25.  
  26. 		public FormMonitorClipboard( )
  27. 		{
  28. 			InitializeComponent( );
  29. 		}
  30.  
  31.  
  32. 		private void FormMonitorClipboard_Load( object sender, EventArgs e )
  33. 		{
  34. 			this.Text = title;
  35. 			ParseCommandLine( );
  36. 			if ( debugmode )
  37. 			{
  38. 				DebugInfo( );
  39. 			}
  40. 			if ( compactmode )
  41. 			{
  42. 				CompactWindow( );
  43. 			}
  44. 			CheckClipboard( );
  45. 			this.TopMost = modalwindow;
  46. 			TimerMonitorClipboard.Interval = 1000 * interval;
  47. 			TimerMonitorClipboard.Start( );
  48. 		}
  49.  
  50. 		private void CheckClipboard( )
  51. 		{
  52. 			if ( Clipboard.ContainsText( ) )
  53. 			{
  54. 				clipboardtext = Clipboard.GetText( TextDataFormat.UnicodeText );
  55. 				TextBoxClipboardContent.BackColor = Color.White;
  56. 				TextBoxClipboardContent.ForeColor = Color.Black;
  57. 				TextBoxClipboardContent.Text = clipboardtext;
  58. 			}
  59. 			else
  60. 			{
  61. 				TextBoxClipboardContent.BackColor = Form.DefaultBackColor;
  62. 				TextBoxClipboardContent.ForeColor = Color.Gray;
  63. 				TextBoxClipboardContent.Text = string.Join( Environment.NewLine, Clipboard.GetDataObject( ).GetFormats( ).ToArray<string>( ) ); // requires Linq
  64. 			}
  65. 			TextBoxClipboardContent.SelectionStart = 0;
  66. 			TextBoxClipboardContent.SelectionLength = 0;
  67. 		}
  68.  
  69.  
  70. 		private void CompactWindow( )
  71. 		{
  72. 			this.ClientSize = new Size( (int)( Screen.PrimaryScreen.Bounds.Width / 2 ), 40 );
  73. 			this.Location = new Point( (int)( Screen.PrimaryScreen.Bounds.Width / 4 ), Screen.PrimaryScreen.Bounds.Height - 40 );
  74. 			this.TopLevel = true;
  75. 			this.ControlBox = false;
  76. 			this.Text = string.Empty;
  77. 			this.FormBorderStyle = FormBorderStyle.None;
  78. 			TextBoxClipboardContent.Size = new Size( this.ClientSize.Width - 40, this.ClientSize.Height );
  79. 			TextBoxClipboardContent.Location = new Point( 0, 0 );
  80. 			TextBoxClipboardContent.Font = new Font( TextBoxClipboardContent.Font.FontFamily, (float)( TextBoxClipboardContent.Font.Size * fontsizefactor ), FontStyle.Regular );
  81. 			Button exitbutton = new Button( );
  82. 			exitbutton.Text = "Exit";
  83. 			exitbutton.Size = new Size( 32, 32 );
  84. 			exitbutton.Location = new Point( this.ClientSize.Width - 36, 4 );
  85. 			exitbutton.Click += Exitbutton_Click;
  86. 			this.Controls.Add( exitbutton );
  87. 		}
  88.  
  89.  
  90. 		private void DebugInfo( )
  91. 		{
  92. 			string programpath = Application.ExecutablePath;
  93. 			string[] arguments = Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
  94. 			string installednetframework = GetInstalledNETFrameworkVersion( );
  95. 			string requirednetframework = GetRequiredNETFrameworkVersion( );
  96. 			string debuginfo = string.Format( "DEBUGGING INFO:{0}==============={0}Program path           : {1}{0}Required .NET version  : {2}{0}Installed .NET version : {3}{0}Command line arguments : {4}{0}Timer interval         : {5}{0}Modal window           : {6}", System.Environment.NewLine, programpath, requirednetframework, installednetframework, string.Join( " ", arguments ), interval, modalwindow );
  97. 			Clipboard.SetText( debuginfo );
  98. 		}
  99.  
  100.  
  101. 		private string GetInstalledNETFrameworkVersion( )
  102. 		{
  103. 			string rc = string.Empty;
  104. 			System.Collections.Generic.List<string> installedversions = new System.Collections.Generic.List<string>( );
  105. 			// Get the list of installed .NET Framework versions from the registry, by Microsoft:
  106. 			// https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
  107. 			using ( RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey( RegistryHive.LocalMachine, "" ).OpenSubKey( @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" ) )
  108. 			{
  109. 				foreach ( string versionKeyName in ndpKey.GetSubKeyNames( ) )
  110. 				{
  111. 					if ( versionKeyName.StartsWith( "v" ) )
  112. 					{
  113. 						RegistryKey versionKey = ndpKey.OpenSubKey( versionKeyName );
  114. 						string name = (string) versionKey.GetValue( "Version", "" );
  115. 						if ( name == "" )
  116. 						{
  117. 							foreach ( string subKeyName in versionKey.GetSubKeyNames( ) )
  118. 							{
  119. 								RegistryKey subKey = versionKey.OpenSubKey( subKeyName );
  120. 								name = (string) subKey.GetValue( "Version", "" );
  121. 								if ( name != "" )
  122. 								{
  123. 									installedversions.Add( name );
  124. 								}
  125. 							}
  126. 						}
  127. 						else
  128. 						{
  129. 							installedversions.Add( name );
  130. 						}
  131. 					}
  132. 				}
  133. 			}
  134. 			// Determine the highest version
  135. 			foreach ( string version in installedversions )
  136. 			{
  137. 				highestversion = HighestVersion( highestversion, version );
  138. 			}
  139. 			return string.Join( ".", highestversion.Select( x => x.ToString( ) ).ToArray( ) ); // requires System.Linq
  140. 		}
  141.  
  142.  
  143. 		static string GetRequiredNETFrameworkVersion( )
  144. 		{
  145. 			// Get the required .NET Framework version
  146. 			// By Fernando Gonzalez Sanchez on StackOverflow.com
  147. 			// https://stackoverflow.com/a/18623516
  148. 			object[] list = Assembly.GetExecutingAssembly( ).GetCustomAttributes( true );
  149. 			var attribute = list.OfType<TargetFrameworkAttribute>( ).First( ); // requires Linq
  150. 			string frameworkname = attribute.FrameworkName;
  151. 			string frameworkdisplayname = attribute.FrameworkDisplayName;
  152. 			return frameworkdisplayname;
  153. 		}
  154.  
  155.  
  156. 		static int[] HighestVersion( int[] version1, string version2 )
  157. 		{
  158. 			int[] converted2 = version2.Split( ".".ToCharArray( ) ).Select( x => System.Convert.ToInt32( x ) ).ToArray( );
  159. 			int minlength = Math.Min( version1.Length, converted2.Length );
  160. 			for ( int i = 0; i < minlength; i++ )
  161. 			{
  162. 				if ( version1[i] > converted2[i] )
  163. 				{
  164. 					return version1;
  165. 				}
  166. 				else if ( version1[i] < converted2[i] )
  167. 				{
  168. 					return converted2;
  169. 				}
  170. 			}
  171. 			return version1;
  172. 		}
  173.  
  174.  
  175. 		private void ParseCommandLine( )
  176. 		{
  177. 			string[] arguments = Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
  178. 			foreach ( string argument in arguments )
  179. 			{
  180. 				if ( argument.ToUpper( ) == "/CM" )
  181. 				{
  182. 					compactmode = true;
  183. 				}
  184. 				else if ( argument.ToUpper( ).StartsWith( "/D" ) )
  185. 				{
  186. 					debugmode = true;
  187. 				}
  188. 				else if ( argument.StartsWith( "/F:", StringComparison.InvariantCultureIgnoreCase ) )
  189. 				{
  190. 					int fontsizepercentage = 100;
  191. 					if ( !int.TryParse( argument.Substring( 3 ), out fontsizepercentage ) )
  192. 					{
  193. 						errormessage = string.Format( "Invalid font size percentage: {0}", argument );
  194. 						ShowHelp( );
  195. 					}
  196. 					else
  197. 					{
  198. 						if ( fontsizepercentage < 50 || fontsizepercentage > 150 )
  199. 						{
  200. 							errormessage = string.Format( "Font size percentage out of range (50..150): {0}", argument );
  201. 							ShowHelp( );
  202. 						}
  203. 						else
  204. 						{
  205. 							fontsizefactor = fontsizepercentage / 100F;
  206. 						}
  207. 					}
  208. 				}
  209. 				else if ( argument.StartsWith( "/I:", StringComparison.InvariantCultureIgnoreCase ) )
  210. 				{
  211. 					if ( !int.TryParse( argument.Substring( 3 ), out interval ) )
  212. 					{
  213. 						errormessage = string.Format( "Invalid interval: {0}", argument );
  214. 						ShowHelp( );
  215. 					}
  216. 					if ( interval < 1 || interval > 10 )
  217. 					{
  218. 						errormessage = string.Format( "Interval out of range (1..10): {0}", argument );
  219. 						ShowHelp( );
  220. 					}
  221. 				}
  222. 				else if ( argument.ToUpper( ) == "/NM" )
  223. 				{
  224. 					modalwindow = false;
  225. 				}
  226. 				else if ( argument == "/?" )
  227. 				{
  228. 					ShowHelp( );
  229. 				}
  230. 				else
  231. 				{
  232. 					errormessage = string.Format( "Invalid argument: {0}", argument );
  233. 					ShowHelp( );
  234. 				}
  235. 			}
  236. 		}
  237.  
  238.  
  239. 		private void ShowHelp( )
  240. 		{
  241. 			string caption = title;
  242. 			MessageBoxIcon icon = MessageBoxIcon.Information;
  243. 			if ( !string.IsNullOrWhiteSpace( errormessage ) )
  244. 			{
  245. 				caption = errormessage;
  246. 				errormessage = string.Empty;
  247. 				icon = MessageBoxIcon.Error;
  248. 			}
  249. 			string message = string.Format( "{0}\nMonitor text on the clipboard in realtime\n\n", title );
  250. 			message += "Usage:\tMONITORCLIPBOARD.EXE  [ options ]\n\n";
  251. 			message += "Options:\t/I:sec\tdefines the Interval in seconds with\n";
  252. 			message += "      \t      \twhich the clipboard is checked for text\n";
  253. 			message += "      \t      \t(1..10 seconds; default: 1 second)\n";
  254. 			message += "      \t/CM   \tCompact Mode: small window over taskbar\n";
  255. 			message += "      \t      \t(default: normal window at center\n";
  256. 			message += "      \t      \tof screen)\n";
  257. 			message += "      \t/F:perc\tFont size percentage\n";
  258. 			message += "      \t      \t(50..150; default: 100)\n";
  259. 			message += "      \t/NM   \tsets the program's window to Non-Modal\n";
  260. 			message += "      \t      \t(default: always on top)\n";
  261. 			message += "      \t/D    \tDebug mode: send some debugging\n";
  262. 			message += "      \t      \tinformation to the clipboard\n";
  263. 			message += "      \t      \t(warning: this will overwrite the\n";
  264. 			message += "      \t      \tcurrent clipboard content)\n\n";
  265. 			message += "Note: \tThough invalid or duplicate arguments DO raise\n";
  266. 			message += "      \tan error message, they are ignored.\n\n";
  267. 			message += "Written by Rob van der Woude\n";
  268. 			message += "https://www.robvanderwoude.com";
  269. 			MessageBox.Show( message, caption, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification );
  270. 		}
  271.  
  272.  
  273. 		private void Exitbutton_Click( object sender, EventArgs e )
  274. 		{
  275. 			Application.Exit( );
  276. 		}
  277.  
  278.  
  279. 		private void FormMonitorClipboard_FormClosing( object sender, FormClosingEventArgs e )
  280. 		{
  281. 			TimerMonitorClipboard.Stop( );
  282. 		}
  283.  
  284.  
  285. 		private void FormMonitorClipboard_HelpRequested( object sender, HelpEventArgs hlpevent )
  286. 		{
  287. 			ShowHelp( );
  288. 		}
  289.  
  290.  
  291. 		private void TimerMonitorClipboard_Tick( object sender, System.EventArgs e )
  292. 		{
  293. 			CheckClipboard( );
  294. 		}
  295. 	}
  296. }
  297.  

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