お久しぶりです。
今回は業務で利用しているC#を用いてアプリを作成しました。
開発にはClaude Sonnet4.5を利用しています。
きっかけ
アプリを作成したきっかけは
業務内で全画面スクリーンショットを撮る際、
Windows標準機能(Windows + PrintScreen)では保存先自動的に決まっているため、後から整理する手間に感じてました。
一部領域の切り取り(Windows + Shift + S)はそのまま使いつつ、タスクトレイ上のアプリをワンクリック+片手操作で任意の場所に全画面スクリーンショットを保存できるツールを作成したいと思いました。
アプリコード
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace ScreenshotApp
{
public partial class ScreenshotManager : Form
{
private string savePath;
private const string SETTINGS_FILE = "settings.txt";
public ScreenshotManager()
{
InitializeComponent();
// 設定を読み込み
LoadSettings();
// トレイアイコンの左クリックイベントを追加
trayIcon.Click += TrayIcon_Click;
trayIcon.ContextMenuStrip = contextMenu;
// フォームを非表示に
//this.Load += ScreenshotManager_Load;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
// トレイアイコンの左クリックイベント(追加)
private void TrayIcon_Click(object sender, EventArgs e)
{
MouseEventArgs me = e as MouseEventArgs;
if (me != null && me.Button == MouseButtons.Left)
{
TakeScreenshot();
}
}
private void LoadSettings()
{
if (File.Exists(SETTINGS_FILE))
{
savePath = File.ReadAllText(SETTINGS_FILE).Trim();
}
else
{
savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}
// ディレクトリが存在しない場合は作成
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
}
private void SaveSettings()
{
File.WriteAllText(SETTINGS_FILE, savePath);
}
private void TakeScreenshot()
{
// デバッグ用:撮影開始の通知
System.Diagnostics.Debug.WriteLine("スクリーンショット撮影開始");
try
{
// 全画面のサイズを取得
Rectangle bounds = Screen.PrimaryScreen.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
// ファイル名を生成
string fileName = $"Screenshot_{DateTime.Now:yyyyMMdd_HHmmss}.png";
string fullPath = Path.Combine(savePath, fileName);
// 保存
bitmap.Save(fullPath, ImageFormat.Png);
// 通知
trayIcon.ShowBalloonTip(2000, "スクリーンショット保存完了",
$"保存先: {fullPath}", ToolTipIcon.Info);
}
}
catch (Exception ex)
{
MessageBox.Show($"スクリーンショットの保存に失敗しました: {ex.Message}",
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenSettings()
{
using (設定 settingsForm = new 設定(savePath))
{
if (settingsForm.ShowDialog() == DialogResult.OK)
{
savePath = settingsForm.SavePath;
SaveSettings();
}
}
}
private void Exit()
{
trayIcon.Visible = false;
Application.Exit();
}
private void ToolStripMenuItem_設定_Click(object sender, EventArgs e)
{
OpenSettings();
}
private void ToolStripMenuItem_終了_Click(object sender, EventArgs e)
{
Exit();
}
}
}
using System;
using System.Windows.Forms;
namespace ScreenshotApp
{
public partial class 設定 : Form
{
public string SavePath { get; private set; }
public 設定(string currentPath)
{
InitializeComponent();
SavePath = currentPath;
txtSavePath.Text = SavePath;
}
private void BrowseFolder()
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.SelectedPath = txtSavePath.Text;
dialog.Description = "スクリーンショットの保存先を選択してください";
if (dialog.ShowDialog() == DialogResult.OK)
{
txtSavePath.Text = dialog.SelectedPath;
}
}
}
private void OK_Click()
{
if (string.IsNullOrWhiteSpace(txtSavePath.Text))
{
MessageBox.Show("保存先フォルダを指定してください", "エラー",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SavePath = txtSavePath.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void Cancel_Click()
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
OK_Click();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Cancel_Click();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
BrowseFolder();
}
}
}
アプリ画面(ファイルパス設定)
動作確認
起動後画面右下のタスクトレイ上にスクリーンショット機能が追加されます。
こちらのアイコンを左クリックするとスクリーンショットをとることができます。
右クリックすると保存先ファイルパスの設定と、アプリケーション自体を終了することができます。

おわりに
今回のアプリは本当はホットキーを用いて全画面スクリーンショットを行う仕様にしたかったのですが、
使用していないホットキーが見つからないため断念しました
(もう少し根気よく探した方がよかったでしょうか。。)
加えて親が使いたがっていたので、極力アプリを入れただけで全画面スクリーンショットをしたかっため、標準的なキーボードとマウスの場合も実行できる仕様にしました。
アプリ自体の規模は小さいためいい復習になりました。
これからもよろしくお願いいたします。


