clipboard-watcher/MainForm.cs

228 lines
5.8 KiB
C#
Raw Permalink Normal View History

2024-02-10 20:59:17 +00:00
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RyzStudio.Windows.Forms;
2024-02-10 20:59:17 +00:00
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;
2024-02-10 20:59:17 +00:00
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";
}
}
/// <summary>
/// New
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.Clear();
this.IsMonitoring = false;
}
/// <summary>
/// Close
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exitToolStripMenuItem2_Click(object sender, EventArgs e)
{
try
{
this.Close();
}
catch
{
}
}
/// <summary>
/// Options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsMonitoring) return;
}
/// <summary>
/// Help
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void viewHelpToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(new ProcessStartInfo()
{
FileName = AppResource.AppHelpURL,
UseShellExecute = true
});
}
catch
{
// do nothing
}
}
/// <summary>
/// About
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show(Application.ProductName + " v" + Application.ProductVersion, "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// Start/stop monitoring
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_MouseClick(object sender, MouseEventArgs e)
{
Clipboard.Clear();
this.IsMonitoring = !this.IsMonitoring;
}
/// <summary>
/// Clear
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_MouseClick(object sender, MouseEventArgs e)
{
Clipboard.Clear();
memoBox1.Text = string.Empty;
lineCount = 0;
2024-02-10 20:59:17 +00:00
}
/// <summary>
/// Close
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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()})");
2024-02-10 20:59:17 +00:00
}
}
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;
2024-02-10 20:59:17 +00:00
}
}
}