clipboard-watcher/MainForm.cs
2024-07-21 14:24:07 +01:00

215 lines
5.5 KiB
C#

using System;
using System.ComponentModel;
using System.Windows.Forms;
using RyzStudio.Windows.Forms;
namespace ClipboardWatcher
{
public partial class MainForm : Form
{
private IntPtr _clipboardViewer;
private bool _isMonitoring = false;
private int _lineCount = 0;
public MainForm()
{
InitializeComponent();
memoBox1.TextBox.WordWrap = false;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
button2.Focus();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
this.IsMonitoring = false;
}
protected override void WndProc(ref Message m)
{
if ((m.Msg == RyzStudio.Runtime.InteropServices.User32.WM_DRAWCLIPBOARD) && this.IsMonitoring)
{
AddClipboardText();
}
base.WndProc(ref m);
}
public bool IsMonitoring
{
get => _isMonitoring;
set
{
_isMonitoring = value;
_clipboardViewer = (_isMonitoring ? RyzStudio.Runtime.InteropServices.User32.SetClipboardViewer(this.Handle) : default);
button2.LabelText = (_isMonitoring ? "Sto&p" : "&Start") + " Monitoring";
}
}
#region Main Menu
/// <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)
{
this.Close();
}
/// <summary>
/// Options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
// do nothing
}
/// <summary>
/// Help
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void viewHelpToolStripMenuItem1_Click(object sender, EventArgs e)
{
RyzStudio.Diagnostics.Process.Execute(AppResource.AppHelpURL);
}
/// <summary>
/// About
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
var form = new RyzStudio.Windows.ThemedForms.AboutForm();
form.ProductURL = AppResource.AppProductURL;
form.AuthorURL = AppResource.AppAuthorURL;
form.CompanyURL = AppResource.AppCompanyURL;
form.ProductCopyrightStartYear = 2024;
form.ProductLogo = AppResource.icon_64;
form.ShowDialog();
}
#endregion
/// <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;
}
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()})");
UIControl.BringToFront(this);
}
}
}