mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 21:13:23 -07:00
Changed the zooming to be in the Capture form itself, as it was really hard to change the shape. Now it's round...
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2312 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
46e9ea0e59
commit
675cdb1aae
3 changed files with 110 additions and 242 deletions
|
@ -1,195 +0,0 @@
|
|||
/*
|
||||
* Greenshot - a free and open source screenshot tool
|
||||
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
|
||||
*
|
||||
* For more information see: http://getgreenshot.org/
|
||||
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
|
||||
*
|
||||
* 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 1 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
using Greenshot.Plugin;
|
||||
using System.Drawing.Drawing2D;
|
||||
using GreenshotPlugin.Controls;
|
||||
using GreenshotPlugin.UnmanagedHelpers;
|
||||
|
||||
namespace Greenshot.Forms {
|
||||
/// <summary>
|
||||
/// This control will show the area around the mouse of the current capture
|
||||
/// </summary>
|
||||
public class ZoomControl : Control {
|
||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ZoomControl));
|
||||
private Image imageToZoom = null;
|
||||
private Point zoomLocation = Point.Empty;
|
||||
private const int distanceX = 20;
|
||||
private const int distanceY = 20;
|
||||
|
||||
/// <summary>
|
||||
/// Create a zoom control for the image and with the size
|
||||
/// </summary>
|
||||
/// <param name="imageToZoom"></param>
|
||||
/// <param name="zoomSize"></param>
|
||||
public ZoomControl(Image imageToZoom, Size zoomSize) {
|
||||
InitializeComponent();
|
||||
this.Width = zoomSize.Width;
|
||||
this.Height = zoomSize.Height;
|
||||
this.imageToZoom = imageToZoom;
|
||||
Zoom = 400;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override some special form parameters
|
||||
/// </summary>
|
||||
protected override CreateParams CreateParams {
|
||||
get {
|
||||
var parms = base.CreateParams;
|
||||
parms.Style &= ~((int)WindowStyleFlags.WS_CLIPCHILDREN); // Turn off WS_CLIPCHILDREN which prevents the screen to be cleared behind this form
|
||||
parms.ExStyle |= (int)ExtendedWindowStyleFlags.WS_EX_NOACTIVATE | (int)ExtendedWindowStyleFlags.WS_EX_TOOLWINDOW | (int)ExtendedWindowStyleFlags.WS_EX_TOPMOST;
|
||||
//parms.ExStyle |= (int)ExtendedWindowStyleFlags.WS_EX_TRANSPARENT;
|
||||
parms.Parent = IntPtr.Zero; // Make parentless
|
||||
return parms;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the control as a floating Window child
|
||||
/// of the desktop. To hide the control again,
|
||||
/// use the <see cref="Visible"/> property.
|
||||
/// </summary>
|
||||
public void ShowFloating() {
|
||||
if (this.Handle == IntPtr.Zero) {
|
||||
base.CreateControl();
|
||||
}
|
||||
User32.SetParent(base.Handle, IntPtr.Zero);
|
||||
User32.ShowWindow(base.Handle, ShowWindowCommand.Show);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make the mouse events pass through
|
||||
/// </summary>
|
||||
/// <param name="m"></param>
|
||||
protected override void WndProc(ref Message m) {
|
||||
const int HTTRANSPARENT = (-1);
|
||||
switch (m.Msg) {
|
||||
case (int)WindowsMessages.WM_NCHITTEST:
|
||||
m.Result = (IntPtr) HTTRANSPARENT;
|
||||
break;
|
||||
default:
|
||||
base.WndProc(ref m);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the location of the mouse on the screen (using screen coordinates, which might differ from bitmap coordindates in a multi screen setup)
|
||||
/// </summary>
|
||||
public Point MouseLocation {
|
||||
set {
|
||||
Rectangle tl = new Rectangle(value.X - (distanceX + Width), value.Y - (distanceY + Height), Width, Height);
|
||||
Rectangle tr = new Rectangle(value.X + distanceX, value.Y - (distanceY + Height), Width, Height);
|
||||
Rectangle bl = new Rectangle(value.X - (distanceX + Width), value.Y + distanceY, Width, Height);
|
||||
Rectangle br = new Rectangle(value.X + distanceX, value.Y + distanceY, Width, Height);
|
||||
Rectangle screenBounds = Screen.GetBounds(value);
|
||||
if (screenBounds.Contains(br)) {
|
||||
this.Location = br.Location;
|
||||
} else if (screenBounds.Contains(bl)) {
|
||||
this.Location = bl.Location;
|
||||
} else if (screenBounds.Contains(tr)) {
|
||||
this.Location = tr.Location;
|
||||
} else {
|
||||
this.Location = tl.Location;
|
||||
}
|
||||
|
||||
this.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the location of the bitmap to be displayed in zoom (using bitmap coordinates, which might differ from screen coordinates in a multi screen setup)
|
||||
/// </summary>
|
||||
public Point ZoomLocation {
|
||||
get {
|
||||
return zoomLocation;
|
||||
}
|
||||
set {
|
||||
zoomLocation = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The zoom property
|
||||
/// </summary>
|
||||
public int Zoom {
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This makes sure there is no background painted, as we have complete "paint" control it doesn't make sense to do otherwise.
|
||||
/// </summary>
|
||||
/// <param name="pevent"></param>
|
||||
protected override void OnPaintBackground(PaintEventArgs pevent) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Paint the zoom area and the cross
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
protected override void OnPaint(PaintEventArgs e) {
|
||||
if (imageToZoom == null) {
|
||||
return;
|
||||
}
|
||||
Graphics graphics = e.Graphics;
|
||||
graphics.Clear(Color.Black);
|
||||
graphics.SmoothingMode = SmoothingMode.None;
|
||||
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
|
||||
graphics.CompositingQuality = CompositingQuality.HighSpeed;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.Half;
|
||||
Rectangle clipRectangle = e.ClipRectangle;
|
||||
float zoomFactor = (float)100 / (float)Zoom;
|
||||
|
||||
int sourceWidth = (int)(Width * zoomFactor);
|
||||
int sourceHeight = (int)(Height * zoomFactor);
|
||||
Rectangle sourceRectangle = new Rectangle(ZoomLocation.X - (sourceWidth / 2) + 1, ZoomLocation.Y - (sourceHeight / 2) + 1, sourceWidth, sourceHeight);
|
||||
Rectangle destinationRectangle = new Rectangle(0, 0, Width, Height);
|
||||
//graphics.StretchBlt((Bitmap)imageToZoom, sourceRectangle, destinationRectangle);
|
||||
graphics.DrawImage(imageToZoom, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
|
||||
|
||||
int pixelThickness = Zoom / 100;
|
||||
using (Pen pen = new Pen(Color.Black, pixelThickness)) {
|
||||
int halfWidth = (Width >> 1) - (pixelThickness >> 1);
|
||||
int halfWidthEnd = (Width >> 1) - pixelThickness;
|
||||
int halfHeight = (Height >> 1) - (pixelThickness >> 1);
|
||||
int halfHeightEnd = (Height >> 1) - pixelThickness;
|
||||
graphics.DrawLine(pen, halfWidth, 0, halfWidth, halfHeightEnd);
|
||||
graphics.DrawLine(pen, halfWidth, halfHeightEnd + pixelThickness, halfWidth, Height);
|
||||
graphics.DrawLine(pen, 0, halfHeight, halfWidthEnd, halfHeight);
|
||||
graphics.DrawLine(pen, halfWidthEnd + pixelThickness, halfHeight, Width, halfHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
this.SuspendLayout();
|
||||
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
|
||||
// Only double-buffer when we are not in a TerminalServerSession
|
||||
this.DoubleBuffered = !System.Windows.Forms.SystemInformation.TerminalServerSession;
|
||||
this.Name = "Zoom";
|
||||
this.ResumeLayout(false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -62,10 +62,9 @@ namespace Greenshot.Forms {
|
|||
private ICapture capture = null;
|
||||
private Image capturedImage = null;
|
||||
private Timer timer = null;
|
||||
|
||||
private bool isZooming = true;
|
||||
private Point previousMousePos = Point.Empty;
|
||||
private FixMode fixMode = FixMode.None;
|
||||
private ZoomControl zoomControl = null;
|
||||
|
||||
/// <summary>
|
||||
/// Property to access the selected capture rectangle
|
||||
|
@ -129,7 +128,6 @@ namespace Greenshot.Forms {
|
|||
|
||||
// clean up
|
||||
this.FormClosed += delegate {
|
||||
RemoveZoom();
|
||||
currentForm = null;
|
||||
LOG.Debug("Remove CaptureForm from currentForm");
|
||||
};
|
||||
|
@ -181,36 +179,6 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the zoom control
|
||||
/// </summary>
|
||||
private void CreateZoom() {
|
||||
if (zoomControl == null) {
|
||||
zoomControl = new ZoomControl(capturedImage, new Size(200, 200));
|
||||
zoomControl.MouseLocation = cursorPos;
|
||||
zoomControl.ZoomLocation = cursorPosOnBitmap;
|
||||
zoomControl.ShowFloating();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the zoom control
|
||||
/// </summary>
|
||||
private void RemoveZoom() {
|
||||
if (zoomControl != null) {
|
||||
zoomControl.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the zoom after the window is showing
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
protected override void OnShown(EventArgs e) {
|
||||
base.OnShown(e);
|
||||
CreateZoom();
|
||||
}
|
||||
|
||||
#region key handling
|
||||
void CaptureFormKeyUp(object sender, KeyEventArgs e) {
|
||||
if (e.KeyCode == Keys.ShiftKey) {
|
||||
|
@ -241,11 +209,7 @@ namespace Greenshot.Forms {
|
|||
Invalidate();
|
||||
} else if (e.KeyCode == Keys.Z) {
|
||||
// Toggle zoom
|
||||
if (zoomControl == null) {
|
||||
CreateZoom();
|
||||
} else {
|
||||
RemoveZoom();
|
||||
}
|
||||
isZooming = !isZooming;
|
||||
} else if (e.KeyCode == Keys.Space) {
|
||||
switch (captureMode) {
|
||||
case CaptureMode.Region:
|
||||
|
@ -265,6 +229,11 @@ namespace Greenshot.Forms {
|
|||
#endregion
|
||||
|
||||
#region events
|
||||
/// <summary>
|
||||
/// The mousedown handler of the capture form
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void OnMouseDown(object sender, MouseEventArgs e) {
|
||||
if (e.Button == MouseButtons.Left) {
|
||||
Point tmpCursorLocation = WindowCapture.GetCursorLocation();
|
||||
|
@ -279,6 +248,11 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mouse up handler of the capture form
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void OnMouseUp(object sender, MouseEventArgs e) {
|
||||
if (mouseDown) {
|
||||
// If the mouse goes up we set down to false (nice logic!)
|
||||
|
@ -301,6 +275,11 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is used to "fix" the mouse coordinates when keeping shift/ctrl pressed
|
||||
/// </summary>
|
||||
/// <param name="currentMouse"></param>
|
||||
/// <returns></returns>
|
||||
private Point FixMouseCoordinates(Point currentMouse) {
|
||||
if (fixMode == FixMode.Initiated) {
|
||||
if (previousMousePos.X != currentMouse.X) {
|
||||
|
@ -317,6 +296,11 @@ namespace Greenshot.Forms {
|
|||
return currentMouse;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mouse move handler of the capture form
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void OnMouseMove(object sender, MouseEventArgs e) {
|
||||
// Make sure the mouse coordinates are fixed, when pressing shift
|
||||
mouseMovePos = FixMouseCoordinates(WindowCapture.GetCursorLocation());
|
||||
|
@ -327,10 +311,18 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tick handler of the capture form, this initiates the frame drawing.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void timer_Tick(object sender, EventArgs e) {
|
||||
updateFrame();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// update the frame, this only invalidates
|
||||
/// </summary>
|
||||
void updateFrame() {
|
||||
Point lastPos = cursorPos.Clone();
|
||||
cursorPos = mouseMovePos.Clone();
|
||||
|
@ -347,12 +339,6 @@ namespace Greenshot.Forms {
|
|||
bool horizontalMove = false;
|
||||
bool verticalMove = false;
|
||||
|
||||
// Change the zoom location
|
||||
if (zoomControl != null) {
|
||||
zoomControl.MouseLocation = cursorPos;
|
||||
zoomControl.ZoomLocation = cursorPosOnBitmap;
|
||||
}
|
||||
|
||||
if (lastPos.X != cursorPos.X) {
|
||||
horizontalMove = true;
|
||||
}
|
||||
|
@ -454,6 +440,10 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (isZooming && captureMode != CaptureMode.Window) {
|
||||
Invalidate(ZoomArea(lastPos, new Size(200, 200)));
|
||||
Invalidate(ZoomArea(cursorPos, new Size(200, 200)));
|
||||
}
|
||||
// Force update "now"
|
||||
Update();
|
||||
}
|
||||
|
@ -465,6 +455,73 @@ namespace Greenshot.Forms {
|
|||
protected override void OnPaintBackground(PaintEventArgs pevent) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the area of where the zoom can be drawn
|
||||
/// </summary>
|
||||
/// <param name="pos"></param>
|
||||
/// <param name="size"></param>
|
||||
/// <returns></returns>
|
||||
private Rectangle ZoomArea(Point pos, Size size) {
|
||||
const int distanceX = 20;
|
||||
const int distanceY = 20;
|
||||
Rectangle tl = new Rectangle(pos.X - (distanceX + size.Width), pos.Y - (distanceY + size.Height), size.Width, size.Height);
|
||||
Rectangle tr = new Rectangle(pos.X + distanceX, pos.Y - (distanceY + size.Height), size.Width, size.Height);
|
||||
Rectangle bl = new Rectangle(pos.X - (distanceX + size.Width), pos.Y + distanceY, size.Width, size.Height);
|
||||
Rectangle br = new Rectangle(pos.X + distanceX, pos.Y + distanceY, size.Width, size.Height);
|
||||
Rectangle screenBounds = Screen.GetBounds(pos);
|
||||
if (screenBounds.Contains(br)) {
|
||||
return br;
|
||||
} else if (screenBounds.Contains(bl)) {
|
||||
return bl;
|
||||
} else if (screenBounds.Contains(tr)) {
|
||||
return tr;
|
||||
} else {
|
||||
return tl;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draw the zoomed area
|
||||
/// </summary>
|
||||
/// <param name="graphics"></param>
|
||||
/// <param name="sourceRectangle"></param>
|
||||
/// <param name="destinationRectangle"></param>
|
||||
private void DrawZoom(Graphics graphics, Rectangle sourceRectangle, Rectangle destinationRectangle) {
|
||||
if (capturedImage == null || !isZooming) {
|
||||
return;
|
||||
}
|
||||
|
||||
graphics.SmoothingMode = SmoothingMode.None;
|
||||
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
|
||||
graphics.CompositingQuality = CompositingQuality.HighSpeed;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.None;
|
||||
|
||||
using (GraphicsPath path = new GraphicsPath()) {
|
||||
path.AddEllipse(destinationRectangle);
|
||||
using (Region clipRegion = new Region(path)) {
|
||||
graphics.Clip = clipRegion;
|
||||
graphics.DrawImage(capturedImage, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
|
||||
}
|
||||
}
|
||||
|
||||
int pixelThickness = destinationRectangle.Width / sourceRectangle.Width;
|
||||
using (Pen pen = new Pen(Color.Black, pixelThickness)) {
|
||||
int halfWidth = (destinationRectangle.Width >> 1) - (pixelThickness >> 1);
|
||||
int halfWidthEnd = (destinationRectangle.Width >> 1) - pixelThickness;
|
||||
int halfHeight = (destinationRectangle.Height >> 1) - (pixelThickness >> 1);
|
||||
int halfHeightEnd = (destinationRectangle.Height >> 1) - pixelThickness;
|
||||
graphics.DrawLine(pen, destinationRectangle.X + halfWidth, destinationRectangle.Y, destinationRectangle.X + halfWidth, destinationRectangle.Y + halfHeightEnd);
|
||||
graphics.DrawLine(pen, destinationRectangle.X + halfWidth, destinationRectangle.Y + halfHeightEnd + pixelThickness, destinationRectangle.X + halfWidth, destinationRectangle.Y + destinationRectangle.Height);
|
||||
graphics.DrawLine(pen, destinationRectangle.X, destinationRectangle.Y + halfHeight, destinationRectangle.X + halfWidthEnd, destinationRectangle.Y + halfHeight);
|
||||
graphics.DrawLine(pen, destinationRectangle.X + halfWidthEnd + pixelThickness, destinationRectangle.Y + halfHeight, destinationRectangle.X + destinationRectangle.Width, destinationRectangle.Y + halfHeight);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Paint the actual visible parts
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void OnPaint(object sender, PaintEventArgs e) {
|
||||
Graphics graphics = e.Graphics;
|
||||
Rectangle clipRectangle = e.ClipRectangle;
|
||||
|
@ -595,6 +652,15 @@ namespace Greenshot.Forms {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (captureMode != CaptureMode.Window) {
|
||||
const int zoomSourceWidth = 25;
|
||||
const int zoomSourceHeight = 25;
|
||||
|
||||
Rectangle sourceRectangle = new Rectangle(cursorPosOnBitmap.X - (zoomSourceWidth / 2), cursorPosOnBitmap.Y - (zoomSourceHeight / 2), zoomSourceWidth, zoomSourceHeight);
|
||||
DrawZoom(graphics, sourceRectangle, ZoomArea(cursorPos, new Size(200, 200)));
|
||||
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
@ -67,9 +67,6 @@
|
|||
<Compile Include="Controls\Pipette.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Controls\ZoomControl.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Destinations\ClipboardDestination.cs" />
|
||||
<Compile Include="Destinations\EditorDestination.cs" />
|
||||
<Compile Include="Destinations\EmailDestination.cs" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue