ShareX/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureForm.cs

194 lines
5.6 KiB
C#
Raw Normal View History

2023-02-26 04:42:21 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2023 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Drawing;
using System.Threading;
2023-02-26 06:00:08 +13:00
using System.Threading.Tasks;
2023-02-26 04:42:21 +13:00
using System.Windows.Forms;
namespace ShareX.ScreenCaptureLib
{
public partial class ScrollingCaptureForm : Form
2023-02-26 04:42:21 +13:00
{
2023-02-26 09:18:39 +13:00
public event Action<Bitmap> UploadRequested;
2023-02-26 04:42:21 +13:00
public ScrollingCaptureOptions Options { get; private set; }
2023-03-02 12:42:36 +13:00
private ScrollingCaptureManager manager;
2023-02-27 07:43:34 +13:00
private Point dragStartPosition;
2023-02-26 04:42:21 +13:00
public ScrollingCaptureForm(ScrollingCaptureOptions options)
2023-02-26 04:42:21 +13:00
{
Options = options;
InitializeComponent();
ShareXResources.ApplyTheme(this);
2023-03-02 12:42:36 +13:00
manager = new ScrollingCaptureManager(Options);
2023-02-26 04:42:21 +13:00
}
2023-02-27 07:43:34 +13:00
protected override void Dispose(bool disposing)
{
if (disposing)
{
2023-02-27 10:46:12 +13:00
components?.Dispose();
2023-03-02 12:42:36 +13:00
manager?.Dispose();
2023-02-27 07:43:34 +13:00
}
base.Dispose(disposing);
}
2023-02-26 04:42:21 +13:00
private void ResetPictureBox()
{
Image temp = pbOutput.Image;
pbOutput.Image = null;
temp?.Dispose();
}
2023-02-27 10:46:12 +13:00
private async Task StartCapture()
2023-02-26 04:42:21 +13:00
{
2023-03-03 09:07:50 +13:00
WindowState = FormWindowState.Minimized;
btnCapture.Enabled = false;
btnUpload.Enabled = false;
2023-03-05 09:41:48 +13:00
btnOptions.Enabled = false;
2023-03-03 10:02:29 +13:00
lblResultSize.Text = "";
2023-03-03 09:07:50 +13:00
ResetPictureBox();
2023-02-26 04:42:21 +13:00
2023-03-03 09:07:50 +13:00
try
{
2023-03-02 12:42:36 +13:00
await manager.StartCapture();
2023-03-03 09:07:50 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
2023-02-26 04:42:21 +13:00
2023-03-03 09:07:50 +13:00
e.ShowError();
2023-02-26 04:42:21 +13:00
}
2023-03-03 09:07:50 +13:00
btnCapture.Enabled = true;
2023-03-05 09:41:48 +13:00
btnOptions.Enabled = true;
2023-03-03 10:02:29 +13:00
if (manager.Result != null)
{
btnUpload.Enabled = true;
pbOutput.Image = manager.Result;
2023-03-07 10:14:02 +13:00
pOutput.AutoScrollPosition = new Point(0, 0);
2023-03-03 10:02:29 +13:00
lblResultSize.Text = $"{manager.Result.Width}x{manager.Result.Height}";
}
2023-03-03 09:07:50 +13:00
this.ForceActivate();
2023-03-08 16:47:44 +13:00
if (Options.AutoUpload)
{
UploadResult();
}
2023-02-26 04:42:21 +13:00
}
2023-02-27 10:46:12 +13:00
private async Task SelectWindow()
2023-02-26 04:42:21 +13:00
{
WindowState = FormWindowState.Minimized;
Thread.Sleep(250);
2023-03-02 12:42:36 +13:00
if (manager.SelectWindow())
2023-02-26 04:42:21 +13:00
{
2023-02-27 10:46:12 +13:00
await StartCapture();
2023-02-26 04:42:21 +13:00
}
else
{
this.ForceActivate();
}
}
2023-02-26 09:18:39 +13:00
private void UploadResult()
{
2023-03-02 12:42:36 +13:00
if (manager.Result != null)
2023-02-26 09:18:39 +13:00
{
2023-03-02 12:42:36 +13:00
OnUploadRequested((Bitmap)manager.Result.Clone());
2023-02-26 09:18:39 +13:00
}
}
protected void OnUploadRequested(Bitmap bmp)
{
UploadRequested?.Invoke(bmp);
}
private async void ScrollingCaptureForm_Load(object sender, EventArgs e)
2023-02-27 10:46:12 +13:00
{
await SelectWindow();
}
private void ScrollingCaptureForm_Activated(object sender, EventArgs e)
2023-03-02 09:01:00 +13:00
{
2023-03-02 12:42:36 +13:00
manager.StopCapture();
2023-03-02 09:01:00 +13:00
}
2023-02-27 10:46:12 +13:00
private async void btnCapture_Click(object sender, EventArgs e)
2023-02-26 04:42:21 +13:00
{
2023-03-02 09:01:00 +13:00
await SelectWindow();
2023-02-26 04:42:21 +13:00
}
2023-02-26 09:18:39 +13:00
private void btnUpload_Click(object sender, EventArgs e)
{
UploadResult();
}
2023-02-26 04:42:21 +13:00
private void btnOptions_Click(object sender, EventArgs e)
{
2023-03-05 09:41:48 +13:00
using (ScrollingCaptureOptionsForm scrollingCaptureOptionsForm = new ScrollingCaptureOptionsForm(Options))
{
scrollingCaptureOptionsForm.ShowDialog();
}
2023-02-26 04:42:21 +13:00
}
2023-02-27 07:43:34 +13:00
private void pbOutput_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && (pOutput.HorizontalScroll.Visible || pOutput.VerticalScroll.Visible))
{
pOutput.Cursor = Cursors.SizeAll;
dragStartPosition = e.Location;
}
}
private void pbOutput_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && (pOutput.HorizontalScroll.Visible || pOutput.VerticalScroll.Visible))
{
Point scrollOffset = new Point(e.X - dragStartPosition.X, e.Y - dragStartPosition.Y);
pOutput.AutoScrollPosition = new Point(-pOutput.AutoScrollPosition.X - scrollOffset.X, -pOutput.AutoScrollPosition.Y - scrollOffset.Y);
pOutput.Update();
}
}
private void pbOutput_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pOutput.Cursor = Cursors.Default;
}
}
2023-02-26 04:42:21 +13:00
}
}