From cce32fd6b0eddc6e91e5f97dcda3b566401af282 Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 14 Feb 2012 11:04:11 +0000 Subject: [PATCH] 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 --- Greenshot/Controls/Pipette.cs | 210 ++++++++++-------- ...er.cs => MovableShowColorForm.Designer.cs} | 2 +- .../{Zoomer.cs => MovableShowColorForm.cs} | 33 +-- Greenshot/Greenshot.csproj | 6 +- 4 files changed, 142 insertions(+), 109 deletions(-) rename Greenshot/Forms/{Zoomer.Designer.cs => MovableShowColorForm.Designer.cs} (96%) rename Greenshot/Forms/{Zoomer.cs => MovableShowColorForm.cs} (69%) diff --git a/Greenshot/Controls/Pipette.cs b/Greenshot/Controls/Pipette.cs index 19f723dd3..98426acc6 100644 --- a/Greenshot/Controls/Pipette.cs +++ b/Greenshot/Controls/Pipette.cs @@ -32,131 +32,159 @@ namespace Greenshot.Controls { /// This code was supplied by Hi-Coder as a patch for Greenshot /// Needed some modifications to be stable. /// - public class Pipette : Label, IMessageFilter, IDisposable { - private Zoomer zoomer; - private bool dragging; - private Cursor _cursor; + public class Pipette : Label, IMessageFilter, IDisposable { + private MovableShowColorForm movableShowColorForm; + private bool dragging; + private Cursor _cursor; private Bitmap _image; - private const int VK_ESC = 27; + private const int VK_ESC = 27; - public event EventHandler PipetteUsed; + public event EventHandler PipetteUsed; - public Pipette() { - BorderStyle = BorderStyle.FixedSingle; - dragging = false; + public Pipette() { + BorderStyle = BorderStyle.FixedSingle; + dragging = false; _image = (Bitmap)new System.ComponentModel.ComponentResourceManager(typeof(ColorDialog)).GetObject("pipette.Image"); Image = _image; - _cursor = CreateCursor((Bitmap)_image, 0, 15); - zoomer = new Zoomer(); - Application.AddMessageFilter(this); - } + _cursor = CreateCursor((Bitmap)_image, 1, 14); + movableShowColorForm = new MovableShowColorForm(); + Application.AddMessageFilter(this); + } - /** - * Destructor - */ + /// + /// Create a cursor from the supplied bitmap & hotspot coordinates + /// + /// Bitmap to create an icon from + /// Hotspot X coordinate + /// Hotspot Y coordinate + /// Cursor + private static Cursor CreateCursor(Bitmap bitmap, int hotspotX, int hotspotY) { + IntPtr iconHandle = bitmap.GetHicon(); + IntPtr icon; + IconInfo iconInfo = new IconInfo(); + User32.GetIconInfo(iconHandle, out iconInfo); + iconInfo.xHotspot = hotspotX; + iconInfo.yHotspot = hotspotY; + iconInfo.fIcon = false; + icon = User32.CreateIconIndirect(ref iconInfo); + Cursor returnCursor = new Cursor(icon); + //User32.DestroyIcon(icon); + User32.DestroyIcon(iconHandle); + return returnCursor; + } + + /// + /// Destructor + /// ~Pipette() { Dispose(false); } - // The bulk of the clean-up code is implemented in Dispose(bool) + /// + /// The bulk of the clean-up code is implemented in Dispose(bool) + /// + public new void Dispose() { + Dispose(true); + } - /** - * This Dispose is called from the Dispose and the Destructor. - * When disposing==true all non-managed resources should be freed too! - */ + /// + /// 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(); + if (movableShowColorForm != null) { + movableShowColorForm.Dispose(); } } - zoomer = null; + movableShowColorForm = null; _cursor = null; + base.Dispose(); } + /// + /// Handle the mouse down on the Pipette "label", we take the capture and move the zoomer to the current location + /// + /// MouseEventArgs 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); - } + if (e.Button == MouseButtons.Left) { + User32.SetCapture(this.Handle); + movableShowColorForm.MoveTo(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); - } + /// + /// Handle the mouse up on the Pipette "label", we release the capture and fire the PipetteUsed event + /// + /// MouseEventArgs + 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); + } - 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); - } + /// + /// Handle the mouse Move event, we move the ColorUnderCursor to the current location. + /// + /// MouseEventArgs + 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); + } - private Cursor CreateCursor(Bitmap bitmap, int x, int y) { - IntPtr iconHandle = bitmap.GetHicon(); - IntPtr icon; - IconInfo iconInfo = new IconInfo(); - User32.GetIconInfo(iconHandle, out iconInfo); - iconInfo.xHotspot = 0; - iconInfo.yHotspot = 15; - iconInfo.fIcon = false; - icon = User32.CreateIconIndirect(ref iconInfo); - Cursor returnCursor = new Cursor(icon); - //User32.DestroyIcon(icon); - User32.DestroyIcon(iconHandle); - return returnCursor; - } - - protected override void OnMouseCaptureChanged(EventArgs e) { - if (this.Capture) { - dragging = true; - Image = null; - Cursor c = _cursor; - Cursor = c; - zoomer.Visible = true; - } else { - dragging = false; - Image = _image; - Cursor = Cursors.Arrow; - zoomer.Visible = false; - } + /// + /// Handle the MouseCaptureChanged event + /// + /// + protected override void OnMouseCaptureChanged(EventArgs e) { + if (this.Capture) { + dragging = true; + Image = null; + Cursor c = _cursor; + Cursor = c; + movableShowColorForm.Visible = true; + } else { + dragging = false; + Image = _image; + Cursor = Cursors.Arrow; + movableShowColorForm.Visible = false; + } Update(); - base.OnMouseCaptureChanged(e); - } + base.OnMouseCaptureChanged(e); + } - #region IMessageFilter Members + #region IMessageFilter Members - public bool PreFilterMessage(ref Message m) { - if (dragging) { + public bool PreFilterMessage(ref Message m) { + if (dragging) { if (m.Msg == (int)WindowsMessages.WM_CHAR) { if ((int)m.WParam == VK_ESC) { User32.ReleaseCapture(); } } - } - return false; - } + } + return false; + } - #endregion - } + #endregion + } - public class PipetteUsedArgs : EventArgs { - public Color color; + public class PipetteUsedArgs : EventArgs { + public Color color; - public PipetteUsedArgs(Color c) { - color = c; - } - } + public PipetteUsedArgs(Color c) { + color = c; + } + } } diff --git a/Greenshot/Forms/Zoomer.Designer.cs b/Greenshot/Forms/MovableShowColorForm.Designer.cs similarity index 96% rename from Greenshot/Forms/Zoomer.Designer.cs rename to Greenshot/Forms/MovableShowColorForm.Designer.cs index 3bc8c6ec2..811772d0b 100644 --- a/Greenshot/Forms/Zoomer.Designer.cs +++ b/Greenshot/Forms/MovableShowColorForm.Designer.cs @@ -1,6 +1,6 @@ namespace Greenshot.Forms { - partial class Zoomer + partial class MovableShowColorForm { /// /// Required designer variable. diff --git a/Greenshot/Forms/Zoomer.cs b/Greenshot/Forms/MovableShowColorForm.cs similarity index 69% rename from Greenshot/Forms/Zoomer.cs rename to Greenshot/Forms/MovableShowColorForm.cs index 4c4d5152b..f2f788d79 100644 --- a/Greenshot/Forms/Zoomer.cs +++ b/Greenshot/Forms/MovableShowColorForm.cs @@ -28,19 +28,23 @@ namespace Greenshot.Forms { /// This code was supplied by Hi-Coder as a patch for Greenshot /// Needed some modifications to be stable. /// - public partial class Zoomer : Form { + public partial class MovableShowColorForm : Form { public Color color { get { return preview.BackColor; } } - public Zoomer() { + public MovableShowColorForm() { InitializeComponent(); } - public void setHotSpot(int x, int y) { - Color c = GetPixelColor(x, y); + /// + /// Move the MovableShowColorForm to the specified location and display the color under the (current mouse) coordinates + /// + /// Coordinates + public void MoveTo(Point screenCoordinates) { + Color c = GetPixelColor(screenCoordinates); preview.BackColor = c; html.Text = "#" + c.Name.Substring(2).ToUpper(); red.Text = "" + c.R; @@ -51,23 +55,23 @@ namespace Greenshot.Forms { Size cursorSize = Cursor.Current.Size; 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.Y += cursorSize.Height + 5 - hotspot.Y; foreach (Screen screen in Screen.AllScreens) { Rectangle screenRectangle = screen.Bounds; - if (screen.Bounds.Contains(x, y)) { + if (screen.Bounds.Contains(screenCoordinates)) { if (zoomerLocation.X < screenRectangle.X) { zoomerLocation.X = screenRectangle.X; } 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) { zoomerLocation.Y = screenRectangle.Y; } 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; } @@ -76,14 +80,15 @@ namespace Greenshot.Forms { Update(); } - public void setHotSpot(Point screenCoordinates) { - setHotSpot(screenCoordinates.X, screenCoordinates.Y); - } - - static private Color GetPixelColor(int x, int y) { + /// + /// Get the color from the pixel on the screen at "x,y" + /// + /// Point with the coordinates + /// Color at the specified screenCoordinates + static private Color GetPixelColor(Point screenCoordinates) { IntPtr hdc = User32.GetDC(IntPtr.Zero); 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); return color; } catch (Exception) { diff --git a/Greenshot/Greenshot.csproj b/Greenshot/Greenshot.csproj index d13cdbd11..325d42ba0 100644 --- a/Greenshot/Greenshot.csproj +++ b/Greenshot/Greenshot.csproj @@ -154,9 +154,9 @@ BugReportForm.cs - - - Zoomer.cs + + + MovableShowColorForm.cs