2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2021-04-30 15:20:19 +12:00
|
|
|
|
using ShareX.ScreenCaptureLib.Properties;
|
2015-01-20 02:38:53 +13:00
|
|
|
|
using System;
|
2015-09-08 00:29:27 +12:00
|
|
|
|
using System.Diagnostics;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2021-04-30 15:20:19 +12:00
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-02-26 09:55:54 +13:00
|
|
|
|
public partial class ScreenRecordForm : Form
|
2015-09-08 00:29:27 +12:00
|
|
|
|
{
|
|
|
|
|
public event Action StopRequested;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2017-06-26 18:05:14 +12:00
|
|
|
|
public bool IsWorking { get; private set; }
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public bool IsRecording { get; private set; }
|
|
|
|
|
public bool IsCountdown { get; set; }
|
|
|
|
|
public TimeSpan Countdown { get; set; }
|
|
|
|
|
public Stopwatch Timer { get; private set; }
|
|
|
|
|
public ManualResetEvent RecordResetEvent { get; set; }
|
2017-11-01 19:29:36 +13:00
|
|
|
|
public bool IsStopRequested { get; private set; }
|
|
|
|
|
public bool IsAbortRequested { get; private set; }
|
2015-09-08 00:29:27 +12:00
|
|
|
|
|
2021-04-30 15:20:19 +12:00
|
|
|
|
public bool ActivateWindow { get; set; } = true;
|
|
|
|
|
public float Duration { get; set; } = 0;
|
|
|
|
|
public bool AskConfirmationOnAbort { get; set; } = false;
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
private Color borderColor = Color.Red;
|
|
|
|
|
private Rectangle borderRectangle;
|
|
|
|
|
private Rectangle borderRectangle0Based;
|
2020-10-01 12:39:49 +13:00
|
|
|
|
private static int lastIconStatus = -1;
|
2015-09-08 00:29:27 +12:00
|
|
|
|
|
2021-04-30 15:20:19 +12:00
|
|
|
|
public ScreenRecordForm(Rectangle regionRectangle)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
InitializeComponent();
|
2016-02-26 09:55:54 +13:00
|
|
|
|
Icon = ShareXResources.Icon;
|
2016-01-20 06:33:13 +13:00
|
|
|
|
niTray.Icon = ShareXResources.Icon;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
borderRectangle = regionRectangle.Offset(1);
|
|
|
|
|
borderRectangle0Based = new Rectangle(0, 0, borderRectangle.Width, borderRectangle.Height);
|
2015-08-14 09:51:58 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
Location = borderRectangle.Location;
|
|
|
|
|
int windowWidth = Math.Max(borderRectangle.Width, pInfo.Width);
|
|
|
|
|
Size = new Size(windowWidth, borderRectangle.Height + pInfo.Height + 1);
|
|
|
|
|
pInfo.Location = new Point(0, borderRectangle.Height + 1);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
Region region = new Region(ClientRectangle);
|
|
|
|
|
region.Exclude(borderRectangle0Based.Offset(-1));
|
|
|
|
|
region.Exclude(new Rectangle(0, borderRectangle.Height, windowWidth, 1));
|
|
|
|
|
if (borderRectangle.Width < pInfo.Width)
|
|
|
|
|
{
|
|
|
|
|
region.Exclude(new Rectangle(borderRectangle.Width, 0, pInfo.Width - borderRectangle.Width, borderRectangle.Height));
|
|
|
|
|
}
|
|
|
|
|
else if (borderRectangle.Width > pInfo.Width)
|
|
|
|
|
{
|
|
|
|
|
region.Exclude(new Rectangle(pInfo.Width, borderRectangle.Height + 1, borderRectangle.Width - pInfo.Width, pInfo.Height));
|
|
|
|
|
}
|
|
|
|
|
Region = region;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
Timer = new Stopwatch();
|
2015-09-08 02:14:44 +12:00
|
|
|
|
UpdateTimer();
|
|
|
|
|
|
|
|
|
|
RecordResetEvent = new ManualResetEvent(false);
|
|
|
|
|
|
|
|
|
|
ChangeState(ScreenRecordState.Waiting);
|
2015-08-26 09:38:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
protected override bool ShowWithoutActivation
|
2015-08-26 09:38:28 +12:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
get
|
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
return !ActivateWindow;
|
2015-09-08 00:29:27 +12:00
|
|
|
|
}
|
2015-08-26 09:38:28 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
protected override CreateParams CreateParams
|
2014-11-14 08:59:34 +13:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
get
|
2014-11-14 08:59:34 +13:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
CreateParams createParams = base.CreateParams;
|
2017-12-20 07:36:04 +13:00
|
|
|
|
createParams.ExStyle |= (int)(WindowStyles.WS_EX_TOPMOST | WindowStyles.WS_EX_TOOLWINDOW);
|
2015-09-08 00:29:27 +12:00
|
|
|
|
return createParams;
|
2014-11-14 08:59:34 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 01:29:37 +12:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
if (components != null)
|
|
|
|
|
{
|
|
|
|
|
components.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RecordResetEvent != null)
|
|
|
|
|
{
|
|
|
|
|
RecordResetEvent.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
private void ScreenRegionForm_Shown(object sender, EventArgs e)
|
2015-08-26 09:38:28 +12:00
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
if (ActivateWindow)
|
2015-08-26 09:38:28 +12:00
|
|
|
|
{
|
2016-03-10 05:37:54 +13:00
|
|
|
|
this.ForceActivate();
|
2015-08-26 09:38:28 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 19:29:36 +13:00
|
|
|
|
private void ScreenRecordForm_FormClosed(object sender, FormClosedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!IsStopRequested)
|
|
|
|
|
{
|
|
|
|
|
AbortRecording();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
protected void OnStopRequested()
|
2014-11-14 08:59:34 +13:00
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
StopRequested?.Invoke();
|
2014-11-14 08:59:34 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public void StartCountdown(int milliseconds)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
IsCountdown = true;
|
|
|
|
|
Countdown = TimeSpan.FromMilliseconds(milliseconds);
|
2015-06-04 21:07:43 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
lblTimer.ForeColor = Color.Yellow;
|
2015-06-04 21:07:43 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
Timer.Start();
|
|
|
|
|
timerRefresh.Start();
|
|
|
|
|
UpdateTimer();
|
|
|
|
|
}
|
2015-06-04 21:07:43 +12:00
|
|
|
|
|
2015-09-08 01:29:37 +12:00
|
|
|
|
public void StartRecordingTimer()
|
2015-09-08 00:29:27 +12:00
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
IsCountdown = Duration > 0;
|
|
|
|
|
Countdown = TimeSpan.FromSeconds(Duration);
|
2015-09-08 00:29:27 +12:00
|
|
|
|
|
|
|
|
|
lblTimer.ForeColor = Color.White;
|
|
|
|
|
borderColor = Color.FromArgb(0, 255, 0);
|
|
|
|
|
Refresh();
|
|
|
|
|
|
|
|
|
|
Timer.Reset();
|
|
|
|
|
Timer.Start();
|
|
|
|
|
timerRefresh.Start();
|
|
|
|
|
UpdateTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateTimer()
|
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
2014-04-20 03:39:17 +12:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
TimeSpan timer;
|
|
|
|
|
|
|
|
|
|
if (IsCountdown)
|
2014-04-20 03:39:17 +12:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
timer = Countdown - Timer.Elapsed;
|
|
|
|
|
if (timer.Ticks < 0) timer = TimeSpan.Zero;
|
2014-04-20 03:39:17 +12:00
|
|
|
|
}
|
2015-09-08 00:29:27 +12:00
|
|
|
|
else
|
2014-04-20 03:39:17 +12:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
timer = Timer.Elapsed;
|
2014-04-20 03:39:17 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
lblTimer.Text = timer.ToString("mm\\:ss\\:ff");
|
2015-06-04 21:07:43 +12:00
|
|
|
|
}
|
2015-09-08 00:29:27 +12:00
|
|
|
|
}
|
2015-06-04 21:07:43 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (Pen pen1 = new Pen(Color.Black) { DashPattern = new float[] { 5, 5 } })
|
|
|
|
|
using (Pen pen2 = new Pen(borderColor) { DashPattern = new float[] { 5, 5 }, DashOffset = 5 })
|
2014-05-10 12:23:47 +12:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
e.Graphics.DrawRectangleProper(pen1, borderRectangle0Based);
|
|
|
|
|
e.Graphics.DrawRectangleProper(pen2, borderRectangle0Based);
|
2014-05-10 12:23:47 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
base.OnPaint(e);
|
|
|
|
|
}
|
2014-12-11 08:33:47 +13:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
private void timerRefresh_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdateTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnStart_MouseClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
2014-12-11 08:33:47 +13:00
|
|
|
|
{
|
2015-09-08 00:29:27 +12:00
|
|
|
|
StartStopRecording();
|
2014-12-11 08:33:47 +13:00
|
|
|
|
}
|
2015-09-08 00:29:27 +12:00
|
|
|
|
}
|
2014-08-09 04:13:00 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
private void btnAbort_MouseClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
if (!AskConfirmationOnAbort || MessageBox.Show(Resources.ScreenRecordForm_ConfirmCancel, "ShareX", MessageBoxButtons.YesNo,
|
|
|
|
|
MessageBoxIcon.Warning) == DialogResult.Yes)
|
2018-04-08 16:31:46 +12:00
|
|
|
|
{
|
|
|
|
|
AbortRecording();
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2015-09-08 00:29:27 +12:00
|
|
|
|
}
|
2015-09-08 01:29:37 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public void StartStopRecording()
|
|
|
|
|
{
|
2017-06-26 18:05:14 +12:00
|
|
|
|
if (IsWorking)
|
2015-09-08 00:29:27 +12:00
|
|
|
|
{
|
2017-11-01 19:29:36 +13:00
|
|
|
|
IsStopRequested = true;
|
|
|
|
|
|
2017-06-26 18:05:14 +12:00
|
|
|
|
if (!IsRecording)
|
|
|
|
|
{
|
2017-11-01 19:29:36 +13:00
|
|
|
|
IsAbortRequested = true;
|
2017-06-26 18:05:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
OnStopRequested();
|
|
|
|
|
}
|
|
|
|
|
else if (RecordResetEvent != null)
|
|
|
|
|
{
|
|
|
|
|
RecordResetEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public void AbortRecording()
|
|
|
|
|
{
|
2017-11-01 19:29:36 +13:00
|
|
|
|
IsAbortRequested = true;
|
2015-09-08 00:29:27 +12:00
|
|
|
|
StartStopRecording();
|
|
|
|
|
}
|
2014-05-22 13:22:32 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public void ChangeState(ScreenRecordState state)
|
|
|
|
|
{
|
2015-09-08 01:29:37 +12:00
|
|
|
|
this.InvokeSafe(() =>
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-09-08 01:29:37 +12:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ScreenRecordState.Waiting:
|
|
|
|
|
string trayTextWaiting = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Waiting___;
|
|
|
|
|
niTray.Text = trayTextWaiting.Truncate(63);
|
|
|
|
|
niTray.Icon = Resources.control_record_yellow.ToIcon();
|
|
|
|
|
cmsMain.Enabled = false;
|
|
|
|
|
niTray.Visible = true;
|
|
|
|
|
break;
|
|
|
|
|
case ScreenRecordState.BeforeStart:
|
|
|
|
|
string trayTextBeforeStart = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_start_recording_;
|
|
|
|
|
niTray.Text = trayTextBeforeStart.Truncate(63);
|
2021-04-30 15:20:19 +12:00
|
|
|
|
tsmiStart.Text = Resources.ScreenRecordForm_Start;
|
2015-09-08 00:29:27 +12:00
|
|
|
|
cmsMain.Enabled = true;
|
2015-09-08 01:29:37 +12:00
|
|
|
|
break;
|
|
|
|
|
case ScreenRecordState.AfterStart:
|
2017-06-26 18:05:14 +12:00
|
|
|
|
IsWorking = true;
|
2015-09-08 01:29:37 +12:00
|
|
|
|
string trayTextAfterStart = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_;
|
|
|
|
|
niTray.Text = trayTextAfterStart.Truncate(63);
|
|
|
|
|
niTray.Icon = Resources.control_record.ToIcon();
|
2021-04-30 15:20:19 +12:00
|
|
|
|
tsmiStart.Text = Resources.ScreenRecordForm_Stop;
|
|
|
|
|
btnStart.Text = Resources.ScreenRecordForm_Stop;
|
2015-09-15 19:00:38 +12:00
|
|
|
|
break;
|
|
|
|
|
case ScreenRecordState.AfterRecordingStart:
|
2017-06-26 18:05:14 +12:00
|
|
|
|
IsRecording = true;
|
2015-09-08 01:29:37 +12:00
|
|
|
|
StartRecordingTimer();
|
|
|
|
|
break;
|
2018-11-01 23:49:27 +13:00
|
|
|
|
case ScreenRecordState.Encoding:
|
2015-09-08 01:29:37 +12:00
|
|
|
|
Hide();
|
|
|
|
|
string trayTextAfterStop = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Encoding___;
|
|
|
|
|
niTray.Text = trayTextAfterStop.Truncate(63);
|
2021-04-30 15:20:19 +12:00
|
|
|
|
niTray.Icon = Resources.camcorder__pencil.ToIcon();
|
2015-09-08 00:29:27 +12:00
|
|
|
|
cmsMain.Enabled = false;
|
2015-09-08 01:29:37 +12:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2015-08-26 09:38:28 +12:00
|
|
|
|
|
2015-09-08 00:29:27 +12:00
|
|
|
|
public void ChangeStateProgress(int progress)
|
2015-08-26 09:38:28 +12:00
|
|
|
|
{
|
2019-11-28 05:10:49 +13:00
|
|
|
|
niTray.Text = $"ShareX - {Resources.ScreenRecordForm_StartRecording_Encoding___} {progress}%";
|
2020-10-01 12:39:49 +13:00
|
|
|
|
|
|
|
|
|
if (niTray.Visible && lastIconStatus != progress)
|
|
|
|
|
{
|
|
|
|
|
Icon icon;
|
|
|
|
|
|
|
|
|
|
if (progress >= 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
icon = Helpers.GetProgressIcon(progress, Color.FromArgb(140, 0, 36));
|
2020-10-01 12:39:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
progress = -1;
|
|
|
|
|
if (lastIconStatus == progress) return;
|
2021-04-30 15:20:19 +12:00
|
|
|
|
icon = Resources.camcorder__pencil.ToIcon();
|
2020-10-01 12:39:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-30 15:20:19 +12:00
|
|
|
|
icon = Resources.camcorder__pencil.ToIcon();
|
2020-10-01 12:39:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Icon oldIcon = niTray.Icon)
|
|
|
|
|
{
|
|
|
|
|
niTray.Icon = icon;
|
|
|
|
|
oldIcon.DisposeHandle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastIconStatus = progress;
|
|
|
|
|
}
|
2015-08-26 09:38:28 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|