ScrollingCaptureLightForm improvements

This commit is contained in:
Jaex 2023-02-27 00:46:12 +03:00
parent a1f881032b
commit f2e412e46f
3 changed files with 58 additions and 70 deletions

View file

@ -15,11 +15,9 @@ partial class ScrollingCaptureLightForm
/// </summary>
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;
}

View file

@ -43,7 +43,7 @@ public partial class ScrollingCaptureLightForm : Form
public Bitmap Result { get; private set; }
private List<Bitmap> images = new List<Bitmap>();
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++;
}
}
}
}

View file

@ -117,9 +117,6 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="tCapture.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>59</value>
</metadata>