Files
ScreenCaptureApp/MainForm.cs
2025-06-09 20:24:51 +07:00

171 lines
6.0 KiB
C#

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.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
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);
}
}
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)
{
using (var resultForm = new ResultForm(captureForm.CapturedImage))
{
if (resultForm.ShowDialog() == DialogResult.OK)
{
SaveImage(captureForm.CapturedImage);
}
else if (resultForm.DialogResult == DialogResult.Yes)
{
CopyToClipboard(captureForm.CapturedImage);
}
}
}
}
this.Show();
}
private void SaveImage(Image image)
{
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "PNG Image|*.png|JPEG Image|*.jpg|Bitmap Image|*.bmp";
sfd.Title = "Save Screenshot";
sfd.FileName = "screenshot.png";
if (sfd.ShowDialog() == DialogResult.OK)
{
image.Save(sfd.FileName, GetImageFormat(sfd.FileName));
MessageBox.Show("Screenshot saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void CopyToClipboard(Image image)
{
try
{
Clipboard.SetImage(image);
MessageBox.Show("Screenshot copied to clipboard!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to copy to clipboard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.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_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, PRINT_SCREEN_ID);
notifyIcon1.Visible = false;
notifyIcon1.Dispose();
}
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)
{
// Левый клик - показываем основное окно
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
}
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = false;
Application.Exit();
}
private void toggleHotkeyToolStripMenuItem_Click(object sender, EventArgs e)
{
captureOnKeyPress = !captureOnKeyPress;
var item = (ToolStripMenuItem)sender;
item.Text = captureOnKeyPress ? "Disable Print Screen capture" : "Enable Print Screen capture";
notifyIcon1.ShowBalloonTip(1000, "Screen Capture",
captureOnKeyPress ? "Print Screen capture enabled" : "Print Screen capture disabled",
ToolTipIcon.Info);
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
}
}
}
}