<feat> + 屏幕坐标颜色选取工具

This commit is contained in:
nsnail 2022-12-05 16:01:31 +08:00
parent 6393132c90
commit 57dc2c7519
14 changed files with 381 additions and 64 deletions

13
src/Color/Main.cs Normal file
View File

@ -0,0 +1,13 @@
namespace Dot.Color;
public sealed class Main : Tool<Option>
{
public Main(Option opt) : base(opt) { }
public override Task Run()
{
Application.Run(new WinMain());
return Task.CompletedTask;
}
}

75
src/Color/MouseHook.cs Normal file
View File

@ -0,0 +1,75 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Dot.Color;
public class MouseHook : IDisposable
{
[StructLayout(LayoutKind.Explicit)]
private struct Msllhookstruct
{
// [FieldOffset(20)] private readonly nint dwExtraInfo;
// [FieldOffset(12)] private readonly uint flags;
// [FieldOffset(8)] private readonly uint mouseData;
// [FieldOffset(16)] private readonly uint time;
[FieldOffset(0)] public readonly int X;
[FieldOffset(4)] public readonly int Y;
}
public event MouseEventHandler MouseEvent = delegate { };
private const int _WH_MOUSE_LL = 14;
private const int _WM_LBUTTONDOWN = 0x0201;
private const int _WM_MOUSEMOVE = 0x0200;
private bool _disposed;
private readonly nint _hookId;
public MouseHook()
{
_hookId = SetHook(HookCallback);
}
~MouseHook()
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing) { }
if (_hookId != default) Win32.UnhookWindowsHookEx(_hookId);
_disposed = true;
}
private nint HookCallback(int nCode, nint wParam, nint lParam)
{
if (nCode < 0 || (_WM_MOUSEMOVE != wParam && _WM_LBUTTONDOWN != wParam))
return Win32.CallNextHookEx(_hookId, nCode, wParam, lParam);
var hookStruct = (Msllhookstruct)Marshal.PtrToStructure(lParam, typeof(Msllhookstruct))!;
MouseEvent(null, new MouseEventArgs( //
wParam == _WM_MOUSEMOVE ? MouseButtons.None : MouseButtons.Left //
, 0 //
, hookStruct.X //
, hookStruct.Y //
, 0));
return Win32.CallNextHookEx(_hookId, nCode, wParam, lParam);
}
private static nint SetHook(Win32.LowLevelMouseProc proc)
{
using var curProcess = Process.GetCurrentProcess();
using var curModule = curProcess.MainModule!;
return Win32.SetWindowsHookEx(_WH_MOUSE_LL, proc, Win32.GetModuleHandle(curModule.ModuleName), 0);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

4
src/Color/Option.cs Normal file
View File

@ -0,0 +1,4 @@
namespace Dot.Color;
[Verb("color", HelpText = nameof(Str.ScreenPixelTool), ResourceType = typeof(Str))]
public class Option : IOption { }

40
src/Color/Win32.cs Normal file
View File

@ -0,0 +1,40 @@
using System.Runtime.InteropServices;
namespace Dot.Color;
public static class Win32
{
public delegate nint LowLevelMouseProc(int nCode, nint wParam, nint lParam);
private const string _GDI32_DLL = "gdi32.dll";
private const string _KERNEL32_DLL = "kernel32.dll";
private const string _USER32_DLL = "user32.dll";
[DllImport(_USER32_DLL)]
public static extern nint CallNextHookEx(nint hhk, int nCode, nint wParam, nint lParam);
[DllImport(_USER32_DLL)]
public static extern nint GetDesktopWindow();
[DllImport(_KERNEL32_DLL, CharSet = CharSet.Unicode)]
public static extern nint GetModuleHandle(string lpModuleName);
[DllImport(_GDI32_DLL)]
public static extern uint GetPixel(nint dc, int x, int y);
[DllImport(_USER32_DLL)]
public static extern nint GetWindowDC(nint hWnd);
[DllImport(_USER32_DLL)]
public static extern int ReleaseDC(nint hWnd, nint dc);
[DllImport(_USER32_DLL)]
public static extern nint SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, nint hMod, uint dwThreadId);
[DllImport(_USER32_DLL)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(nint hhk);
}

70
src/Color/WinInfo.cs Normal file
View File

@ -0,0 +1,70 @@
using System.Drawing.Drawing2D;
namespace Dot.Color;
public class WinInfo : Form
{
private const int _WINDOW_SIZE = 480;
private const int _ZOOM_RATE = 16;
private bool _disposed;
private readonly Graphics _graphics;
private readonly PictureBox _pbox;
public WinInfo()
{
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
MinimizeBox = false;
MaximizeBox = false;
Size = new Size(_WINDOW_SIZE, _WINDOW_SIZE);
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
_pbox = new PictureBox();
_pbox.Location = new Point(0, 0);
_pbox.Size = Size;
_pbox.Image = new Bitmap(_WINDOW_SIZE, _WINDOW_SIZE);
_graphics = Graphics.FromImage(_pbox.Image);
_graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
_graphics.CompositingQuality = CompositingQuality.HighQuality;
_graphics.SmoothingMode = SmoothingMode.None;
Controls.Add(_pbox);
}
~WinInfo()
{
Dispose(false);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_disposed) return;
if (disposing) {
_graphics?.Dispose();
_pbox?.Dispose();
}
_disposed = true;
}
public void UpdateImage(Bitmap img, int x, int y)
{
var copySize = new Size(_WINDOW_SIZE / _ZOOM_RATE, _WINDOW_SIZE / _ZOOM_RATE);
_graphics.DrawImage(img, new Rectangle(0, 0, _WINDOW_SIZE, _WINDOW_SIZE) //
, x - copySize.Width / 2 //
, y - copySize.Height / 2 //
, copySize.Width, copySize.Height, GraphicsUnit.Pixel);
using var pen = new Pen(System.Drawing.Color.Aqua);
_graphics.DrawRectangle(pen, _WINDOW_SIZE / 2 - _ZOOM_RATE / 2 //
, _WINDOW_SIZE / 2 - _ZOOM_RATE / 2 //
, _ZOOM_RATE, _ZOOM_RATE);
var posColor = img.GetPixel(x, y);
_graphics.FillRectangle(Brushes.Black, 0, _WINDOW_SIZE - 30, _WINDOW_SIZE, 30);
_graphics.DrawString($"{Str.ClickCopyColor} X: {x} Y: {y} RGB({posColor.R},{posColor.G},{posColor.B})"
, new Font(FontFamily.GenericSerif, 10), Brushes.White, 0, _WINDOW_SIZE - 20);
_pbox.Refresh();
}
}

