(view source code of monitorclipboard.cs as plain text)
using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Windows.Forms;
using Microsoft.Win32;
namespace RobvanderWoude
{
public partial class FormMonitorClipboard : Form
{
static string clipboardtext = string.Empty;
static string progver = Application.ProductVersion.ToString( );
static string title = string.Format( "MonitorClipboard, Version {0}", progver );
static string errormessage = string.Empty;
static bool debugmode = false;
static bool modalwindow = true;
static bool compactmode = false;
static float fontsizefactor = 1F;
static int interval = 1;
static int[] highestversion = new int[] { 0, 0, 0, 0 };
public FormMonitorClipboard( )
{
InitializeComponent( );
}
private void FormMonitorClipboard_Load( object sender, EventArgs e )
{
this.Text = title;
ParseCommandLine( );
if ( debugmode )
{
DebugInfo( );
}
if ( compactmode )
{
CompactWindow( );
}
CheckClipboard( );
this.TopMost = modalwindow;
TimerMonitorClipboard.Interval = 1000 * interval;
TimerMonitorClipboard.Start( );
}
private void CheckClipboard( )
{
if ( Clipboard.ContainsText( ) )
{
clipboardtext = Clipboard.GetText( TextDataFormat.UnicodeText );
TextBoxClipboardContent.BackColor = Color.White;
TextBoxClipboardContent.ForeColor = Color.Black;
TextBoxClipboardContent.Text = clipboardtext;
}
else
{
TextBoxClipboardContent.BackColor = Form.DefaultBackColor;
TextBoxClipboardContent.ForeColor = Color.Gray;
TextBoxClipboardContent.Text = string.Join( Environment.NewLine, Clipboard.GetDataObject( ).GetFormats( ).ToArray<string>( ) ); // requires Linq
}
TextBoxClipboardContent.SelectionStart = 0;
TextBoxClipboardContent.SelectionLength = 0;
}
private void CompactWindow( )
{
this.ClientSize = new Size( (int)( Screen.PrimaryScreen.Bounds.Width / 2 ), 40 );
this.Location = new Point( (int)( Screen.PrimaryScreen.Bounds.Width / 4 ), Screen.PrimaryScreen.Bounds.Height - 40 );
this.TopLevel = true;
this.ControlBox = false;
this.Text = string.Empty;
this.FormBorderStyle = FormBorderStyle.None;
TextBoxClipboardContent.Size = new Size( this.ClientSize.Width - 40, this.ClientSize.Height );
TextBoxClipboardContent.Location = new Point( 0, 0 );
TextBoxClipboardContent.Font = new Font( TextBoxClipboardContent.Font.FontFamily, (float)( TextBoxClipboardContent.Font.Size * fontsizefactor ), FontStyle.Regular );
Button exitbutton = new Button( );
exitbutton.Text = "Exit";
exitbutton.Size = new Size( 32, 32 );
exitbutton.Location = new Point( this.ClientSize.Width - 36, 4 );
exitbutton.Click += Exitbutton_Click;
this.Controls.Add( exitbutton );
}
private void DebugInfo( )
{
string programpath = Application.ExecutablePath;
string[] arguments = Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
string installednetframework = GetInstalledNETFrameworkVersion( );
string requirednetframework = GetRequiredNETFrameworkVersion( );
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 );
Clipboard.SetText( debuginfo );
}
private string GetInstalledNETFrameworkVersion( )
{
string rc = 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 ( RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey( RegistryHive.LocalMachine, "" ).OpenSubKey( @"SOFTWARE\Microsoft\NET Framework Setup\NDP\" ) )
{
foreach ( string versionKeyName in ndpKey.GetSubKeyNames( ) )
{
if ( versionKeyName.StartsWith( "v" ) )
{
RegistryKey versionKey = ndpKey.OpenSubKey( versionKeyName );
string name = (string) versionKey.GetValue( "Version", "" );
if ( name == "" )
{
foreach ( string subKeyName in versionKey.GetSubKeyNames( ) )
{
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 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 = Assembly.GetExecutingAssembly( ).GetCustomAttributes( true );
var attribute = list.OfType<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 = 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 = Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( ); // requires Linq
foreach ( string argument in arguments )
{
if ( argument.ToUpper( ) == "/CM" )
{
compactmode = true;
}
else if ( argument.ToUpper( ).StartsWith( "/D" ) )
{
debugmode = true;
}
else if ( argument.StartsWith( "/F:", StringComparison.InvariantCultureIgnoreCase ) )
{
int fontsizepercentage = 100;
if ( !int.TryParse( argument.Substring( 3 ), out fontsizepercentage ) )
{
errormessage = string.Format( "Invalid font size percentage: {0}", argument );
ShowHelp( );
}
else
{
if ( fontsizepercentage < 50 || fontsizepercentage > 150 )
{
errormessage = string.Format( "Font size percentage out of range (50..150): {0}", argument );
ShowHelp( );
}
else
{
fontsizefactor = fontsizepercentage / 100F;
}
}
}
else if ( argument.StartsWith( "/I:", StringComparison.InvariantCultureIgnoreCase ) )
{
if ( !int.TryParse( argument.Substring( 3 ), out interval ) )
{
errormessage = string.Format( "Invalid interval: {0}", argument );
ShowHelp( );
}
if ( interval < 1 || interval > 10 )
{
errormessage = string.Format( "Interval out of range (1..10): {0}", argument );
ShowHelp( );
}
}
else if ( argument.ToUpper( ) == "/NM" )
{
modalwindow = false;
}
else if ( argument == "/?" )
{
ShowHelp( );
}
else
{
errormessage = string.Format( "Invalid argument: {0}", argument );
ShowHelp( );
}
}
}
private void ShowHelp( )
{
string caption = title;
MessageBoxIcon icon = MessageBoxIcon.Information;
if ( !string.IsNullOrWhiteSpace( errormessage ) )
{
caption = errormessage;
errormessage = string.Empty;
icon = MessageBoxIcon.Error;
}
string message = string.Format( "{0}\nMonitor text on the clipboard in realtime\n\n", title );
message += "Usage:\tMONITORCLIPBOARD.EXE [ options ]\n\n";
message += "Options:\t/I:sec\tdefines the Interval in seconds with\n";
message += " \t \twhich the clipboard is checked for text\n";
message += " \t \t(1..10 seconds; default: 1 second)\n";
message += " \t/CM \tCompact Mode: small window over taskbar\n";
message += " \t \t(default: normal window at center\n";
message += " \t \tof screen)\n";
message += " \t/F:perc\tFont size percentage\n";
message += " \t \t(50..150; default: 100)\n";
message += " \t/NM \tsets the program's window to Non-Modal\n";
message += " \t \t(default: always on top)\n";
message += " \t/D \tDebug mode: send some debugging\n";
message += " \t \tinformation to the clipboard\n";
message += " \t \t(warning: this will overwrite the\n";
message += " \t \tcurrent clipboard content)\n\n";
message += "Note: \tThough invalid or duplicate arguments DO raise\n";
message += " \tan error message, they are ignored.\n\n";
message += "Written by Rob van der Woude\n";
message += "https://www.robvanderwoude.com";
MessageBox.Show( message, caption, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification );
}
private void Exitbutton_Click( object sender, EventArgs e )
{
Application.Exit( );
}
private void FormMonitorClipboard_FormClosing( object sender, FormClosingEventArgs e )
{
TimerMonitorClipboard.Stop( );
}
private void FormMonitorClipboard_HelpRequested( object sender, HelpEventArgs hlpevent )
{
ShowHelp( );
}
private void TimerMonitorClipboard_Tick( object sender, System.EventArgs e )
{
CheckClipboard( );
}
}
}
page last modified: 2023-03-10