moved to MVVM architecture
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CursorLang.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Win32 API для окна-подсказки: стили, позиционирование у курсора
|
||||
/// и масштаб монитора, на котором курсор находится.
|
||||
/// </summary>
|
||||
internal static class PopupWindowNative
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct Point
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetCursorPos(out Point lpPoint);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
|
||||
int X, int Y, int cx, int cy, uint uFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr MonitorFromPoint(Point pt, uint dwFlags);
|
||||
|
||||
[DllImport("shcore.dll")]
|
||||
private static extern int GetDpiForMonitor(IntPtr hMonitor, int dpiType, out uint dpiX, out uint dpiY);
|
||||
|
||||
private const int GWL_EXSTYLE = -20;
|
||||
// Окно не забирает фокус у активного приложения
|
||||
private const int WS_EX_NOACTIVATE = 0x08000000;
|
||||
// И не попадает в Alt+Tab
|
||||
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
||||
|
||||
private const uint SWP_NOSIZE = 0x0001;
|
||||
private const uint SWP_NOZORDER = 0x0004;
|
||||
private const uint SWP_NOACTIVATE = 0x0010;
|
||||
|
||||
private const uint MONITOR_DEFAULTTONEAREST = 2;
|
||||
private const int MDT_EFFECTIVE_DPI = 0;
|
||||
|
||||
internal static Point GetCursorPosition()
|
||||
{
|
||||
GetCursorPos(out Point cursor);
|
||||
return cursor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Подсказка всплывает поверх чужих приложений, поэтому она не должна
|
||||
/// ни активироваться сама, ни отбирать фокус ввода у активного окна.
|
||||
/// </summary>
|
||||
internal static void MakePassive(IntPtr hWnd)
|
||||
{
|
||||
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
|
||||
SetWindowLong(hWnd, GWL_EXSTYLE, exStyle | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Двигает окно в точку экрана, не меняя размер и порядок окон.
|
||||
/// Координаты — физические пиксели: у мониторов разный масштаб, а
|
||||
/// Window.Left/Top пересчитываются по DPI того монитора, где окно сейчас,
|
||||
/// и на соседнем мониторе дают промах.
|
||||
/// </summary>
|
||||
internal static void MoveTo(IntPtr hWnd, int x, int y)
|
||||
{
|
||||
SetWindowPos(hWnd, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
}
|
||||
|
||||
/// <summary>Масштаб монитора, на котором находится точка (1.0 при 96 DPI).</summary>
|
||||
internal static double GetScaleAt(Point point)
|
||||
{
|
||||
IntPtr monitor = MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);
|
||||
if (GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, out uint dpiX, out _) < 0)
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
return dpiX / 96.0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user