ShareX/ShareX.ScreenCaptureLib/Forms/BaseRegionForm.cs

465 lines
15 KiB
C#
Raw Normal View History

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
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 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;
using ShareX.ScreenCaptureLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.ScreenCaptureLib
2013-11-03 23:53:49 +13:00
{
public abstract class BaseRegionForm : Form
2013-11-03 23:53:49 +13:00
{
public static GraphicsPath LastRegionFillPath { get; protected set; }
public RegionCaptureOptions Config { get; set; }
2013-11-03 23:53:49 +13:00
public int FPS { get; private set; }
public Rectangle ScreenRectangle { get; private set; }
public Rectangle ScreenRectangle0Based { get; private set; }
public RegionResult Result { get; private set; }
2016-05-04 21:32:09 +12:00
public int MonitorIndex { get; set; }
2013-11-03 23:53:49 +13:00
protected Image backgroundImage;
2013-11-03 23:53:49 +13:00
protected TextureBrush darkBackgroundBrush, lightBackgroundBrush;
protected GraphicsPath regionFillPath, regionDrawPath;
protected Pen borderPen, borderDotPen, textBackgroundPenWhite, textBackgroundPenBlack, markerPen;
2015-05-08 11:31:08 +12:00
protected Brush nodeBackgroundBrush, textBackgroundBrush;
2016-05-09 07:23:47 +12:00
protected Font infoFont, infoFontMedium, infoFontBig;
protected Stopwatch timerStart, timerFPS;
2013-11-03 23:53:49 +13:00
protected int frameCount;
protected bool pause, isKeyAllowed;
protected List<DrawableObject> drawableObjects;
public BaseRegionForm()
2013-11-03 23:53:49 +13:00
{
ScreenRectangle = CaptureHelpers.GetScreenBounds();
ScreenRectangle0Based = CaptureHelpers.ScreenToClient(ScreenRectangle);
InitializeComponent();
Icon = ShareXResources.Icon;
2016-07-04 22:27:31 +12:00
Cursor = Helpers.CreateCursor(Resources.Crosshair);
2013-11-03 23:53:49 +13:00
drawableObjects = new List<DrawableObject>();
Config = new RegionCaptureOptions();
timerStart = new Stopwatch();
timerFPS = new Stopwatch();
2013-11-03 23:53:49 +13:00
borderPen = new Pen(Color.Black);
2014-07-25 17:54:24 +12:00
borderDotPen = new Pen(Color.White);
borderDotPen.DashPattern = new float[] { 5, 5 };
2013-11-03 23:53:49 +13:00
nodeBackgroundBrush = new SolidBrush(Color.White);
2015-05-08 11:31:08 +12:00
infoFont = new Font("Verdana", 9);
2016-05-09 07:23:47 +12:00
infoFontMedium = new Font("Verdana", 12);
infoFontBig = new Font("Verdana", 16, FontStyle.Bold);
2015-05-08 11:31:08 +12:00
textBackgroundBrush = new SolidBrush(Color.FromArgb(75, Color.Black));
textBackgroundPenWhite = new Pen(Color.FromArgb(50, Color.White));
textBackgroundPenBlack = new Pen(Color.FromArgb(150, Color.Black));
markerPen = new Pen(Color.FromArgb(200, Color.Red));
2013-11-03 23:53:49 +13:00
}
private void InitializeComponent()
{
components = new Container();
2013-11-03 23:53:49 +13:00
SuspendLayout();
AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
StartPosition = FormStartPosition.Manual;
FormBorderStyle = FormBorderStyle.None;
2015-09-11 12:45:40 +12:00
Bounds = ScreenRectangle;
2013-11-03 23:53:49 +13:00
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Text = "ShareX - " + Resources.BaseRegionForm_InitializeComponent_Region_capture;
2013-11-03 23:53:49 +13:00
ShowInTaskbar = false;
2016-05-06 05:03:57 +12:00
#if !DEBUG
2013-11-03 23:53:49 +13:00
TopMost = true;
2016-05-06 05:03:57 +12:00
#endif
Shown += BaseRegionForm_Shown;
KeyUp += BaseRegionForm_KeyUp;
2013-11-03 23:53:49 +13:00
ResumeLayout(false);
}
2016-07-22 02:23:45 +12:00
public void Prepare()
{
Prepare(new Screenshot());
}
2013-11-03 23:53:49 +13:00
/// <summary>Must be called before show form</summary>
2016-07-22 02:23:45 +12:00
public virtual void Prepare(Screenshot screenshot)
2013-11-03 23:53:49 +13:00
{
2016-07-22 02:23:45 +12:00
backgroundImage = screenshot.CaptureFullscreen();
2013-11-03 23:53:49 +13:00
if (Config.UseDimming)
2013-11-03 23:53:49 +13:00
{
using (Bitmap darkBackground = (Bitmap)backgroundImage.Clone())
using (Graphics g = Graphics.FromImage(darkBackground))
{
using (Brush brush = new SolidBrush(Color.FromArgb(30, Color.Black)))
{
g.FillRectangle(brush, 0, 0, darkBackground.Width, darkBackground.Height);
}
darkBackgroundBrush = new TextureBrush(darkBackground) { WrapMode = WrapMode.Clamp };
lightBackgroundBrush = new TextureBrush(backgroundImage) { WrapMode = WrapMode.Clamp };
}
2013-11-03 23:53:49 +13:00
}
else
2013-11-03 23:53:49 +13:00
{
darkBackgroundBrush = new TextureBrush(backgroundImage) { WrapMode = WrapMode.Clamp };
2013-11-03 23:53:49 +13:00
}
}
private void BaseRegionForm_Shown(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
this.ForceActivate();
2013-11-03 23:53:49 +13:00
}
private void BaseRegionForm_KeyUp(object sender, KeyEventArgs e)
2013-11-03 23:53:49 +13:00
{
if (!isKeyAllowed && timerStart.ElapsedMilliseconds < 1000)
{
return;
}
isKeyAllowed = true;
if (e.KeyData >= Keys.D0 && e.KeyData <= Keys.D9)
{
MonitorKey(e.KeyData - Keys.D0);
return;
}
switch (e.KeyData)
2013-11-03 23:53:49 +13:00
{
case Keys.Escape:
Close(RegionResult.Close);
2013-11-03 23:53:49 +13:00
break;
case Keys.Space:
Close(RegionResult.Fullscreen);
break;
2013-11-03 23:53:49 +13:00
case Keys.Enter:
Close(RegionResult.Region);
2013-11-03 23:53:49 +13:00
break;
case Keys.Oemtilde:
Close(RegionResult.ActiveMonitor);
break;
2013-11-03 23:53:49 +13:00
}
}
private void MonitorKey(int index)
{
if (index == 0)
{
index = 10;
}
index--;
MonitorIndex = index;
Close(RegionResult.Monitor);
}
public void Pause()
{
pause = true;
}
public void Resume()
{
pause = false;
Invalidate();
}
2013-11-03 23:53:49 +13:00
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
protected override void OnPaint(PaintEventArgs e)
{
Update();
Graphics g = e.Graphics;
g.CompositingMode = CompositingMode.SourceCopy;
2013-11-03 23:53:49 +13:00
g.FillRectangle(darkBackgroundBrush, ScreenRectangle0Based);
g.CompositingMode = CompositingMode.SourceOver;
2014-07-25 17:54:24 +12:00
2013-11-03 23:53:49 +13:00
Draw(g);
if (Config.ShowFPS)
{
CheckFPS();
2016-05-09 07:23:47 +12:00
DrawFPS(g);
2013-11-03 23:53:49 +13:00
}
if (!pause)
{
Invalidate();
}
2013-11-03 23:53:49 +13:00
}
2016-05-03 23:54:02 +12:00
public virtual Image GetResultImage()
2013-11-03 23:53:49 +13:00
{
if (Result == RegionResult.Region)
{
using (Image img = GetOutputImage())
{
return RegionCaptureHelpers.ApplyRegionPathToImage(img, regionFillPath);
}
}
else if (Result == RegionResult.Fullscreen)
{
return GetOutputImage();
}
else if (Result == RegionResult.Monitor)
{
Screen[] screens = Screen.AllScreens;
if (MonitorIndex < screens.Length)
{
Screen screen = screens[MonitorIndex];
Rectangle screenRect = CaptureHelpers.ScreenToClient(screen.Bounds);
using (Image img = GetOutputImage())
{
return ImageHelpers.CropImage(img, screenRect);
}
}
}
else if (Result == RegionResult.ActiveMonitor)
{
Rectangle activeScreenRect = CaptureHelpers.GetActiveScreenBounds0Based();
using (Image img = GetOutputImage())
{
return ImageHelpers.CropImage(img, activeScreenRect);
}
}
return null;
}
protected virtual Image GetOutputImage()
{
return (Image)backgroundImage.Clone();
2013-11-03 23:53:49 +13:00
}
public virtual WindowInfo GetWindowInfo()
{
return null;
}
public void Close(RegionResult result)
2013-11-03 23:53:49 +13:00
{
Result = result;
Close();
}
protected new virtual void Update()
{
if (!timerStart.IsRunning)
{
timerStart.Start();
timerFPS.Start();
}
2013-11-03 23:53:49 +13:00
InputManager.Update();
DrawableObject[] objects = drawableObjects.OrderByDescending(x => x.Order).ToArray();
2013-11-03 23:53:49 +13:00
2016-05-06 10:52:16 +12:00
if (objects.All(x => !x.IsDragging))
2013-11-03 23:53:49 +13:00
{
for (int i = 0; i < objects.Count(); i++)
{
DrawableObject obj = objects[i];
2016-05-06 10:52:16 +12:00
if (obj.Visible)
2013-11-03 23:53:49 +13:00
{
2016-05-25 05:31:40 +12:00
obj.IsCursorHover = obj.Rectangle.Contains(InputManager.MousePosition0Based);
2016-05-06 10:52:16 +12:00
2016-05-25 05:31:40 +12:00
if (obj.IsCursorHover)
2013-11-03 23:53:49 +13:00
{
2016-05-06 10:52:16 +12:00
if (InputManager.IsMousePressed(MouseButtons.Left))
{
obj.IsDragging = true;
}
2013-11-03 23:53:49 +13:00
2016-05-06 10:52:16 +12:00
for (int y = i + 1; y < objects.Count(); y++)
{
2016-05-25 05:31:40 +12:00
objects[y].IsCursorHover = false;
2016-05-06 10:52:16 +12:00
}
2013-11-03 23:53:49 +13:00
2016-05-06 10:52:16 +12:00
break;
}
2013-11-03 23:53:49 +13:00
}
}
}
else
{
if (InputManager.IsMouseReleased(MouseButtons.Left))
{
foreach (DrawableObject obj in objects)
{
obj.IsDragging = false;
}
}
}
2015-11-29 02:58:58 +13:00
borderDotPen.DashOffset = (float)timerStart.Elapsed.TotalSeconds * -15;
2013-11-03 23:53:49 +13:00
}
protected virtual void Draw(Graphics g)
{
DrawObjects(g);
}
protected void DrawObjects(Graphics g)
{
foreach (DrawableObject drawObject in drawableObjects)
2013-11-03 23:53:49 +13:00
{
if (drawObject.Visible)
{
drawObject.Draw(g);
}
}
}
private void CheckFPS()
{
frameCount++;
if (timerFPS.ElapsedMilliseconds >= 1000)
2013-11-03 23:53:49 +13:00
{
FPS = (int)(frameCount / timerFPS.Elapsed.TotalSeconds);
2013-11-03 23:53:49 +13:00
frameCount = 0;
timerFPS.Reset();
timerFPS.Start();
2013-11-03 23:53:49 +13:00
}
}
2016-05-09 07:23:47 +12:00
private void DrawFPS(Graphics g)
2013-11-03 23:53:49 +13:00
{
string text = "FPS: " + FPS;
2016-05-09 07:23:47 +12:00
SizeF textSize = g.MeasureString(text, infoFontBig);
2013-11-03 23:53:49 +13:00
2016-05-09 07:23:47 +12:00
int offset = 10;
2013-11-03 23:53:49 +13:00
Rectangle primaryScreenBounds = CaptureHelpers.GetPrimaryScreenBounds0Based();
Rectangle textRectangle = new Rectangle(primaryScreenBounds.X + offset, primaryScreenBounds.Y + offset, (int)textSize.Width, (int)textSize.Height);
2013-11-03 23:53:49 +13:00
if (textRectangle.Offset(10).Contains(InputManager.MousePosition0Based))
2013-11-03 23:53:49 +13:00
{
textRectangle.Y = primaryScreenBounds.Height - textRectangle.Height - offset;
2013-11-03 23:53:49 +13:00
}
2016-05-09 07:23:47 +12:00
ImageHelpers.DrawTextWithOutline(g, text, textRectangle.Location, infoFontBig, Color.White, Color.Black);
2013-11-03 23:53:49 +13:00
}
protected Rectangle CalculateAreaFromNodes()
{
IEnumerable<NodeObject> nodes = drawableObjects.OfType<NodeObject>().Where(x => x.Visible);
2013-11-03 23:53:49 +13:00
if (nodes.Count() > 1)
{
int left = (int)nodes.Min(x => x.Position.X);
int top = (int)nodes.Min(x => x.Position.Y);
int right = (int)nodes.Max(x => x.Position.X);
int bottom = (int)nodes.Max(x => x.Position.Y);
return CaptureHelpers.CreateRectangle(new Point(left, top), new Point(right, bottom));
}
return Rectangle.Empty;
}
internal NodeObject MakeNode()
2013-11-03 23:53:49 +13:00
{
NodeObject node = new NodeObject();
drawableObjects.Add(node);
2013-11-03 23:53:49 +13:00
return node;
}
protected void ShowNodes()
{
foreach (NodeObject node in drawableObjects.OfType<NodeObject>())
2013-11-03 23:53:49 +13:00
{
node.Visible = true;
}
}
protected void HideNodes()
{
foreach (NodeObject node in drawableObjects.OfType<NodeObject>())
2013-11-03 23:53:49 +13:00
{
node.Visible = false;
}
}
public IContainer components = null;
2013-11-03 23:53:49 +13:00
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (darkBackgroundBrush != null) darkBackgroundBrush.Dispose();
if (lightBackgroundBrush != null) lightBackgroundBrush.Dispose();
if (borderPen != null) borderPen.Dispose();
if (borderDotPen != null) borderDotPen.Dispose();
if (nodeBackgroundBrush != null) nodeBackgroundBrush.Dispose();
2015-05-08 11:31:08 +12:00
if (infoFont != null) infoFont.Dispose();
2016-05-09 07:23:47 +12:00
if (infoFontMedium != null) infoFontMedium.Dispose();
if (infoFontBig != null) infoFontBig.Dispose();
2015-05-08 11:31:08 +12:00
if (textBackgroundBrush != null) textBackgroundBrush.Dispose();
if (textBackgroundPenWhite != null) textBackgroundPenWhite.Dispose();
if (textBackgroundPenBlack != null) textBackgroundPenBlack.Dispose();
if (markerPen != null) markerPen.Dispose();
2013-11-03 23:53:49 +13:00
if (regionFillPath != null)
{
if (LastRegionFillPath != null) LastRegionFillPath.Dispose();
LastRegionFillPath = regionFillPath;
}
else
{
if (regionFillPath != null) regionFillPath.Dispose();
if (regionDrawPath != null) regionDrawPath.Dispose();
}
if (backgroundImage != null) backgroundImage.Dispose();
2013-11-03 23:53:49 +13:00
base.Dispose(disposing);
}
}
}