Initial commit: application ColorPicker (palette, pipette ecran, couleur de dossier)
This commit is contained in:
94
src/ScreenColorPickerForm.cs
Normal file
94
src/ScreenColorPickerForm.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace ColorPickerApp
|
||||
{
|
||||
/// <summary>
|
||||
/// Superpose une fenetre transparente plein ecran (tous moniteurs) permettant
|
||||
/// de capturer la couleur de n'importe quel pixel affiche a l'ecran.
|
||||
/// </summary>
|
||||
internal sealed class ScreenColorPickerForm : Form
|
||||
{
|
||||
private readonly Timer _timer;
|
||||
private readonly MagnifierForm _magnifier;
|
||||
private Color _hoverColor = Color.White;
|
||||
private Point _lastScreenPoint;
|
||||
|
||||
public Color PickedColor { get; private set; }
|
||||
|
||||
public ScreenColorPickerForm()
|
||||
{
|
||||
FormBorderStyle = FormBorderStyle.None;
|
||||
ShowInTaskbar = false;
|
||||
StartPosition = FormStartPosition.Manual;
|
||||
TopMost = true;
|
||||
BackColor = Color.Black;
|
||||
Opacity = 0.01; // fenetre quasi invisible, sert uniquement a capter la souris
|
||||
Cursor = Cursors.Cross;
|
||||
DoubleBuffered = true;
|
||||
KeyPreview = true;
|
||||
|
||||
Bounds = SystemInformation.VirtualScreen;
|
||||
|
||||
_magnifier = new MagnifierForm();
|
||||
|
||||
_timer = new Timer { Interval = 40 };
|
||||
_timer.Tick += (s, e) => UpdateHover();
|
||||
|
||||
MouseClick += OnMouseClick;
|
||||
KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape) { PickedColor = Color.Empty; Close(); } };
|
||||
}
|
||||
|
||||
protected override void OnShown(EventArgs e)
|
||||
{
|
||||
base.OnShown(e);
|
||||
_magnifier.Show(this);
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
protected override void OnFormClosed(FormClosedEventArgs e)
|
||||
{
|
||||
_timer.Stop();
|
||||
_magnifier.Close();
|
||||
base.OnFormClosed(e);
|
||||
}
|
||||
|
||||
private void UpdateHover()
|
||||
{
|
||||
_lastScreenPoint = Cursor.Position;
|
||||
try
|
||||
{
|
||||
using (var bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
|
||||
{
|
||||
using (var g = Graphics.FromImage(bmp))
|
||||
{
|
||||
g.CopyFromScreen(_lastScreenPoint, Point.Empty, new Size(1, 1));
|
||||
}
|
||||
_hoverColor = bmp.GetPixel(0, 0);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore capture errors (ex: DPI change) et garder la derniere couleur connue
|
||||
}
|
||||
|
||||
_magnifier.UpdateDisplay(_lastScreenPoint, _hoverColor);
|
||||
}
|
||||
|
||||
private void OnMouseClick(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.Button == MouseButtons.Left)
|
||||
{
|
||||
PickedColor = _hoverColor;
|
||||
Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
PickedColor = Color.Empty;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user