From 594217203d5da0d9236ea953af806d751a86244c Mon Sep 17 00:00:00 2001 From: Lev Rusanov <30170278+JDM170@users.noreply.github.com> Date: Mon, 9 Jun 2025 20:46:31 +0700 Subject: [PATCH] Final revision Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com> --- MainForm.Designer.cs | 38 ++++------ MainForm.cs | 160 ++++++++++++++++++----------------------- MainForm.resx | 4 +- ResultForm.Designer.cs | 57 +++++++++++---- ResultForm.cs | 4 +- ScreenCaptureForm.cs | 9 ++- 6 files changed, 137 insertions(+), 135 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index fcb3433..fcb7328 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -32,8 +32,7 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); - this.showToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.toggleHotkeyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.disablePrintScreenCaptureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip1.SuspendLayout(); @@ -43,33 +42,26 @@ // this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); - this.notifyIcon1.Text = "notifyIcon1"; + this.notifyIcon1.Text = "ScreenCaptureApp"; this.notifyIcon1.Visible = true; this.notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.showToolStripMenuItem, - this.toggleHotkeyToolStripMenuItem, + this.disablePrintScreenCaptureToolStripMenuItem, this.toolStripSeparator1, this.exitToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; - this.contextMenuStrip1.Size = new System.Drawing.Size(222, 76); + this.contextMenuStrip1.Size = new System.Drawing.Size(222, 54); + this.contextMenuStrip1.Text = "test"; // - // showToolStripMenuItem + // disablePrintScreenCaptureToolStripMenuItem // - this.showToolStripMenuItem.Name = "showToolStripMenuItem"; - this.showToolStripMenuItem.Size = new System.Drawing.Size(221, 22); - this.showToolStripMenuItem.Text = "Show in Taskbar"; - this.showToolStripMenuItem.Click += new System.EventHandler(this.showToolStripMenuItem_Click); - // - // toggleHotkeyToolStripMenuItem - // - this.toggleHotkeyToolStripMenuItem.Name = "toggleHotkeyToolStripMenuItem"; - this.toggleHotkeyToolStripMenuItem.Size = new System.Drawing.Size(221, 22); - this.toggleHotkeyToolStripMenuItem.Text = "Disable Print Screen capture"; - this.toggleHotkeyToolStripMenuItem.Click += new System.EventHandler(this.toggleHotkeyToolStripMenuItem_Click); + this.disablePrintScreenCaptureToolStripMenuItem.Name = "disablePrintScreenCaptureToolStripMenuItem"; + this.disablePrintScreenCaptureToolStripMenuItem.Size = new System.Drawing.Size(221, 22); + this.disablePrintScreenCaptureToolStripMenuItem.Text = "Disable Print Screen capture"; + this.disablePrintScreenCaptureToolStripMenuItem.Click += new System.EventHandler(this.toggleHotkeyToolStripMenuItem_Click); // // toolStripSeparator1 // @@ -87,23 +79,23 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(339, 186); + this.ClientSize = new System.Drawing.Size(184, 61); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "MainForm"; this.Text = "MainForm"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); + this.Load += new System.EventHandler(this.MainForm_Load); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); } #endregion - private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private System.Windows.Forms.ToolStripMenuItem toggleHotkeyToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem showToolStripMenuItem; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; + private System.Windows.Forms.ToolStripMenuItem disablePrintScreenCaptureToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; } } diff --git a/MainForm.cs b/MainForm.cs index 6866f1e..be4ac6e 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -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(); } } } \ No newline at end of file diff --git a/MainForm.resx b/MainForm.resx index 08b51c7..ab2e0b2 100644 --- a/MainForm.resx +++ b/MainForm.resx @@ -118,10 +118,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - 132, 17 + 17, 13 - 246, 17 + 131, 13 diff --git a/ResultForm.Designer.cs b/ResultForm.Designer.cs index 95a1716..7fdd396 100644 --- a/ResultForm.Designer.cs +++ b/ResultForm.Designer.cs @@ -32,23 +32,29 @@ this.btnSave = new System.Windows.Forms.Button(); this.btnCopy = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.tableLayoutPanel1.SuspendLayout(); this.SuspendLayout(); // // pictureBox1 // - this.pictureBox1.Location = new System.Drawing.Point(12, 12); + this.tableLayoutPanel1.SetColumnSpan(this.pictureBox1, 4); + this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox1.Location = new System.Drawing.Point(3, 3); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(716, 346); + this.pictureBox1.Size = new System.Drawing.Size(793, 530); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // btnSave // - this.btnSave.Location = new System.Drawing.Point(491, 364); + this.btnSave.Dock = System.Windows.Forms.DockStyle.Fill; + this.btnSave.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.btnSave.Location = new System.Drawing.Point(433, 539); this.btnSave.Name = "btnSave"; - this.btnSave.Size = new System.Drawing.Size(75, 23); + this.btnSave.Size = new System.Drawing.Size(117, 29); this.btnSave.TabIndex = 1; this.btnSave.Text = "Save"; this.btnSave.UseVisualStyleBackColor = true; @@ -56,9 +62,11 @@ // // btnCopy // - this.btnCopy.Location = new System.Drawing.Point(572, 364); + this.btnCopy.Dock = System.Windows.Forms.DockStyle.Fill; + this.btnCopy.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.btnCopy.Location = new System.Drawing.Point(556, 539); this.btnCopy.Name = "btnCopy"; - this.btnCopy.Size = new System.Drawing.Size(75, 23); + this.btnCopy.Size = new System.Drawing.Size(116, 29); this.btnCopy.TabIndex = 2; this.btnCopy.Text = "Copy"; this.btnCopy.UseVisualStyleBackColor = true; @@ -66,27 +74,47 @@ // // btnCancel // - this.btnCancel.Location = new System.Drawing.Point(653, 364); + this.btnCancel.Dock = System.Windows.Forms.DockStyle.Fill; + this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); + this.btnCancel.Location = new System.Drawing.Point(678, 539); this.btnCancel.Name = "btnCancel"; - this.btnCancel.Size = new System.Drawing.Size(75, 23); + this.btnCancel.Size = new System.Drawing.Size(118, 29); this.btnCancel.TabIndex = 3; this.btnCancel.Text = "Cancel"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // + // tableLayoutPanel1 + // + this.tableLayoutPanel1.ColumnCount = 4; + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 53.86703F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.46811F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.33243F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.19674F)); + this.tableLayoutPanel1.Controls.Add(this.btnCancel, 3, 1); + this.tableLayoutPanel1.Controls.Add(this.btnCopy, 2, 1); + this.tableLayoutPanel1.Controls.Add(this.btnSave, 1, 1); + this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 0, 0); + this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; + this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 2; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 93.95866F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 6.041336F)); + this.tableLayoutPanel1.Size = new System.Drawing.Size(799, 571); + this.tableLayoutPanel1.TabIndex = 4; + // // ResultForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(737, 396); - this.Controls.Add(this.btnCancel); - this.Controls.Add(this.btnCopy); - this.Controls.Add(this.btnSave); - this.Controls.Add(this.pictureBox1); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.ClientSize = new System.Drawing.Size(799, 571); + this.Controls.Add(this.tableLayoutPanel1); this.Name = "ResultForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ResultForm"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); } @@ -97,5 +125,6 @@ private System.Windows.Forms.Button btnSave; private System.Windows.Forms.Button btnCopy; private System.Windows.Forms.Button btnCancel; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; } } \ No newline at end of file diff --git a/ResultForm.cs b/ResultForm.cs index 54ecb86..dbbd505 100644 --- a/ResultForm.cs +++ b/ResultForm.cs @@ -4,7 +4,7 @@ using System.Windows.Forms; namespace ScreenCaptureApp { - public partial class ResultForm : Form + public partial class ResultForm : Form, IDisposable { public ResultForm(Image capturedImage) { @@ -30,4 +30,4 @@ namespace ScreenCaptureApp this.Close(); } } -} \ No newline at end of file +} diff --git a/ScreenCaptureForm.cs b/ScreenCaptureForm.cs index 36b80d6..71965c3 100644 --- a/ScreenCaptureForm.cs +++ b/ScreenCaptureForm.cs @@ -5,7 +5,7 @@ using System.Windows.Forms; namespace ScreenCaptureApp { - public sealed class ScreenCaptureForm : Form + public partial class ScreenCaptureForm : Form { private Point _startPoint; private Point _endPoint; @@ -46,7 +46,7 @@ namespace ScreenCaptureApp Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); - using (var g = Graphics.FromImage(_screenBitmap)) + using (Graphics g = Graphics.FromImage(_screenBitmap)) { g.CopyFromScreen( Screen.PrimaryScreen.Bounds.X, @@ -100,10 +100,9 @@ namespace ScreenCaptureApp { if (_isSelecting) { - var rect = GetSelectionRectangle(); + Rectangle rect = GetSelectionRectangle(); e.Graphics.DrawRectangle(_selectionPen, rect); - // Перекрестие для точного выбора e.Graphics.DrawLine(_selectionPen, new Point(rect.X, rect.Y + rect.Height / 2), new Point(rect.X + rect.Width, rect.Y + rect.Height / 2)); @@ -137,7 +136,7 @@ namespace ScreenCaptureApp { CapturedImage = new Bitmap(_selectedRegion.Width, _selectedRegion.Height); - using (var g = Graphics.FromImage(CapturedImage)) + using (Graphics g = Graphics.FromImage(CapturedImage)) { g.DrawImage(_screenBitmap, new Rectangle(0, 0, _selectedRegion.Width, _selectedRegion.Height),