139 lines
5.6 KiB
C#
139 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, "Ошибка", "Не удалось привязать Print Screen", 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 = "Сохранить скриншот";
|
||
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, "Успех", "Скриншот сохранен!", ToolTipIcon.Info);
|
||
}
|
||
}
|
||
}
|
||
else if (resultForm.DialogResult == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
Clipboard.SetImage(captureForm.CapturedImage);
|
||
notifyIcon1.ShowBalloonTip(1000, "Успех", "Скриншот скопирован в буфер обмена!", ToolTipIcon.Info);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
notifyIcon1.ShowBalloonTip(1000, "Ошибка", $"Копирование не удалось: {ex.Message}", ToolTipIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private System.Drawing.Imaging.ImageFormat GetImageFormat(string fileName)
|
||
{
|
||
switch (System.IO.Path.GetExtension(fileName).ToLower())
|
||
{
|
||
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)
|
||
{
|
||
toggleHotkeyToolStripMenuItem.Text = $"Отлик на Print Screen: {(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;
|
||
toggleHotkeyToolStripMenuItem.Text = $"Отлик на Print Screen: {(captureOnKeyPress ? "ON" : "OFF")}";
|
||
notifyIcon1.ShowBalloonTip(250, "Захват экрана",
|
||
captureOnKeyPress ? "Отлик на Print Screen включен" : "Отлик на Print Screen отключен",
|
||
ToolTipIcon.Info);
|
||
}
|
||
|
||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
base.OnFormClosing(e as FormClosingEventArgs);
|
||
Application.Exit();
|
||
}
|
||
}
|
||
} |