clipboard-watcher/MainForm.cs

215 lines
5.5 KiB
C#
Raw Permalink Normal View History

2024-02-10 20:59:17 +00:00
using System;
using System.ComponentModel;
2024-02-10 20:59:17 +00:00
using System.Windows.Forms;
using RyzStudio.Windows.Forms;
2024-02-10 20:59:17 +00:00
namespace ClipboardWatcher
{
public partial class MainForm : Form
{
private IntPtr _clipboardViewer;
private bool _isMonitoring = false;
private int _lineCount = 0;
2024-02-10 20:59:17 +00:00
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;
}
2024-02-10 20:59:17 +00:00
protected override void WndProc(ref Message m)
{
if ((m.Msg == RyzStudio.Runtime.InteropServices.User32.WM_DRAWCLIPBOARD) && this.IsMonitoring)
2024-02-10 20:59:17 +00:00
{
AddClipboardText();
}
base.WndProc(ref m);
}
public bool IsMonitoring
{
get => _isMonitoring;
2024-02-10 20:59:17 +00:00
set
{
_isMonitoring = value;
2024-02-10 20:59:17 +00:00
_clipboardViewer = (_isMonitoring ? RyzStudio.Runtime.InteropServices.User32.SetClipboardViewer(this.Handle) : default);
2024-02-10 20:59:17 +00:00
button2.LabelText = (_isMonitoring ? "Sto&p" : "&Start") + " Monitoring";
2024-02-10 20:59:17 +00:00
}
}
#region Main Menu
2024-02-10 20:59:17 +00:00
/// <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();
2024-02-10 20:59:17 +00:00
}
/// <summary>
/// Options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
// do nothing
2024-02-10 20:59:17 +00:00
}
/// <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);
2024-02-10 20:59:17 +00:00
}
/// <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;
2024-07-21 13:24:07 +00:00
form.ProductLogo = AppResource.icon_64;
form.ShowDialog();
2024-02-10 20:59:17 +00:00
}
#endregion
2024-02-10 20:59:17 +00:00
/// <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
}
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()})");
UIControl.BringToFront(this);
2024-02-10 20:59:17 +00:00
}
}
}