using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; 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(); 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); } } private void ExitApplication() { UnregisterHotKey(this.Handle, PRINT_SCREEN_ID); notifyIcon1.Visible = false; Application.Exit(); } 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() { this.Hide(); using (var captureForm = new ScreenCaptureForm()) { if (captureForm.ShowDialog() == DialogResult.OK) { ProcessCapturedImage(captureForm.CapturedImage); } } this.Show(); } private void ProcessCapturedImage(Image image) { using (var resultForm = new ResultForm(image)) { var result = resultForm.ShowDialog(); if (result == DialogResult.OK) { SaveImage(image); } 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); } } 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 notifyIcon1_MouseClick(object sender, EventArgs e) => ShowMainWindow(); private void showToolStripMenuItem_Click(object sender, EventArgs e) => ShowMainWindow(); 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")}", ToolTipIcon.Info); } private void exitToolStripMenuItem_Click(Object sender, EventArgs e) { ExitApplication(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Hide(); // Гарантированно скрываем окно при загрузке } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); ExitApplication(); } } }