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

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