在c#中如何实时监控剪贴板上的内容
using System.Linq;
 
 
namespace onitroad
{
	public partial class FormMonitorClipboard : System.Windows.Forms.Form
	{
		static string clipboardtext = System.String.Empty;
		static string progver = System.Reflection.Assembly.GetExecutingAssembly( ).GetName( ).Version.ToString( );
		static string title = System.String.Format( "MonitorClipboard, Version {0}", progver );
		static string errormessage = System.String.Empty;
		static bool debugmode = false;
		static bool modalwindow = true;
		static int interval = 1;
		static int[] highestversion = new int[] { 0, 0, 0, 0 };
 
 
		public FormMonitorClipboard( )
		{
			InitializeComponent( );
		}
 
 
		private void FormMonitorClipboard_Load( object sender, System.EventArgs e )
		{
			this.Text = title;
			ParseCommandLine( );
			if ( debugmode )
			{
				DebugInfo( );
			}
			CheckClipboard( );
			FormMonitorClipboard.ActiveForm.TopMost = modalwindow;
			TimerMonitorClipboard.Interval = 1000 * interval;
			TimerMonitorClipboard.Start( );
		}
 
 
		private void CheckClipboard( )
		{
			if ( System.Windows.Forms.Clipboard.ContainsText( ) )
			{
				clipboardtext = System.Windows.Forms.Clipboard.GetText( System.Windows.Forms.TextDataFormat.UnicodeText );
				TextBoxClipboardContent.BackColor = System.Drawing.Color.White;
				TextBoxClipboardContent.ForeColor = System.Drawing.Color.Black;
				TextBoxClipboardContent.Text = clipboardtext;
			}
			else
			{
				TextBoxClipboardContent.BackColor = System.Windows.Forms.Form.DefaultBackColor;
				TextBoxClipboardContent.ForeColor = System.Drawing.Color.Gray;
				TextBoxClipboardContent.Text = System.String.Join( System.Environment.NewLine, System.Windows.Forms.Clipboard.GetDataObject( ).GetFormats( ).ToArray<string>( ) ); // requires Linq
			}
			TextBoxClipboardContent.SelectionStart = 0;
			TextBoxClipboardContent.SelectionLength = 0;
		}
 
 
		private void DebugInfo( )
		{
			//string programpath = new System.Uri( System.Reflection.Assembly.GetExecutingAssembly( ).CodeBase ).LocalPath;
			string programpath = System.Windows.Forms.Application.ExecutablePath;
			string[] arguments = System.Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
			string installednetframework = GetInstalledNETFrameworkVersion( );
			string requirednetframework = GetRequiredNETFrameworkVersion( );
			/*
 
			 */
			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 );
			System.Windows.Forms.Clipboard.SetText( debuginfo );
		}
 
 
		private string GetInstalledNETFrameworkVersion( )
		{
			string rc = System.String.Empty;
			System.Collections.Generic.List<string> installedversions = new System.Collections.Generic.List<string>( );
			// Get the list of installed .NET Framework versions from the registry, by Microsoft:
			// https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
			using ( Microsoft.Win32.RegistryKey ndpKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, "" ).OpenSubKey( @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" ) )
			{
				foreach ( string versionKeyName in ndpKey.GetSubKeyNames( ) )
				{
					if ( versionKeyName.StartsWith( "v" ) )
					{
						Microsoft.Win32.RegistryKey versionKey = ndpKey.OpenSubKey( versionKeyName );
						string name = (string) versionKey.GetValue( "Version", "" );
						if ( name == "" )
						{
							foreach ( string subKeyName in versionKey.GetSubKeyNames( ) )
							{
								Microsoft.Win32.RegistryKey subKey = versionKey.OpenSubKey( subKeyName );
								name = (string) subKey.GetValue( "Version", "" );
								if ( name != "" )
								{
									installedversions.Add( name );
								}
							}
						}
						else
						{
							installedversions.Add( name );
						}
					}
				}
			}
			// Determine the highest version
			foreach ( string version in installedversions )
			{
				highestversion = HighestVersion( highestversion, version );
			}
			return System.String.Join( ".", highestversion.Select( x => x.ToString( ) ).ToArray( ) ); // requires System.Linq
		}
 
 
		static string GetRequiredNETFrameworkVersion( )
		{
			// Get the required .NET Framework version
			// By Fernando Gonzalez Sanchez on StackOverflow.com
			// https://stackoverflow.com/a/18623516
			object[] list = System.Reflection.Assembly.GetExecutingAssembly( ).GetCustomAttributes( true );
			var attribute = list.OfType<System.Runtime.Versioning.TargetFrameworkAttribute>( ).First( ); // requires Linq
			string frameworkname = attribute.FrameworkName;
			string frameworkdisplayname = attribute.FrameworkDisplayName;
			return frameworkdisplayname;
		}
 
 
		static int[] HighestVersion( int[] version1, string version2 )
		{
			int[] converted2 = version2.Split( ".".ToCharArray( ) ).Select( x => System.Convert.ToInt32( x ) ).ToArray( );
			int minlength = System.Math.Min( version1.Length, converted2.Length );
			for ( int i = 0; i < minlength; i++ )
			{
				if ( version1[i] > converted2[i] )
				{
					return version1;
				}
				else if ( version1[i] < converted2[i] )
				{
					return converted2;
				}
			}
			return version1;
		}
 
 
		private void ParseCommandLine( )
		{
			string[] arguments = System.Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
			foreach ( string argument in arguments )
			{
				if ( argument.ToUpper( ).StartsWith( "/D" ) )
				{
					debugmode = true;
				}
				else if ( argument.StartsWith( "/I:", System.StringComparison.InvariantCultureIgnoreCase ) )
				{
					if ( !System.Int32.TryParse( argument.Substring( 3 ), out interval ) )
					{
						errormessage = System.String.Format( "Invalid interval: {0}", argument );
						ShowHelp( );
					}
					if ( interval < 1 || interval > 10 )
					{
						errormessage = System.String.Format( "Interval out of range (1..10): {0}", argument );
						ShowHelp( );
					}
				}
				else if ( argument.ToUpper( ) == "/NM" )
				{
					modalwindow = false;
				}
				else
				{
					errormessage = System.String.Format( "Invalid argument: {0}", argument );
					
				}
			}
		}
 
 
		 
 
		private void TimerMonitorClipboard_Tick( object sender, System.EventArgs e )
		{
			CheckClipboard( );
		}
 
 
		private void FormMonitorClipboard_FormClosing( object sender, System.Windows.Forms.FormClosingEventArgs e )
		{
			TimerMonitorClipboard.Stop( );
		}
 
 
	}
}
日期:2020-04-11 22:50:24 来源:oir作者:oir