Files
ScreenCaptureApp/MainForm.cs
2025-06-09 20:46:55 +07:00

145 lines
5.6 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenCaptureApp
{
public partial class MainForm : Form
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int WM_HOTKEY = 0x0312;
private const int PRINT_SCREEN_ID = 1;
private bool captureOnKeyPress = true;
public MainForm()
{
InitializeComponent();
if (!RegisterHotKey(this.Handle, PRINT_SCREEN_ID, 0x0, 0x2C))
{
notifyIcon1.ShowBalloonTip(1000, "Error", "Failed to register Print Screen hotkey", ToolTipIcon.Error);
}
this.WindowState = FormWindowState.Minimized;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY && captureOnKeyPress)
{
if (m.WParam.ToInt32() == PRINT_SCREEN_ID)
{
CaptureScreen();
}
}
base.WndProc(ref m);
}
private void CaptureScreen()
{
using (ScreenCaptureForm captureForm = new ScreenCaptureForm())
{
captureForm.Focus();
if (captureForm.ShowDialog() == DialogResult.OK)
{
using (captureForm.CapturedImage)
{
using (ResultForm resultForm = new ResultForm(captureForm.CapturedImage))
{
if (resultForm.ShowDialog() == DialogResult.OK)
{
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Title = "Save screenshot";
sfd.Filter = "PNG Image|*.png|JPEG Image|*.jpg|Bitmap Image|*.bmp";
sfd.FileName = $"screenshot_{DateTime.Now:dd-MM-yyyy_HH.mm.ss}";
if (sfd.ShowDialog() == DialogResult.OK)
{
captureForm.CapturedImage.Save(sfd.FileName, GetImageFormat(sfd.FileName));
notifyIcon1.ShowBalloonTip(1000, "Success", "Screenshot saved!", ToolTipIcon.Info);
}
}
}
else if (resultForm.DialogResult == DialogResult.Yes)
{
try
{
Clipboard.SetImage(captureForm.CapturedImage);
notifyIcon1.ShowBalloonTip(1000, "Success", "Copied to clipboard!", ToolTipIcon.Info);
}
catch (Exception ex)
{
notifyIcon1.ShowBalloonTip(1000, "Error", $"Copy failed: {ex.Message}", ToolTipIcon.Error);
}
}
}
}
}
}
}
private System.Drawing.Imaging.ImageFormat GetImageFormat(string fileName)
{
string ext = System.IO.Path.GetExtension(fileName).ToLower();
switch (ext)
{
case ".jpg": return System.Drawing.Imaging.ImageFormat.Jpeg;
case ".jpeg": return System.Drawing.Imaging.ImageFormat.Jpeg;
case ".bmp": return System.Drawing.Imaging.ImageFormat.Bmp;
default: return System.Drawing.Imaging.ImageFormat.Png;
}
}
private void MainForm_Load(object sender, EventArgs e)
{
disablePrintScreenCaptureToolStripMenuItem.Text = $"Print Screen Capture: {(captureOnKeyPress ? "ON" : "OFF")}";
this.Hide();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, PRINT_SCREEN_ID);
notifyIcon1.Visible = false;
this.Dispose(true);
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MethodInvoker mi = new MethodInvoker(() => {
notifyIcon1.ContextMenuStrip.Show(Cursor.Position);
});
this.BeginInvoke(mi);
}
else if (e.Button == MouseButtons.Left)
{
CaptureScreen();
}
}
private void toggleHotkeyToolStripMenuItem_Click(object sender, EventArgs e)
{
captureOnKeyPress = !captureOnKeyPress;
disablePrintScreenCaptureToolStripMenuItem.Text = $"Print Screen Capture: {(captureOnKeyPress ? "ON" : "OFF")}";
notifyIcon1.ShowBalloonTip(250, "Screen Capture",
captureOnKeyPress ? "Print Screen capture enabled" : "Print Screen capture disabled",
ToolTipIcon.Info);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
base.OnFormClosing(e as FormClosingEventArgs);
Application.Exit();
}
private void bigButton_Click(object sender, EventArgs e)
{
CaptureScreen();
}
}
}