ShareX/ShareX.ScreenCaptureLib/Forms/ScrollingCaptureLightForm.cs

171 lines
4.9 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 ScrollingCaptureLightForm : Form
{
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 ScrollingCaptureLightForm(ScrollingCaptureOptions options)
{
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-02 12:42:36 +13:00
if (!manager.IsCapturing)
2023-02-26 04:42:21 +13:00
{
WindowState = FormWindowState.Minimized;
2023-03-02 12:42:36 +13:00
btnCapture.Enabled = false;
btnUpload.Enabled = false;
ResetPictureBox();
2023-02-26 04:42:21 +13:00
2023-03-02 12:42:36 +13:00
await manager.StartCapture();
2023-02-26 04:42:21 +13:00
2023-03-02 12:42:36 +13:00
btnCapture.Enabled = true;
btnUpload.Enabled = manager.Result != null;
pbOutput.Image = manager.Result;
2023-02-26 04:42:21 +13:00
2023-03-02 12:42:36 +13:00
this.ForceActivate();
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);
}
2023-02-27 10:46:12 +13:00
private async void ScrollingCaptureLightForm_Load(object sender, EventArgs e)
{
await SelectWindow();
}
2023-03-02 09:01:00 +13:00
private void ScrollingCaptureLightForm_Activated(object sender, EventArgs e)
{
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-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
}
}