68
src/Color/WinMain.cs Normal file
View File

@ -0,0 +1,68 @@
using TextCopy;
namespace Dot.Color;
public class WinMain : Form
{
private readonly Bitmap _bmp;
private bool _disposed;
private readonly WinInfo _winInfo = new();
public WinMain()
{
FormBorderStyle = FormBorderStyle.None;
Size = Screen.PrimaryScreen!.Bounds.Size;
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
Opacity = 0.01d;
_bmp = new Bitmap(Size.Width, Size.Height);
using var g = Graphics.FromImage(_bmp);
g.CopyFromScreen(0, 0, 0, 0, Size);
}
~WinMain()
{
Dispose(false);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_disposed) return;
if (disposing) {
_bmp?.Dispose();
_winInfo?.Dispose();
}
_disposed = true;
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) Application.Exit();
}
protected override void OnLoad(EventArgs e)
{
_winInfo.Show();
}
protected override void OnMouseDown(MouseEventArgs e)
{
var color = _bmp.GetPixel(e.X, e.Y);
ClipboardService.SetText(
$"{e.X},{e.Y} #{color.R.ToString("X2")}{color.G.ToString("X2")}{color.B.ToString("X2")}({color.R},{color.G},{color.B})");
Application.Exit();
}
protected override void OnMouseMove(MouseEventArgs e)
{
_winInfo.UpdateImage(_bmp, e.X, e.Y);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(_bmp, 0, 0);
Opacity = 1;
}
}

View File

