diff --git a/Greenshot/Controls/Dropper.cs b/Greenshot/Controls/Dropper.cs new file mode 100644 index 000000000..4159ddedc --- /dev/null +++ b/Greenshot/Controls/Dropper.cs @@ -0,0 +1,162 @@ +/* + * 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 . + */ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Forms; +using Greenshot.Plugin; +using Greenshot.Forms; +using System.Drawing; +using GreenshotPlugin.UnmanagedHelpers; + +namespace Greenshot.Controls { + /// + /// This code was supplied by Hi-Coder as a patch for Greenshot + /// Needed some modifications to be stable. + /// + public class Dropper : Label, IMessageFilter, IDisposable { + private Zoomer zoomer; + private bool dragging; + private Cursor _cursor; + private Bitmap _image; + private const int VK_ESC = 27; + + public event EventHandler DropperUsed; + + public Dropper() { + BorderStyle = BorderStyle.FixedSingle; + dragging = false; + _image = (Bitmap)new System.ComponentModel.ComponentResourceManager(typeof(ColorDialog)).GetObject("dropper.Image"); + _cursor = CreateCursor((Bitmap)_image, 0, 15); + zoomer = new Zoomer(); + zoomer.Visible = false; + Application.AddMessageFilter(this); + } + + /** + * Destructor + */ + ~Dropper() { + Dispose(false); + } + + // The bulk of the clean-up code is implemented in Dispose(bool) + + /** + * 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(); + DropperUsed(this, new DropperUsedArgs(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 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; + } + base.OnMouseCaptureChanged(e); + } + + #region IMessageFilter Members + + 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; + } + + #endregion + } + + public class DropperUsedArgs : EventArgs { + public Color color; + + public DropperUsedArgs(Color c) { + color = c; + } + } +} diff --git a/Greenshot/Forms/ColorDialog.Designer.cs b/Greenshot/Forms/ColorDialog.Designer.cs index 6b4a00cc5..e233f983d 100644 --- a/Greenshot/Forms/ColorDialog.Designer.cs +++ b/Greenshot/Forms/ColorDialog.Designer.cs @@ -46,6 +46,7 @@ namespace Greenshot { /// private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ColorDialog)); this.btnTransparent = new System.Windows.Forms.Button(); this.colorPanel = new System.Windows.Forms.Panel(); this.labelHtmlColor = new System.Windows.Forms.Label(); @@ -60,6 +61,7 @@ namespace Greenshot { this.textBoxAlpha = new System.Windows.Forms.TextBox(); this.labelAlpha = new System.Windows.Forms.Label(); this.btnApply = new System.Windows.Forms.Button(); + this.dropper = new Greenshot.Controls.Dropper(); this.SuspendLayout(); // // btnTransparent @@ -77,9 +79,9 @@ namespace Greenshot { // colorPanel // this.colorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.colorPanel.Location = new System.Drawing.Point(210, 31); + this.colorPanel.Location = new System.Drawing.Point(213, 30); this.colorPanel.Name = "colorPanel"; - this.colorPanel.Size = new System.Drawing.Size(78, 23); + this.colorPanel.Size = new System.Drawing.Size(33, 23); this.colorPanel.TabIndex = 1; // // labelHtmlColor @@ -206,11 +208,23 @@ namespace Greenshot { this.btnApply.UseVisualStyleBackColor = false; this.btnApply.Click += new System.EventHandler(this.BtnApplyClick); // + // dropper + // + this.dropper.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.dropper.Cursor = System.Windows.Forms.Cursors.Arrow; + this.dropper.Image = ((System.Drawing.Image)(resources.GetObject("dropper.Image"))); + this.dropper.Location = new System.Drawing.Point(255, 30); + this.dropper.Name = "dropper"; + this.dropper.Size = new System.Drawing.Size(33, 23); + this.dropper.TabIndex = 13; + this.dropper.DropperUsed += new System.EventHandler(this.dropperUsed); + // // ColorDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.ClientSize = new System.Drawing.Size(292, 218); + this.Controls.Add(this.dropper); this.Controls.Add(this.btnApply); this.Controls.Add(this.textBoxAlpha); this.Controls.Add(this.labelAlpha); @@ -235,6 +249,7 @@ namespace Greenshot { this.Text = "TestProject"; this.ResumeLayout(false); this.PerformLayout(); + } private System.Windows.Forms.Label labelRed; private System.Windows.Forms.Label labelGreen; @@ -250,6 +265,7 @@ namespace Greenshot { private System.Windows.Forms.TextBox textBoxBlue; private System.Windows.Forms.Panel colorPanel; private System.Windows.Forms.Button btnTransparent; + private Greenshot.Controls.Dropper dropper; diff --git a/Greenshot/Forms/ColorDialog.cs b/Greenshot/Forms/ColorDialog.cs index b29140690..4c47e1531 100644 --- a/Greenshot/Forms/ColorDialog.cs +++ b/Greenshot/Forms/ColorDialog.cs @@ -234,5 +234,9 @@ namespace Greenshot { } #endregion + + private void dropperUsed(object sender, Greenshot.Controls.DropperUsedArgs e) { + this.Color = e.color; + } } } diff --git a/Greenshot/Forms/ColorDialog.resx b/Greenshot/Forms/ColorDialog.resx new file mode 100644 index 000000000..b6b28bd6d --- /dev/null +++ b/Greenshot/Forms/ColorDialog.resx @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 + YQUAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAABqklEQVQ4T6WTvUtCURiH34Mi + Koo6CKI45HUT/4CgDwz8oMVFXLyGIVpDjTYkuDsoF6RPmtJKkIgKipYsCIoQihYXKxSCICjaGoTTe6R7 + uflBgcOPwz3wPL/3HM4FSikMkw6sVCphHPNCSE9auPf8kydCJuqEHNYIOakSMsXYjiClUPSFmVAUiHBb + EOh7KkWPAC4lQb9mcY8J5HAzGKSP4TDdBrj+SzCGkkqTkHk2NmuWw+sAfklQBYDunAPUaLlMG4Qc9INX + kZEERa8Xdvx+2A0EpJR8Pv4UJe1CoaeZwb8E2WwWcrkcCILQST6fd2YymeRS6SG9j5JGKEQ3AW7XAJZF + eKBAhN8+v+p7V61jJimi5AMvbgVXDIiRjiBO0A2f3b9uud3u5IzRyOMEDA4MFKCES6fTCbGZwS6XK2G3 + 250RvR42ZM09E/A8D/F4PCaHOY6LWSwWzmq1wp8Cj8fjiMzOLbAzs2YEeZPJ5DCbzfAvgc1mi7L20Unv + ok6ni2q12hGDwQCDBEV8nRWMdIkajWZapVLFMZxarQb8hn6CMkI3mDvMhVwwzO/8DbWQqn7YBXReAAAA + AElFTkSuQmCC + + + \ No newline at end of file diff --git a/Greenshot/Forms/Zoomer.Designer.cs b/Greenshot/Forms/Zoomer.Designer.cs new file mode 100644 index 000000000..bac5a3c1c --- /dev/null +++ b/Greenshot/Forms/Zoomer.Designer.cs @@ -0,0 +1,188 @@ +namespace Greenshot.Forms +{ + partial class Zoomer + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.panel1 = new System.Windows.Forms.Panel(); + this.html = new System.Windows.Forms.Label(); + this.label1 = new System.Windows.Forms.Label(); + this.preview = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.red = new System.Windows.Forms.Label(); + this.green = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.blue = new System.Windows.Forms.Label(); + this.label6 = new System.Windows.Forms.Label(); + this.alpha = new System.Windows.Forms.Label(); + this.label5 = new System.Windows.Forms.Label(); + this.panel1.SuspendLayout(); + this.SuspendLayout(); + // + // panel1 + // + this.panel1.BackColor = System.Drawing.SystemColors.Info; + this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panel1.Controls.Add(this.alpha); + this.panel1.Controls.Add(this.label5); + this.panel1.Controls.Add(this.blue); + this.panel1.Controls.Add(this.label6); + this.panel1.Controls.Add(this.green); + this.panel1.Controls.Add(this.label4); + this.panel1.Controls.Add(this.red); + this.panel1.Controls.Add(this.label2); + this.panel1.Controls.Add(this.html); + this.panel1.Controls.Add(this.label1); + this.panel1.Controls.Add(this.preview); + this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel1.Location = new System.Drawing.Point(0, 0); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(100, 100); + this.panel1.TabIndex = 0; + // + // html + // + this.html.Location = new System.Drawing.Point(40, 18); + this.html.Name = "html"; + this.html.Size = new System.Drawing.Size(59, 19); + this.html.TabIndex = 2; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(40, 5); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(37, 13); + this.label1.TabIndex = 1; + this.label1.Text = "HTML"; + // + // preview + // + this.preview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.preview.Location = new System.Drawing.Point(5, 5); + this.preview.Name = "preview"; + this.preview.Size = new System.Drawing.Size(32, 32); + this.preview.TabIndex = 0; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(2, 37); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(33, 13); + this.label2.TabIndex = 3; + this.label2.Text = "Red: "; + // + // red + // + this.red.Location = new System.Drawing.Point(43, 37); + this.red.Name = "red"; + this.red.Size = new System.Drawing.Size(55, 13); + this.red.TabIndex = 4; + // + // green + // + this.green.Location = new System.Drawing.Point(43, 50); + this.green.Name = "green"; + this.green.Size = new System.Drawing.Size(55, 13); + this.green.TabIndex = 6; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(2, 50); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(42, 13); + this.label4.TabIndex = 5; + this.label4.Text = "Green: "; + // + // blue + // + this.blue.Location = new System.Drawing.Point(43, 63); + this.blue.Name = "blue"; + this.blue.Size = new System.Drawing.Size(55, 13); + this.blue.TabIndex = 8; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(2, 63); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(34, 13); + this.label6.TabIndex = 7; + this.label6.Text = "Blue: "; + // + // alpha + // + this.alpha.Location = new System.Drawing.Point(43, 76); + this.alpha.Name = "alpha"; + this.alpha.Size = new System.Drawing.Size(55, 13); + this.alpha.TabIndex = 10; + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(2, 76); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(40, 13); + this.label5.TabIndex = 9; + this.label5.Text = "Alpha: "; + // + // Zoomer + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(100, 100); + this.Controls.Add(this.panel1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; + this.Name = "Zoomer"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.Text = "Zoomer"; + this.TopMost = true; + this.panel1.ResumeLayout(false); + this.panel1.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.Label html; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label preview; + private System.Windows.Forms.Label alpha; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label blue; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.Label green; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label red; + private System.Windows.Forms.Label label2; + } +} diff --git a/Greenshot/Forms/Zoomer.cs b/Greenshot/Forms/Zoomer.cs new file mode 100644 index 000000000..ed918c914 --- /dev/null +++ b/Greenshot/Forms/Zoomer.cs @@ -0,0 +1,93 @@ +/* + * 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 . + */ +using System; +using System.Windows.Forms; +using System.Drawing; +using GreenshotPlugin.UnmanagedHelpers; + +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 Color color { + get { + return preview.BackColor; + } + } + + public Zoomer() { + InitializeComponent(); + } + + public void setHotSpot(int x, int y) { + Color c = GetPixelColor(x, y); + preview.BackColor = c; + html.Text = "#" + c.Name.Substring(2).ToUpper(); + red.Text = "" + c.R; + blue.Text = "" + c.B; + green.Text = "" + c.G; + alpha.Text = "" + c.A; + + Size cs = Cursor.Current.Size; + Point hs = Cursor.Current.HotSpot; + + Point zp = new Point(x, y); + zp.X += cs.Width + 2 - hs.X; + zp.Y -= hs.Y; + + if (zp.X < 0) { + zp.X = 0; + } else if (zp.X + Width > Screen.PrimaryScreen.Bounds.Width) { + zp.X = x - Width - 2 - hs.X; + } + + if (zp.Y < 0) { + zp.Y = 0; + } else if (zp.Y + Height > Screen.PrimaryScreen.Bounds.Height) { + zp.Y = Screen.PrimaryScreen.Bounds.Height - Height; + } + + Location = zp; + } + + public void setHotSpot(Point screenCoordinates) { + setHotSpot(screenCoordinates.X, screenCoordinates.Y); + } + + static private Color GetPixelColor(int x, int y) { + IntPtr hdc = User32.GetDC(IntPtr.Zero); + try { + uint pixel = GDI32.GetPixel(hdc, x, y); + Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16); + return color; + } catch (Exception) { + return Color.Empty; + } finally { + if (hdc != IntPtr.Zero) { + User32.ReleaseDC(IntPtr.Zero, hdc); + } + } + } + + } +} diff --git a/Greenshot/Greenshot.csproj b/Greenshot/Greenshot.csproj index 578961637..cf11900f3 100644 --- a/Greenshot/Greenshot.csproj +++ b/Greenshot/Greenshot.csproj @@ -67,6 +67,7 @@ + @@ -153,6 +154,10 @@ BugReportForm.cs + + + Zoomer.cs + @@ -217,6 +222,9 @@ + + ColorDialog.cs + ImageEditorForm.cs diff --git a/Greenshot/icons/pipette.png b/Greenshot/icons/pipette.png new file mode 100644 index 000000000..2a161faeb Binary files /dev/null and b/Greenshot/icons/pipette.png differ diff --git a/GreenshotPlugin/UnmanagedHelpers/GDI32.cs b/GreenshotPlugin/UnmanagedHelpers/GDI32.cs index 812fd5814..c1262f850 100644 --- a/GreenshotPlugin/UnmanagedHelpers/GDI32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/GDI32.cs @@ -43,6 +43,8 @@ namespace GreenshotPlugin.UnmanagedHelpers { public static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect); [DllImport("gdi32", SetLastError=true)] public static extern int GetClipBox(IntPtr hdc, out RECT lprc); + [DllImport("gdi32", SetLastError = true)] + public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); } [StructLayout(LayoutKind.Sequential)] diff --git a/GreenshotPlugin/UnmanagedHelpers/User32.cs b/GreenshotPlugin/UnmanagedHelpers/User32.cs index 164cdc41a..60fed8861 100644 --- a/GreenshotPlugin/UnmanagedHelpers/User32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/User32.cs @@ -453,6 +453,21 @@ namespace GreenshotPlugin.UnmanagedHelpers { [DllImport("user32", SetLastError = true)] public static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon); + + [DllImport("user32", SetLastError = true)] + public static extern IntPtr SetCapture(IntPtr hWnd); + + [DllImport("user32", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool ReleaseCapture(); + + [DllImport("user32", SetLastError = true)] + public static extern int GetSystemMetrics(SystemMetric index); + + [DllImport("user32", SetLastError = true)] + public static extern IntPtr CreateIconIndirect(ref IconInfo icon); + + #endregion } }