おなじもののC#版が欲しかったので、GoogleCLIに作ってもらったもの。
※私はソース書いてません。GoogleCLIによる生成です。
真似る対象
本当にクリップボードにコピーされたかどうかの不安を解消するためのツール
ソース
ClipMon.cs
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ClipMon : Form
{
private static class NativeMethods
{
public const int WM_CLIPBOARDUPDATE = 0x031D;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
}
public ClipMon()
{
// 隠しウィンドウとして設定
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Load += (s, e) => {
this.Size = Size.Empty;
NativeMethods.AddClipboardFormatListener(this.Handle);
};
this.FormClosed += (s, e) => NativeMethods.RemoveClipboardFormatListener(this.Handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE)
{
try
{
if (Clipboard.ContainsText())
{
string text = Clipboard.GetText();
ShowToast(text);
}
}
catch (Exception ex)
{
Console.WriteLine($"クリップボード取得エラー: {ex.Message}");
}
}
base.WndProc(ref m);
}
private void ShowToast(string text)
{
ToastForm toast = new ToastForm(text, 3000);
toast.Show();
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ClipMon());
}
}
public class ToastForm : Form
{
private System.Windows.Forms.Timer closeTimer;
public ToastForm(string text, int duration)
{
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.BackColor = Color.FromArgb(51, 51, 51);
this.TopMost = true;
Label label = new Label();
label.Text = text;
label.ForeColor = Color.White;
label.Font = new Font("Meiryo", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(128)));
label.Location = new Point(18, 12);
label.AutoSize = true;
this.Controls.Add(label);
// 画面サイズと最大サイズを計算
Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
int maxWidth = (int)(workingArea.Width / 2.8);
int maxHeight = (int)(workingArea.Height / 8.0);
label.MaximumSize = new Size(maxWidth, maxHeight);
// サイズと位置を調整
this.ClientSize = new Size(label.PreferredWidth + 36, label.PreferredHeight + 24);
this.Location = new Point(workingArea.Right - this.Width - 32, workingArea.Bottom - this.Height - 48);
closeTimer = new System.Windows.Forms.Timer();
closeTimer.Interval = duration;
closeTimer.Tick += (s, e) => { this.Close(); };
closeTimer.Start();
}
protected override void Dispose(bool disposing)
{
if (disposing && (closeTimer != null))
{
closeTimer.Dispose();
}
base.Dispose(disposing);
}
}
clipmon.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
ビルド方法
dotnet build
│ ClipMon.cs
│ clipmon.csproj
│
├─bin
│ └─Debug
│ └─net8.0-windows
│ clipmon.deps.json
│ clipmon.dll
│ clipmon.exe
│ clipmon.pdb
│ clipmon.runtimeconfig.json
│
└─obj
│ clipmon.csproj.nuget.dgspec.json
│ clipmon.csproj.nuget.g.props
│ clipmon.csproj.nuget.g.targets
│ project.assets.json
│ project.nuget.cache
│
└─Debug
└─net8.0-windows
│ .NETCoreApp,Version=v8.0.AssemblyAttributes.cs
│ apphost.exe
│ clipmon.AssemblyInfo.cs
│ clipmon.AssemblyInfoInputs.cache
│ clipmon.assets.cache
│ clipmon.csproj.CoreCompileInputs.cache
│ clipmon.csproj.FileListAbsolute.txt
│ clipmon.dll
│ clipmon.GeneratedMSBuildEditorConfig.editorconfig
│ clipmon.genruntimeconfig.cache
│ clipmon.GlobalUsings.g.cs
│ clipmon.pdb
│
├─ref
│ clipmon.dll
│
└─refint
clipmon.dll
※コピーした、「net8.0-windows」という文字列が右下に出ている


