* <fix> 隐藏控制台窗口,避免捕获到截屏
This commit is contained in:
nsnail 2022-12-05 16:28:09 +08:00 committed by GitHub
parent f2e8b8b891
commit 1ba7f3fcfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 62 deletions

View File

@ -11,9 +11,15 @@ public static class Win32
private const string _USER32_DLL = "user32.dll";
public const int SW_HIDE = 0;
[DllImport(_USER32_DLL)]
public static extern nint CallNextHookEx(nint hhk, int nCode, nint wParam, nint lParam);
[DllImport(_KERNEL32_DLL)]
public static extern nint GetConsoleWindow();
[DllImport(_USER32_DLL)]
public static extern nint GetDesktopWindow();
@ -34,6 +40,9 @@ public static class Win32
[DllImport(_USER32_DLL)]
public static extern nint SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, nint hMod, uint dwThreadId);
[DllImport(_USER32_DLL)]
public static extern bool ShowWindow(nint hWnd, int nCmdShow);
[DllImport(_USER32_DLL)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(nint hhk);

View File

@ -4,8 +4,8 @@ namespace Dot.Color;
public class WinInfo : Form
{
private const int _WINDOW_SIZE = 480;
private const int _ZOOM_RATE = 16;
private const int _WINDOW_SIZE = 480; //窗口大小
private const int _ZOOM_RATE = 16; //缩放倍率
private bool _disposed;
private readonly Graphics _graphics;
private readonly PictureBox _pbox;
@ -24,7 +24,7 @@ public class WinInfo : Form
_pbox.Size = Size;
_pbox.Image = new Bitmap(_WINDOW_SIZE, _WINDOW_SIZE);
_graphics = Graphics.FromImage(_pbox.Image);
_graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
_graphics.InterpolationMode = InterpolationMode.NearestNeighbor; //指定最临近插值法,禁止平滑缩放(模糊)
_graphics.CompositingQuality = CompositingQuality.HighQuality;
_graphics.SmoothingMode = SmoothingMode.None;
Controls.Add(_pbox);
@ -52,19 +52,25 @@ public class WinInfo : Form
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 //
, x - copySize.Width / 2 // 左移x使光标位置居中
, y - copySize.Height / 2 // 上移y使光标位置居中
, copySize.Width, copySize.Height, GraphicsUnit.Pixel);
using var pen = new Pen(System.Drawing.Color.Aqua);
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);
, new Font(FontFamily.GenericSerif, 10) //
, Brushes.White, 0, _WINDOW_SIZE - 20);
// 触发重绘
_pbox.Refresh();
}
}

View File

@ -6,15 +6,18 @@ public class WinMain : Form
{
private readonly Bitmap _bmp;
private bool _disposed;
private readonly WinInfo _winInfo = new();
private readonly WinInfo _winInfo = new(); //小图窗口
public WinMain()
{
// 隐藏控制台窗口,避免捕获到截屏
Win32.ShowWindow(Win32.GetConsoleWindow(), Win32.SW_HIDE);
FormBorderStyle = FormBorderStyle.None;
Size = Screen.PrimaryScreen!.Bounds.Size;
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
Opacity = 0.01d;
Opacity = 0.01d; //主窗体加载截图过程设置为透明避免闪烁
_bmp = new Bitmap(Size.Width, Size.Height);
using var g = Graphics.FromImage(_bmp);
g.CopyFromScreen(0, 0, 0, 0, Size);
@ -57,6 +60,7 @@ public class WinMain : Form
protected override void OnMouseMove(MouseEventArgs e)
{
// 移动鼠标时更新小图窗口
_winInfo.UpdateImage(_bmp, e.X, e.Y);
}

View File

@ -1,6 +1,7 @@
<?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">

View File

@ -24,10 +24,10 @@
</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>