Final revision

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2025-06-09 20:46:31 +07:00
parent 09cee74f0c
commit 594217203d
6 changed files with 137 additions and 135 deletions

View File

@@ -1,7 +1,6 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenCaptureApp
{
@@ -20,34 +19,11 @@ namespace ScreenCaptureApp
public MainForm()
{
InitializeComponent();
RegisterHotkey();
this.Hide(); // Сразу скрываем основное окно
}
private void ShowMainWindow()
{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Show();
this.Activate();
}
private void RegisterHotkey()
{
// Регистрируем Print Screen (0x2C) без модификаторов (0x0)
if (!RegisterHotKey(this.Handle, PRINT_SCREEN_ID, 0x0, 0x2C))
{
MessageBox.Show("Failed to register Print Screen hotkey", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
notifyIcon1.ShowBalloonTip(1000, "Error", "Failed to register Print Screen hotkey", ToolTipIcon.Error);
}
}
private void ExitApplication()
{
UnregisterHotKey(this.Handle, PRINT_SCREEN_ID);
notifyIcon1.Visible = false;
Application.Exit();
this.WindowState = FormWindowState.Minimized;
}
protected override void WndProc(ref Message m)
@@ -64,56 +40,45 @@ namespace ScreenCaptureApp
private void CaptureScreen()
{
this.Hide();
using (var captureForm = new ScreenCaptureForm())
using (ScreenCaptureForm captureForm = new ScreenCaptureForm())
{
captureForm.Focus();
if (captureForm.ShowDialog() == DialogResult.OK)
{
ProcessCapturedImage(captureForm.CapturedImage);
}
}
this.Show();
}
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}";
private void ProcessCapturedImage(Image image)
{
using (var resultForm = new ResultForm(image))
{
var result = resultForm.ShowDialog();
if (result == DialogResult.OK)
{
SaveImage(image);
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);
}
}
}
}
}
else if (result == DialogResult.Yes)
{
CopyToClipboard(image);
}
}
}
private void SaveImage(Image image)
{
using (var sfd = new SaveFileDialog())
{
sfd.Filter = "PNG Image|*.png|JPEG Image|*.jpg|Bitmap Image|*.bmp";
if (sfd.ShowDialog() == DialogResult.OK)
{
image.Save(sfd.FileName, GetImageFormat(sfd.FileName));
notifyIcon1.ShowBalloonTip(1000, "Success", "Screenshot saved!", ToolTipIcon.Info);
}
}
}
private void CopyToClipboard(Image image)
{
try
{
Clipboard.SetImage(image);
notifyIcon1.ShowBalloonTip(1000, "Success", "Copied to clipboard!", ToolTipIcon.Info);
}
catch (Exception ex)
{
notifyIcon1.ShowBalloonTip(1000, "Error", $"Copy failed: {ex.Message}", ToolTipIcon.Error);
}
}
@@ -129,35 +94,52 @@ namespace ScreenCaptureApp
}
}
private void notifyIcon1_MouseClick(object sender, EventArgs e) => ShowMainWindow();
private void MainForm_Load(object sender, EventArgs e)
{
disablePrintScreenCaptureToolStripMenuItem.Text = $"Print Screen Capture: {(captureOnKeyPress ? "ON" : "OFF")}";
this.Hide();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) => ShowMainWindow();
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;
var item = (ToolStripMenuItem)sender;
item.Text = $"Print Screen Capture: {(captureOnKeyPress ? "ON" : "OFF")}";
notifyIcon1.ShowBalloonTip(1000, "Hotkey",
$"Print Screen capture {(captureOnKeyPress ? "enabled" : "disabled")}",
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)
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
ExitApplication();
base.OnFormClosing(e as FormClosingEventArgs);
Application.Exit();
}
protected override void OnLoad(EventArgs e)
private void bigButton_Click(object sender, EventArgs e)
{
base.OnLoad(e);
this.Hide(); // Гарантированно скрываем окно при загрузке
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
ExitApplication();
CaptureScreen();
}
}
}