@ -59,6 +59,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 单击鼠标左键复制颜色和坐标到剪贴板.
/// </summary>
public static string ClickCopyColor {
get {
return ResourceManager.GetString("ClickCopyColor", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 转换换行符为LF.
/// </summary>
@ -104,15 +113,6 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 文本编码工具.
/// </summary>
public static string HelpForText {
get {
return ResourceManager.GetString("HelpForText", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 输入文本为空.
/// </summary>
@ -248,6 +248,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 屏幕坐标颜色选取工具.
/// </summary>
public static string ScreenPixelTool {
get {
return ResourceManager.GetString("ScreenPixelTool", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 查找文件....
/// </summary>
@ -311,6 +320,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 文本编码工具.
/// </summary>
public static string TextTool {
get {
return ResourceManager.GetString("TextTool", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 连接NTP服务器超时时间 (毫秒).
/// </summary>
@ -320,6 +338,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to 时间同步工具.
/// </summary>
public static string TimeTool {
get {
return ResourceManager.GetString("TimeTool", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 移除文件的uf8 bom.
/// </summary>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
id="root" xmlns="">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root" xmlns="">
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
@ -35,9 +34,6 @@
<data name="ShowMessageTemp" xml:space="preserve">
<value>Read: {0}/{1}, processed: {2}, skipped: {3}</value>
</data>
<data name="HelpForText" xml:space="preserve">
<value>Text encoding tool</value>
</data>
<data name="Copied" xml:space="preserve">
<value>{0}(copied to clipboard)</value>
</data>
@ -113,4 +109,16 @@
<data name="LocalClockOffset" xml:space="preserve">
<value>Local clock offset</value>
</data>
<data name="TimeTool" xml:space="preserve">
<value>Time synchronization tool</value>
</data>
<data name="TextTool" xml:space="preserve">
<value>Text encoding tool</value>
</data>
<data name="ScreenPixelTool" xml:space="preserve">
<value>Screen coordinate color selection tool</value>
</data>
<data name="ClickCopyColor" xml:space="preserve">
<value>Click the left mouse button to copy the colors and coordinates to the clipboard</value>
</data>
</root>

View File

@ -39,7 +39,13 @@
<data name="ShowMessageTemp" xml:space="preserve">
<value>已读取:{0}/{1},处理:{2},跳过:{3}</value>
</data>
<data name="HelpForText" xml:space="preserve">
<data name="TimeTool" xml:space="preserve">
<value>时间同步工具</value>
</data>
<data name="ScreenPixelTool" xml:space="preserve">
<value>屏幕坐标颜色选取工具</value>
</data>
<data name="TextTool" xml:space="preserve">
<value>文本编码工具</value>
</data>
<data name="TextTobeProcessed" xml:space="preserve">
@ -122,4 +128,7 @@
<data name="LocalClockOffset" xml:space="preserve">
<value>Local clock offset</value>
</data>
<data name="ClickCopyColor" xml:space="preserve">
<value>单击鼠标左键复制颜色和坐标到剪贴板</value>
</data>
</root>

View File

@ -31,7 +31,7 @@ public sealed class Main : Tool<Option>, IDisposable
{
_step2Bar.Tick();
ShowMessage(1, 0, 0);
var spacesCnt = 0;
int spacesCnt;
await using var fsrw = OpenFileStream(file, FileMode.Open, FileAccess.ReadWrite);

View File

@ -1,6 +1,6 @@
namespace Dot.Text;
[Verb("text", HelpText = nameof(Str.HelpForText), ResourceType = typeof(Str))]
[Verb("text", HelpText = nameof(Str.TextTool), ResourceType = typeof(Str))]
public class Option : IOption
{
[Value(0, HelpText = nameof(Str.TextTobeProcessed), ResourceType = typeof(Str))]

View File

@ -1,6 +1,6 @@
namespace Dot.Time;
[Verb("time", HelpText = nameof(Str.HelpForText), ResourceType = typeof(Str))]
[Verb("time", HelpText = nameof(Str.TimeTool), ResourceType = typeof(Str))]
public class Option : IOption
{
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]

View File

@ -14,6 +14,7 @@ public static class ToolsFactory
, Text.Option o => new Text.Main(o)
, Guid.Option o => new Guid.Main(o)
, Time.Option o => new Time.Main(o)
, Color.Option o => new Color.Main(o)
, _ => throw new ArgumentOutOfRangeException(nameof(option))
};
}

View File

@ -2,11 +2,12 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Dot</RootNamespace>
<AssemblyName>dot</AssemblyName>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<Authors>nsnail</Authors>
<Copyright>Copyright (c) 2022 nsnail</Copyright>
<RepositoryUrl>https://github.com/nsnail/dot.git</RepositoryUrl>
@ -29,6 +30,7 @@
<PackageReference Include="TextCopy" Version="6.2.0"/>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Lang\Str.resx">
<Generator>PublicResXFileCodeGenerator</Generator>