Update 2
Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
67
MainForm.cs
67
MainForm.cs
@@ -1,24 +1,59 @@
|
||||
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 btnCapture_Click(object sender, EventArgs e)
|
||||
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)
|
||||
@@ -75,5 +110,33 @@ namespace ScreenCaptureApp
|
||||
default: return System.Drawing.Imaging.ImageFormat.Png;
|
||||
}
|
||||
}
|
||||
|
||||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
UnregisterHotKey(this.Handle, PRINT_SCREEN_ID);
|
||||
}
|
||||
|
||||
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
|
||||
{
|
||||
this.WindowState = FormWindowState.Normal;
|
||||
this.ShowInTaskbar = true;
|
||||
}
|
||||
|
||||
private void showToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
this.WindowState = FormWindowState.Normal;
|
||||
this.ShowInTaskbar = true;
|
||||
}
|
||||
|
||||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
Application.Exit();
|
||||
}
|
||||
|
||||
private void toggleHotkeyToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
captureOnKeyPress = !captureOnKeyPress;
|
||||
toggleHotkeyToolStripMenuItem.Text = captureOnKeyPress ? "Disable Print Screen capture" : "Enable Print Screen capture";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user