* <feat> + 屏幕坐标颜色选取工具
This commit is contained in:
nsnail 2022-12-05 16:05:58 +08:00 committed by GitHub
parent 8e0836dc15
commit f2e8b8b891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 516 additions and 579 deletions

View File

@ -1,7 +1,6 @@
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve">
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"&gt;
&lt;TypePattern&gt;
@ -11,6 +10,7 @@
&lt;Kind.Order&gt;
&lt;DeclarationKind&gt;Interface&lt;/DeclarationKind&gt;
&lt;DeclarationKind&gt;Class&lt;/DeclarationKind&gt;
&lt;DeclarationKind&gt;Record&lt;/DeclarationKind&gt;
&lt;DeclarationKind&gt;Enum&lt;/DeclarationKind&gt;
&lt;DeclarationKind&gt;Struct&lt;/DeclarationKind&gt;
&lt;DeclarationKind&gt;Delegate&lt;/DeclarationKind&gt;

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>
@ -275,6 +284,15 @@ namespace Dot.Lang {
}
}
/// <summary>
/// Looks up a localized string similar to NTP 标准网络时钟: {0}.
/// </summary>
public static string ServerTime {
get {
return ResourceManager.GetString("ServerTime", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
/// </summary>
@ -311,6 +329,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 +347,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,116 +1,124 @@
<?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:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
<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">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
<value>The input text is empty</value>
</data>
<data name="SearchingFile" xml:space="preserve">
<data name="SearchingFile" xml:space="preserve">
<value>Find files...</value>
</data>
<data name="PathNotFound" xml:space="preserve">
<data name="PathNotFound" xml:space="preserve">
<value>The specified path "{0}" does not exist</value>
</data>
<data name="SearchingFileOK" xml:space="preserve">
<data name="SearchingFileOK" xml:space="preserve">
<value>Find files...OK</value>
</data>
<data name="ShowMessageTemp" xml:space="preserve">
<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">
<data name="Copied" xml:space="preserve">
<value>{0}(copied to clipboard)</value>
</data>
<data name="FileSearchPattern" xml:space="preserve">
<data name="FileSearchPattern" xml:space="preserve">
<value>File wildcards</value>
</data>
<data name="FolderPath" xml:space="preserve">
<data name="FolderPath" xml:space="preserve">
<value>Directory path to be processed</value>
</data>
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<value>Convert newline characters to LF</value>
</data>
<data name="GuidTool" xml:space="preserve">
<data name="GuidTool" xml:space="preserve">
<value>GUID tool</value>
</data>
<data name="UseUppercase" xml:space="preserve">
<data name="UseUppercase" xml:space="preserve">
<value>Use uppercase output</value>
</data>
<data name="RandomPasswordGenerator" xml:space="preserve">
<data name="RandomPasswordGenerator" xml:space="preserve">
<value>Random password generator</value>
</data>
<data name="PwdLength" xml:space="preserve">
<data name="PwdLength" xml:space="preserve">
<value>Password length</value>
</data>
<data name="PwdGenerateTypes" xml:space="preserve">
<data name="PwdGenerateTypes" xml:space="preserve">
<value>BitSet 1[0-9]2[a-z]4[A-Z]8[ascii.0x21-0x2F]</value>
</data>
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<value>Remove line breaks and spaces at the end of the file</value>
</data>
<data name="TrimUtf8Bom" xml:space="preserve">
<data name="TrimUtf8Bom" xml:space="preserve">
<value>Remove the uf8 bom of the file</value>
</data>
<data name="TextTobeProcessed" xml:space="preserve">
<data name="TextTobeProcessed" xml:space="preserve">
<value>Text to be processed (clipboard value is taken by default)</value>
</data>
<data name="PressAnyKey" xml:space="preserve">
<data name="PressAnyKey" xml:space="preserve">
<value>Press any key to continue...</value>
</data>
<data name="ReadOnly" xml:space="preserve">
<data name="ReadOnly" xml:space="preserve">
<value>Read-only mode (only for testing, no actual modification)</value>
</data>
<data name="NoFileToBeProcessed" xml:space="preserve">
<data name="NoFileToBeProcessed" xml:space="preserve">
<value>No documents to be processed</value>
</data>
<data name="TimeoutMillSecs" xml:space="preserve">
<data name="TimeoutMillSecs" xml:space="preserve">
<value>Timeout for connecting to the NTP server (milliseconds)</value>
</data>
<data name="SyncToLocalTime" xml:space="preserve">
<data name="SyncToLocalTime" xml:space="preserve">
<value>Synchronize local time</value>
</data>
<data name="NtpReceiveDone" xml:space="preserve">
<data name="NtpReceiveDone" xml:space="preserve">
<value>Success {0}/{1}, the average value of the clock offset of the machine:{2}ms</value>
</data>
<data name="NtpServerCount" xml:space="preserve">
<data name="NtpServerCount" xml:space="preserve">
<value>{0}/{1} NTP servers</value>
</data>
<data name="NtpCalling" xml:space="preserve">
<data name="NtpCalling" xml:space="preserve">
<value>{0} In communication...</value>
</data>
<data name="LocalTimeOffset" xml:space="preserve">
<data name="LocalTimeOffset" xml:space="preserve">
<value>{0}, local clock offset: {1} ms</value>
</data>
<data name="LocalTimeSyncDone" xml:space="preserve">
<data name="LocalTimeSyncDone" xml:space="preserve">
<value>Local time has been synchronized</value>
</data>
<data name="Server" xml:space="preserve">
<data name="Server" xml:space="preserve">
<value>Server</value>
</data>
<data name="Status" xml:space="preserve">
<data name="Status" xml:space="preserve">
<value>Status</value>
</data>
<data name="LocalClockOffset" xml:space="preserve">
<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

@ -1,251 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Dot.Lang {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Strings {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Strings() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Dot.Lang.Strings", typeof(Strings).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized string similar to 转换换行符为LF.
/// </summary>
public static string ConvertEndOfLineToLF {
get {
return ResourceManager.GetString("ConvertEndOfLineToLF", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to {0}(已复制到剪贴板).
/// </summary>
public static string Copied {
get {
return ResourceManager.GetString("Copied", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 文件通配符.
/// </summary>
public static string FileSearchPattern {
get {
return ResourceManager.GetString("FileSearchPattern", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 要处理的目录路径.
/// </summary>
public static string FolderPath {
get {
return ResourceManager.GetString("FolderPath", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to GUID工具.
/// </summary>
public static string GuidTool {
get {
return ResourceManager.GetString("GuidTool", resourceCulture);
}
}
/// <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>
public static string InputTextIsEmpty {
get {
return ResourceManager.GetString("InputTextIsEmpty", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 没有需要处理的文件.
/// </summary>
public static string NoFileToBeProcessed {
get {
return ResourceManager.GetString("NoFileToBeProcessed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 指定的路径“{0}”不存在.
/// </summary>
public static string PathNotFound {
get {
return ResourceManager.GetString("PathNotFound", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 按下任意键继续....
/// </summary>
public static string PressAnyKey {
get {
return ResourceManager.GetString("PressAnyKey", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to BitSet 1[0-9]2[a-z]4[A-Z]8[ascii.0x21-0x2F].
/// </summary>
public static string PwdGenerateTypes {
get {
return ResourceManager.GetString("PwdGenerateTypes", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 密码长度.
/// </summary>
public static string PwdLength {
get {
return ResourceManager.GetString("PwdLength", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 随机密码生成器.
/// </summary>
public static string RandomPasswordGenerator {
get {
return ResourceManager.GetString("RandomPasswordGenerator", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 只读模式(仅做测试,不实际修改).
/// </summary>
public static string ReadOnly {
get {
return ResourceManager.GetString("ReadOnly", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 移除文件尾部换行和空格.
/// </summary>
public static string RemoveTrailingWhiteSpaces {
get {
return ResourceManager.GetString("RemoveTrailingWhiteSpaces", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 查找文件....
/// </summary>
public static string SearchingFile {
get {
return ResourceManager.GetString("SearchingFile", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to {0} 个文件.
/// </summary>
public static string SearchingFileOK {
get {
return ResourceManager.GetString("SearchingFileOK", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
/// </summary>
public static string ShowMessageTemp {
get {
return ResourceManager.GetString("ShowMessageTemp", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 要处理的文本(默认取取剪贴板值).
/// </summary>
public static string TextTobeProcessed {
get {
return ResourceManager.GetString("TextTobeProcessed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 移除文件的uf8 bom.
/// </summary>
public static string TrimUtf8Bom {
get {
return ResourceManager.GetString("TrimUtf8Bom", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to 使用大写输出.
/// </summary>
public static string UseUppercase {
get {
return ResourceManager.GetString("UseUppercase", resourceCulture);
}
}
}
}

View File

@ -1,86 +0,0 @@
<?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:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
<value>The input text is empty</value>
</data>
<data name="SearchingFile" xml:space="preserve">
<value>Find files...</value>
</data>
<data name="PathNotFound" xml:space="preserve">
<value>The specified path "{0}" does not exist</value>
</data>
<data name="SearchingFileOK" xml:space="preserve">
<value>Find files...OK</value>
</data>
<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>
<data name="FileSearchPattern" xml:space="preserve">
<value>File wildcards</value>
</data>
<data name="FolderPath" xml:space="preserve">
<value>Directory path to be processed</value>
</data>
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<value>Convert newline characters to LF</value>
</data>
<data name="GuidTool" xml:space="preserve">
<value>GUID tool</value>
</data>
<data name="UseUppercase" xml:space="preserve">
<value>Use uppercase output</value>
</data>
<data name="RandomPasswordGenerator" xml:space="preserve">
<value>Random password generator</value>
</data>
<data name="PwdLength" xml:space="preserve">
<value>Password length</value>
</data>
<data name="PwdGenerateTypes" xml:space="preserve">
<value>BitSet 1[0-9]2[a-z]4[A-Z]8[ascii.0x21-0x2F]</value>
</data>
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<value>Remove line breaks and spaces at the end of the file</value>
</data>
<data name="TrimUtf8Bom" xml:space="preserve">
<value>Remove the uf8 bom of the file</value>
</data>
<data name="TextTobeProcessed" xml:space="preserve">
<value>Text to be processed (clipboard value is taken by default)</value>
</data>
<data name="PressAnyKey" xml:space="preserve">
<value>Press any key to continue...</value>
</data>
<data name="ReadOnly" xml:space="preserve">
<value>Read-only mode (only for testing, no actual modification)</value>
</data>
<data name="NoFileToBeProcessed" xml:space="preserve">
<value>No documents to be processed</value>
</data>
</root>

View File

@ -1,94 +0,0 @@
<?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:element name="root" msdata:IsDataSet="true">
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="InputTextIsEmpty" xml:space="preserve">
<value>输入文本为空</value>
</data>
<data name="SearchingFile" xml:space="preserve">
<value>查找文件...</value>
</data>
<data name="PathNotFound" xml:space="preserve">
<value>指定的路径“{0}”不存在</value>
</data>
<data name="SearchingFileOK" xml:space="preserve">
<value>{0} 个文件</value>
</data>
<data name="ShowMessageTemp" xml:space="preserve">
<value>已读取:{0}/{1},处理:{2},跳过:{3}</value>
</data>
<data name="HelpForText" xml:space="preserve">
<value>文本编码工具</value>
</data>
<data name="TextTobeProcessed" xml:space="preserve">
<value>要处理的文本(默认取取剪贴板值)</value>
</data>
<data name="Copied" xml:space="preserve">
<value>{0}(已复制到剪贴板)</value>
</data>
<data name="FileSearchPattern" xml:space="preserve">
<value>文件通配符</value>
</data>
<data name="FolderPath" xml:space="preserve">
<value>要处理的目录路径</value>
</data>
<data name="ConvertEndOfLineToLF" xml:space="preserve">
<value>转换换行符为LF</value>
</data>
<data name="GuidTool" xml:space="preserve">
<value>GUID工具</value>
</data>
<data name="UseUppercase" xml:space="preserve">
<value>使用大写输出</value>
</data>
<data name="RandomPasswordGenerator" xml:space="preserve">
<value>随机密码生成器</value>
</data>
<data name="PwdLength" xml:space="preserve">
<value>密码长度</value>
</data>
<data name="PwdGenerateTypes" xml:space="preserve">
<value>BitSet 1[0-9]2[a-z]4[A-Z]8[ascii.0x21-0x2F]</value>
</data>
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
<value>移除文件尾部换行和空格</value>
</data>
<data name="TrimUtf8Bom" xml:space="preserve">
<value>移除文件的uf8 bom</value>
</data>
<data name="PressAnyKey" xml:space="preserve">
<value>按下任意键继续...</value>
</data>
<data name="ReadOnly" xml:space="preserve">
<value>只读模式(仅做测试,不实际修改)</value>
</data>
<data name="NoFileToBeProcessed" 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,3 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net.Sockets;
using System.Runtime.InteropServices;
@ -6,6 +7,13 @@ namespace Dot.Time;
public sealed class Main : Tool<Option>
{
private record Server
{
public int ConsoleRowIndex;
public TimeSpan Offset;
public ServerStatues Status;
}
private enum ServerStatues : byte
{
Ready
@ -27,41 +35,76 @@ public sealed class Main : Tool<Option>
[FieldOffset(0)] public ushort wYear;
}
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
private const int _NTP_PORT = 123;
private readonly char[] _loading = { '-', '\\', '|', '/' };
private int _procedCnt;
private readonly int _serverCnt;
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
private const int _NTP_PORT = 123;
private const string _OUTPUT_TEMP = "{0,-30} {1,20} {2,20}";
private static readonly object _lockObj = new();
private int _procedCnt;
private readonly int _serverCnt;
private readonly Dictionary<string, Server> _serverDictionary;
private readonly string[] _srvAddr = {
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn", "cn.pool.ntp.org"
, "time.pool.aliyun.com", "time1.aliyun.com", "time2.aliyun.com"
, "time3.aliyun.com", "time4.aliyun.com", "time5.aliyun.com"
, "time6.aliyun.com", "time7.aliyun.com", "time1.cloud.tencent.com"
, "time2.cloud.tencent.com", "time3.cloud.tencent.com"
, "time4.cloud.tencent.com", "time5.cloud.tencent.com", "ntp.sjtu.edu.cn"
, "ntp.neu.edu.cn", "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org"
, "0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
, "time3.google.com", "time4.google.com", "time.apple.com", "time1.apple.com"
, "time2.apple.com", "time3.apple.com", "time4.apple.com", "time5.apple.com"
, "time6.apple.com", "time7.apple.com", "time.windows.com", "time.nist.gov"
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "stdtime.gov.hk"
};
private readonly string[] _serverDomains = {
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn"
, "cn.pool.ntp.org", "time.pool.aliyun.com", "time1.aliyun.com"
, "time2.aliyun.com", "time3.aliyun.com", "time4.aliyun.com"
, "time5.aliyun.com", "time6.aliyun.com", "time7.aliyun.com"
, "time1.cloud.tencent.com", "time2.cloud.tencent.com"
, "time3.cloud.tencent.com", "time4.cloud.tencent.com"
, "time5.cloud.tencent.com", "ntp.sjtu.edu.cn", "ntp.neu.edu.cn"
, "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org", "0.pool.ntp.org"
, "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
, "time3.google.com", "time4.google.com", "time.apple.com"
, "time1.apple.com", "time2.apple.com", "time3.apple.com"
, "time4.apple.com", "time5.apple.com", "time6.apple.com"
, "time7.apple.com", "time.windows.com", "time.nist.gov"
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov"
, "stdtime.gov.hk"
};
private int _successCnt;
private readonly Dictionary<string, Server> _srvStatus;
private int _successCnt;
public Main(Option opt) : base(opt)
{
_serverCnt = _serverDomains.Length;
_serverDictionary = _serverDomains.ToDictionary(x => x, _ => new Server { Status = ServerStatues.Ready });
_serverCnt = _srvAddr.Length;
var i = 0;
_srvStatus = _srvAddr.ToDictionary(
x => x, _ => new Server { Status = ServerStatues.Ready, ConsoleRowIndex = ++i });
}
private static void ChangeStatus(KeyValuePair<string, Server> server, ServerStatues status
, TimeSpan offset = default)
{
server.Value.Status = status;
server.Value.Offset = offset;
DrawTextInConsole(0, server.Value.ConsoleRowIndex
, string.Format(_OUTPUT_TEMP, server.Key, server.Value.Status
, status == ServerStatues.Succeed ? server.Value.Offset : string.Empty));
}
private async Task DrawLoading()
{
char[] loading = { '-', '\\', '|', '/' };
var loadingIndex = 0;
while (true) {
if (Volatile.Read(ref _procedCnt) == _serverCnt) break;
await Task.Delay(100);
++loadingIndex;
for (var i = 0; i != _serverCnt; ++i)
DrawTextInConsole(
34, i + 1
, _srvStatus[_srvAddr[i]].Status is ServerStatues.Succeed or ServerStatues.Failed
? " "
: loading[loadingIndex % 4].ToString());
}
Debug.WriteLine(Environment.CurrentManagedThreadId + ":" + DateTime.Now.ToString("O"));
}
private static void DrawTextInConsole(int left, int top, string text)
{
lock (_lockObj) {
Console.SetCursorPosition(left, top);
Console.Write(text);
}
}
@ -102,30 +145,35 @@ public sealed class Main : Tool<Option>
}
}
private async void Printing()
private void PrintTemplate()
{
const string outputTemp = "{0,-30}\t{1}\t{2,20}\t{3,20}";
var rolling = 0;
Console.Clear();
while (true) {
await Task.Delay(100);
Console.SetCursorPosition(0, 0);
var row = //
_serverDictionary.Select(x //
=> string.Format(outputTemp, x.Key
, x.Value.Status == ServerStatues.Connecting
? _loading[++rolling % 4]
: ' ', x.Value.Status
, x.Value.Offset == TimeSpan.Zero
? string.Empty
: x.Value.Offset));
Console.CursorVisible = false;
var row = //
_srvStatus.Select(x //
=> string.Format(_OUTPUT_TEMP, x.Key, x.Value.Status
, x.Value.Offset == TimeSpan.Zero ? string.Empty : x.Value.Offset));
Console.WriteLine(outputTemp, Str.Server, ' ', Str.Status, Str.LocalClockOffset);
Console.WriteLine(string.Join(Environment.NewLine, row));
if (_procedCnt == _serverCnt) break;
Console.WriteLine(_OUTPUT_TEMP, Str.Server, Str.Status, Str.LocalClockOffset);
Console.WriteLine(string.Join(Environment.NewLine, row));
}
private ValueTask ServerHandle(KeyValuePair<string, Server> server)
{
ChangeStatus(server, ServerStatues.Connecting);
var offset = GetNtpOffset(server.Key);
Interlocked.Increment(ref _procedCnt);
if (offset == TimeSpan.Zero) {
ChangeStatus(server, ServerStatues.Failed);
}
else {
Interlocked.Increment(ref _successCnt);
ChangeStatus(server, ServerStatues.Succeed, offset);
}
return ValueTask.CompletedTask;
}
@ -147,48 +195,42 @@ public sealed class Main : Tool<Option>
SetLocalTime(timeToSet);
}
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
public override async Task Run()
{
var tPrinting = Task.Run(Printing);
PrintTemplate();
var tLoading = DrawLoading();
await Parallel.ForEachAsync(_serverDictionary
await Parallel.ForEachAsync(_srvStatus
, new ParallelOptions { MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM }
, (server, _) => {
server.Value.Status = ServerStatues.Connecting;
var offset = GetNtpOffset(server.Key);
, (server, _) => ServerHandle(server));
Interlocked.Increment(ref _procedCnt);
await tLoading;
if (offset == TimeSpan.Zero) {
server.Value.Status = ServerStatues.Failed;
}
else {
server.Value.Status = ServerStatues.Succeed;
Interlocked.Increment(ref _successCnt);
server.Value.Offset = offset;
}
return ValueTask.CompletedTask;
});
tPrinting.Wait();
var avgOffset = TimeSpan.FromTicks((long)_serverDictionary //
var avgOffset = TimeSpan.FromTicks((long)_srvStatus //
.Where(x => x.Value.Status == ServerStatues.Succeed)
.Average(x => x.Value.Offset.Ticks));
Console.SetCursorPosition(0, _serverCnt + 1);
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
if (!Opt.Sync) return;
Console.WriteLine();
if (!Opt.Sync) {
var _ = Task.Run(async () => {
while (true) {
await Task.Delay(100);
Console.SetCursorPosition(0, Console.GetCursorPosition().Top);
Console.Write(Str.ServerTime, (DateTime.Now - avgOffset).ToString("O"));
Console.Write(@", {0}", Str.PressAnyKey);
}
// ReSharper disable once FunctionNeverReturns
});
Console.ReadKey();
return;
}
SetSysteTime(DateTime.Now - avgOffset);
Console.WriteLine(Str.LocalTimeSyncDone);
}
private record Server
{
public TimeSpan Offset;
public ServerStatues Status;
}
}

View File

@ -1,11 +1,11 @@
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))]
public bool Sync { get; set; }
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 3000, ResourceType = typeof(Str))]
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 2000, ResourceType = typeof(Str))]
public int Timeout { get; set; }
}

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>
@ -23,12 +24,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1"/>
<PackageReference Include="NSExt" Version="1.0.6"/>
<PackageReference Include="ShellProgressBar" Version="5.2.0"/>
<PackageReference Include="TextCopy" Version="6.2.0"/>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="NSExt" Version="1.0.6" />
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
<PackageReference Include="TextCopy" Version="6.2.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Lang\Str.resx">
<Generator>PublicResXFileCodeGenerator</Generator>