From f2e412e46fa21db5e0b4a83595a073942c57382c Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 27 Feb 2023 00:46:12 +0300 Subject: [PATCH] ScrollingCaptureLightForm improvements --- .../ScrollingCaptureLightForm.Designer.cs | 8 +- .../Forms/ScrollingCaptureLightForm.cs | 117 +++++++++--------- .../Forms/ScrollingCaptureLightForm.resx | 3 - 3 files changed, 58 insertions(+), 70 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.Designer.cs b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.Designer.cs index fff040501..68758af17 100644 --- a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.Designer.cs +++ b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.Designer.cs @@ -15,11 +15,9 @@ partial class ScrollingCaptureLightForm /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); this.btnCapture = new System.Windows.Forms.Button(); this.pOutput = new System.Windows.Forms.Panel(); this.pbOutput = new System.Windows.Forms.PictureBox(); - this.tCapture = new System.Windows.Forms.Timer(this.components); this.btnOptions = new System.Windows.Forms.Button(); this.btnUpload = new System.Windows.Forms.Button(); this.pOutput.SuspendLayout(); @@ -61,10 +59,6 @@ private void InitializeComponent() this.pbOutput.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pbOutput_MouseMove); this.pbOutput.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pbOutput_MouseUp); // - // tCapture - // - this.tCapture.Tick += new System.EventHandler(this.tCapture_Tick); - // // btnOptions // this.btnOptions.Location = new System.Drawing.Point(344, 8); @@ -100,6 +94,7 @@ private void InitializeComponent() this.Name = "ScrollingCaptureLightForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ShareX - Scrolling capture"; + this.Load += new System.EventHandler(this.ScrollingCaptureLightForm_Load); this.pOutput.ResumeLayout(false); this.pOutput.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pbOutput)).EndInit(); @@ -112,7 +107,6 @@ private void InitializeComponent() private System.Windows.Forms.Button btnCapture; private System.Windows.Forms.Panel pOutput; private System.Windows.Forms.PictureBox pbOutput; - private System.Windows.Forms.Timer tCapture; private System.Windows.Forms.Button btnOptions; private System.Windows.Forms.Button btnUpload; } diff --git a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.cs b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.cs index c1c005d5f..4b3836ab8 100644 --- a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.cs +++ b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.cs @@ -43,7 +43,7 @@ public partial class ScrollingCaptureLightForm : Form public Bitmap Result { get; private set; } private List images = new List(); - private bool isCapturing, scrollTop; + private bool isCapturing, stopRequested; private int currentScrollCount, bestMatchCount, bestMatchIndex; private WindowInfo selectedWindow; private Rectangle selectedRectangle; @@ -55,18 +55,13 @@ public ScrollingCaptureLightForm(ScrollingCaptureOptions options) InitializeComponent(); ShareXResources.ApplyTheme(this); - - SelectWindow(); } protected override void Dispose(bool disposing) { if (disposing) { - if (components != null) - { - components.Dispose(); - } + components?.Dispose(); Reset(); } @@ -105,12 +100,12 @@ private void ResetPictureBox() temp?.Dispose(); } - private void StartCapture() + private async Task StartCapture() { if (!isCapturing) { isCapturing = true; - scrollTop = true; + stopRequested = false; bestMatchCount = 0; bestMatchIndex = 0; @@ -120,25 +115,59 @@ private void StartCapture() Reset(); selectedWindow.Activate(); - tCapture.Interval = Options.StartDelay; - tCapture.Start(); + await Task.Delay(Options.StartDelay); + await CaptureJob(); } } private void StopCapture() { - if (isCapturing) + stopRequested = true; + } + + private void EndCapture() + { + // TODO: Translate + btnCapture.Text = "Capture..."; + btnUpload.Enabled = Result != null; + pbOutput.Image = Result; + this.ForceActivate(); + + isCapturing = false; + } + + private async Task CaptureJob() + { + InputHelpers.SendKeyPress(VirtualKeyCode.HOME); + NativeMethods.SendMessage(selectedWindow.Handle, (int)WindowsMessages.VSCROLL, (int)ScrollBarCommands.SB_TOP, 0); + + do { - tCapture.Stop(); + await Task.Delay(Options.ScrollDelay); - // TODO: Translate - btnCapture.Text = "Capture..."; - btnUpload.Enabled = Result != null; - pbOutput.Image = Result; - this.ForceActivate(); + Screenshot screenshot = new Screenshot() + { + CaptureCursor = false + }; - isCapturing = false; + Bitmap bmp = screenshot.CaptureRectangle(selectedRectangle); + + if (bmp != null) + { + images.Add(bmp); + } + + InputHelpers.SendMouseWheel(-120 * 2); + currentScrollCount++; + + if (images.Count > 0) + { + Result = await CombineImagesAsync(Result, images[images.Count - 1]); + } } + while (!stopRequested && currentScrollCount <= Options.MaximumScrollCount && !IsScrollReachedBottom(selectedWindow.Handle)); + + EndCapture(); } private bool IsScrollReachedBottom(IntPtr handle) @@ -174,14 +203,14 @@ private bool IsLastTwoImagesSame() return result; } - private void SelectWindow() + private async Task SelectWindow() { WindowState = FormWindowState.Minimized; Thread.Sleep(250); if (RegionCaptureTasks.GetRectangleRegion(out selectedRectangle, out selectedWindow, new RegionCaptureOptions())) { - StartCapture(); + await StartCapture(); } else { @@ -297,7 +326,12 @@ protected void OnUploadRequested(Bitmap bmp) UploadRequested?.Invoke(bmp); } - private void btnCapture_Click(object sender, EventArgs e) + private async void ScrollingCaptureLightForm_Load(object sender, EventArgs e) + { + await SelectWindow(); + } + + private async void btnCapture_Click(object sender, EventArgs e) { if (isCapturing) { @@ -305,7 +339,7 @@ private void btnCapture_Click(object sender, EventArgs e) } else { - SelectWindow(); + await SelectWindow(); } } @@ -345,42 +379,5 @@ private void pbOutput_MouseUp(object sender, MouseEventArgs e) pOutput.Cursor = Cursors.Default; } } - - private async void tCapture_Tick(object sender, EventArgs e) - { - if (scrollTop) - { - scrollTop = false; - tCapture.Interval = Options.ScrollDelay; - - InputHelpers.SendKeyPress(VirtualKeyCode.HOME); - NativeMethods.SendMessage(selectedWindow.Handle, (int)WindowsMessages.VSCROLL, (int)ScrollBarCommands.SB_TOP, 0); - - return; - } - - Screenshot screenshot = new Screenshot() { CaptureCursor = false }; - Bitmap bmp = screenshot.CaptureRectangle(selectedRectangle); - - if (bmp != null) - { - images.Add(bmp); - } - - if (images.Count > 0) - { - Result = await CombineImagesAsync(Result, images[images.Count - 1]); - } - - if (currentScrollCount == Options.MaximumScrollCount || (Options.AutoDetectScrollEnd && IsScrollReachedBottom(selectedWindow.Handle))) - { - StopCapture(); - } - else - { - InputHelpers.SendMouseWheel(-120 * 2); - currentScrollCount++; - } - } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.resx b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.resx index 48d5489a8..1b011be65 100644 --- a/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.resx +++ b/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - 59