Refactoring the Pipette logic, also fixed some small issue...

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1658 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-02-14 11:04:11 +00:00
commit cce32fd6b0
4 changed files with 142 additions and 109 deletions

View file

@ -33,7 +33,7 @@ namespace Greenshot.Controls {
/// Needed some modifications to be stable. /// Needed some modifications to be stable.
/// </summary> /// </summary>
public class Pipette : Label, IMessageFilter, IDisposable { public class Pipette : Label, IMessageFilter, IDisposable {
private Zoomer zoomer; private MovableShowColorForm movableShowColorForm;
private bool dragging; private bool dragging;
private Cursor _cursor; private Cursor _cursor;
private Bitmap _image; private Bitmap _image;
@ -46,71 +46,25 @@ namespace Greenshot.Controls {
dragging = false; dragging = false;
_image = (Bitmap)new System.ComponentModel.ComponentResourceManager(typeof(ColorDialog)).GetObject("pipette.Image"); _image = (Bitmap)new System.ComponentModel.ComponentResourceManager(typeof(ColorDialog)).GetObject("pipette.Image");
Image = _image; Image = _image;
_cursor = CreateCursor((Bitmap)_image, 0, 15); _cursor = CreateCursor((Bitmap)_image, 1, 14);
zoomer = new Zoomer(); movableShowColorForm = new MovableShowColorForm();
Application.AddMessageFilter(this); Application.AddMessageFilter(this);
} }
/** /// <summary>
* Destructor /// Create a cursor from the supplied bitmap & hotspot coordinates
*/ /// </summary>
~Pipette() { /// <param name="bitmap">Bitmap to create an icon from</param>
Dispose(false); /// <param name="hotspotX">Hotspot X coordinate</param>
} /// <param name="hotspotY">Hotspot Y coordinate</param>
/// <returns>Cursor</returns>
// The bulk of the clean-up code is implemented in Dispose(bool) private static Cursor CreateCursor(Bitmap bitmap, int hotspotX, int hotspotY) {
/**
* This Dispose is called from the Dispose and the Destructor.
* When disposing==true all non-managed resources should be freed too!
*/
protected override void Dispose(bool disposing) {
base.Dispose();
if (disposing) {
if (_cursor != null) {
_cursor.Dispose();
}
if (zoomer != null) {
zoomer.Dispose();
}
}
zoomer = null;
_cursor = null;
}
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
User32.SetCapture(this.Handle);
zoomer.setHotSpot(PointToScreen(new Point(e.X, e.Y)));
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
//Release Capture should consume MouseUp when canceled with the escape key
User32.ReleaseCapture();
PipetteUsed(this, new PipetteUsedArgs(zoomer.color));
}
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e) {
if (dragging) {
//display the form on the right side of the cursor by default;
Point zp = PointToScreen(new Point(e.X, e.Y));
zoomer.setHotSpot(zp);
}
base.OnMouseMove(e);
}
private Cursor CreateCursor(Bitmap bitmap, int x, int y) {
IntPtr iconHandle = bitmap.GetHicon(); IntPtr iconHandle = bitmap.GetHicon();
IntPtr icon; IntPtr icon;
IconInfo iconInfo = new IconInfo(); IconInfo iconInfo = new IconInfo();
User32.GetIconInfo(iconHandle, out iconInfo); User32.GetIconInfo(iconHandle, out iconInfo);
iconInfo.xHotspot = 0; iconInfo.xHotspot = hotspotX;
iconInfo.yHotspot = 15; iconInfo.yHotspot = hotspotY;
iconInfo.fIcon = false; iconInfo.fIcon = false;
icon = User32.CreateIconIndirect(ref iconInfo); icon = User32.CreateIconIndirect(ref iconInfo);
Cursor returnCursor = new Cursor(icon); Cursor returnCursor = new Cursor(icon);
@ -119,18 +73,92 @@ namespace Greenshot.Controls {
return returnCursor; return returnCursor;
} }
/// <summary>
/// Destructor
/// </summary>
~Pipette() {
Dispose(false);
}
/// <summary>
/// The bulk of the clean-up code is implemented in Dispose(bool)
/// </summary>
public new void Dispose() {
Dispose(true);
}
/// <summary>
/// This Dispose is called from the Dispose and the Destructor.
/// </summary>
/// <param name="disposing">When disposing==true all non-managed resources should be freed too!</param>
protected override void Dispose(bool disposing) {
if (disposing) {
if (_cursor != null) {
_cursor.Dispose();
}
if (movableShowColorForm != null) {
movableShowColorForm.Dispose();
}
}
movableShowColorForm = null;
_cursor = null;
base.Dispose();
}
/// <summary>
/// Handle the mouse down on the Pipette "label", we take the capture and move the zoomer to the current location
/// </summary>
/// <param name="e">MouseEventArgs</param>
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
User32.SetCapture(this.Handle);
movableShowColorForm.MoveTo(PointToScreen(new Point(e.X, e.Y)));
}
base.OnMouseDown(e);
}
/// <summary>
/// Handle the mouse up on the Pipette "label", we release the capture and fire the PipetteUsed event
/// </summary>
/// <param name="e">MouseEventArgs</param>
protected override void OnMouseUp(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
//Release Capture should consume MouseUp when canceled with the escape key
User32.ReleaseCapture();
PipetteUsed(this, new PipetteUsedArgs(movableShowColorForm.color));
}
base.OnMouseUp(e);
}
/// <summary>
/// Handle the mouse Move event, we move the ColorUnderCursor to the current location.
/// </summary>
/// <param name="e">MouseEventArgs</param>
protected override void OnMouseMove(MouseEventArgs e) {
if (dragging) {
//display the form on the right side of the cursor by default;
Point zp = PointToScreen(new Point(e.X, e.Y));
movableShowColorForm.MoveTo(zp);
}
base.OnMouseMove(e);
}
/// <summary>
/// Handle the MouseCaptureChanged event
/// </summary>
/// <param name="e"></param>
protected override void OnMouseCaptureChanged(EventArgs e) { protected override void OnMouseCaptureChanged(EventArgs e) {
if (this.Capture) { if (this.Capture) {
dragging = true; dragging = true;
Image = null; Image = null;
Cursor c = _cursor; Cursor c = _cursor;
Cursor = c; Cursor = c;
zoomer.Visible = true; movableShowColorForm.Visible = true;
} else { } else {
dragging = false; dragging = false;
Image = _image; Image = _image;
Cursor = Cursors.Arrow; Cursor = Cursors.Arrow;
zoomer.Visible = false; movableShowColorForm.Visible = false;
} }
Update(); Update();
base.OnMouseCaptureChanged(e); base.OnMouseCaptureChanged(e);

View file

@ -1,6 +1,6 @@
namespace Greenshot.Forms namespace Greenshot.Forms
{ {
partial class Zoomer partial class MovableShowColorForm
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.

View file

@ -28,19 +28,23 @@ namespace Greenshot.Forms {
/// This code was supplied by Hi-Coder as a patch for Greenshot /// This code was supplied by Hi-Coder as a patch for Greenshot
/// Needed some modifications to be stable. /// Needed some modifications to be stable.
/// </summary> /// </summary>
public partial class Zoomer : Form { public partial class MovableShowColorForm : Form {
public Color color { public Color color {
get { get {
return preview.BackColor; return preview.BackColor;
} }
} }
public Zoomer() { public MovableShowColorForm() {
InitializeComponent(); InitializeComponent();
} }
public void setHotSpot(int x, int y) { /// <summary>
Color c = GetPixelColor(x, y); /// Move the MovableShowColorForm to the specified location and display the color under the (current mouse) coordinates
/// </summary>
/// <param name="screenCoordinates">Coordinates</param>
public void MoveTo(Point screenCoordinates) {
Color c = GetPixelColor(screenCoordinates);
preview.BackColor = c; preview.BackColor = c;
html.Text = "#" + c.Name.Substring(2).ToUpper(); html.Text = "#" + c.Name.Substring(2).ToUpper();
red.Text = "" + c.R; red.Text = "" + c.R;
@ -51,23 +55,23 @@ namespace Greenshot.Forms {
Size cursorSize = Cursor.Current.Size; Size cursorSize = Cursor.Current.Size;
Point hotspot = Cursor.Current.HotSpot; Point hotspot = Cursor.Current.HotSpot;
Point zoomerLocation = new Point(x, y); Point zoomerLocation = new Point(screenCoordinates.X, screenCoordinates.Y);
zoomerLocation.X += cursorSize.Width + 5 - hotspot.X; zoomerLocation.X += cursorSize.Width + 5 - hotspot.X;
zoomerLocation.Y += cursorSize.Height + 5 - hotspot.Y; zoomerLocation.Y += cursorSize.Height + 5 - hotspot.Y;
foreach (Screen screen in Screen.AllScreens) { foreach (Screen screen in Screen.AllScreens) {
Rectangle screenRectangle = screen.Bounds; Rectangle screenRectangle = screen.Bounds;
if (screen.Bounds.Contains(x, y)) { if (screen.Bounds.Contains(screenCoordinates)) {
if (zoomerLocation.X < screenRectangle.X) { if (zoomerLocation.X < screenRectangle.X) {
zoomerLocation.X = screenRectangle.X; zoomerLocation.X = screenRectangle.X;
} else if (zoomerLocation.X + Width > screenRectangle.X + screenRectangle.Width) { } else if (zoomerLocation.X + Width > screenRectangle.X + screenRectangle.Width) {
zoomerLocation.X = x - Width - 5 - hotspot.X; zoomerLocation.X = screenCoordinates.X - Width - 5 - hotspot.X;
} }
if (zoomerLocation.Y < screenRectangle.Y) { if (zoomerLocation.Y < screenRectangle.Y) {
zoomerLocation.Y = screenRectangle.Y; zoomerLocation.Y = screenRectangle.Y;
} else if (zoomerLocation.Y + Height > screenRectangle.Y + screenRectangle.Height) { } else if (zoomerLocation.Y + Height > screenRectangle.Y + screenRectangle.Height) {
zoomerLocation.Y = y - Height - 5 - hotspot.Y; zoomerLocation.Y = screenCoordinates.Y - Height - 5 - hotspot.Y;
} }
break; break;
} }
@ -76,14 +80,15 @@ namespace Greenshot.Forms {
Update(); Update();
} }
public void setHotSpot(Point screenCoordinates) { /// <summary>
setHotSpot(screenCoordinates.X, screenCoordinates.Y); /// Get the color from the pixel on the screen at "x,y"
} /// </summary>
/// <param name="screenCoordinates">Point with the coordinates</param>
static private Color GetPixelColor(int x, int y) { /// <returns>Color at the specified screenCoordinates</returns>
static private Color GetPixelColor(Point screenCoordinates) {
IntPtr hdc = User32.GetDC(IntPtr.Zero); IntPtr hdc = User32.GetDC(IntPtr.Zero);
try { try {
uint pixel = GDI32.GetPixel(hdc, x, y); uint pixel = GDI32.GetPixel(hdc, screenCoordinates.X, screenCoordinates.Y);
Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16); Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16);
return color; return color;
} catch (Exception) { } catch (Exception) {

View file

@ -154,9 +154,9 @@
<Compile Include="Forms\BugReportForm.Designer.cs"> <Compile Include="Forms\BugReportForm.Designer.cs">
<DependentUpon>BugReportForm.cs</DependentUpon> <DependentUpon>BugReportForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Forms\Zoomer.cs" /> <Compile Include="Forms\MovableShowColorForm.cs" />
<Compile Include="Forms\Zoomer.Designer.cs"> <Compile Include="Forms\MovableShowColorForm.Designer.cs">
<DependentUpon>Zoomer.cs</DependentUpon> <DependentUpon>MovableShowColorForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Helpers\AviHelper.cs" /> <Compile Include="Helpers\AviHelper.cs" />
<Compile Include="Helpers\CaptureHelper.cs" /> <Compile Include="Helpers\CaptureHelper.cs" />