using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; using RyzStudio.Windows.Forms; using RyzStudio.Windows.ThemedForms; namespace ClipboardWatcher { public partial class MainForm : Form { [DllImport("User32.dll", CharSet = CharSet.Auto)] protected static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer); protected const int WM_DRAWCLIPBOARD = 0x308; protected IntPtr _clipboardViewer; protected bool isMonitoring = false; protected int lineCount = 0; public MainForm() { InitializeComponent(); memoBox1.TextBox.WordWrap = false; } protected override void WndProc(ref Message m) { if ((m.Msg == WM_DRAWCLIPBOARD) && this.IsMonitoring) { AddClipboardText(); } base.WndProc(ref m); } public bool IsMonitoring { get => isMonitoring; set { isMonitoring = value; _clipboardViewer = (isMonitoring ? SetClipboardViewer(this.Handle) : default); button2.LabelText = (isMonitoring ? "Sto&p" : "&Start") + " Monitoring"; } } /// /// New /// /// /// private void newToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.Clear(); this.IsMonitoring = false; } /// /// Close /// /// /// private void exitToolStripMenuItem2_Click(object sender, EventArgs e) { try { this.Close(); } catch { } } /// /// Options /// /// /// private void optionsToolStripMenuItem_Click(object sender, EventArgs e) { if (this.IsMonitoring) return; } /// /// Help /// /// /// private void viewHelpToolStripMenuItem1_Click(object sender, EventArgs e) { try { System.Diagnostics.Process.Start(new ProcessStartInfo() { FileName = AppResource.AppHelpURL, UseShellExecute = true }); } catch { // do nothing } } /// /// About /// /// /// private void aboutToolStripMenuItem1_Click(object sender, EventArgs e) { MessageBox.Show(Application.ProductName + " v" + Application.ProductVersion, "About", MessageBoxButtons.OK, MessageBoxIcon.Information); } /// /// Start/stop monitoring /// /// /// private void button3_MouseClick(object sender, MouseEventArgs e) { Clipboard.Clear(); this.IsMonitoring = !this.IsMonitoring; } /// /// Clear /// /// /// private void button4_MouseClick(object sender, MouseEventArgs e) { Clipboard.Clear(); memoBox1.Text = string.Empty; lineCount = 0; } /// /// Close /// /// /// private void button1_MouseClick(object sender, MouseEventArgs e) { this.Close(); } private void memoBox1_DragOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { if (this.IsMonitoring) { e.Effect = DragDropEffects.None; } else { e.Effect = DragDropEffects.Copy; } } else { e.Effect = DragDropEffects.None; } } private void memoBox1_DragDrop(object sender, DragEventArgs e) { if (this.IsMonitoring) { return; } string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[]; if (fileList == null) { return; } foreach (string item in fileList) { memoBox1.Text += item + Environment.NewLine; lineCount++; UIControl.SetText(this, $"Clipboard Monitor ({lineCount.ToString()})"); } } private void AddClipboardText() { if (!Clipboard.ContainsText()) { return; } var clipboardText = Clipboard.GetText(); if (string.IsNullOrWhiteSpace(clipboardText)) { return; } memoBox1.Text += clipboardText.Trim() + Environment.NewLine; lineCount++; UIControl.SetText(this, $"Clipboard Monitor ({lineCount.ToString()})"); this.TopMost = true; this.BringToFront(); this.Focus(); this.TopMost = false; } } }