PrivacyGlass/PrivacyGlass/Native.cs
2025-12-09 21:43:34 -05:00

158 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Runtime.InteropServices;
namespace PrivacyGlass
{
internal static class Native
{
[Flags]
private enum DwmBlurBehindFlags : uint
{
DWM_BB_ENABLE = 0x00000001,
DWM_BB_BLURREGION = 0x00000002,
DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004
}
[StructLayout(LayoutKind.Sequential)]
private struct DWM_BLURBEHIND
{
public DwmBlurBehindFlags dwFlags;
public bool fEnable;
public IntPtr hRgnBlur;
public bool fTransitionOnMaximized;
}
[DllImport("dwmapi.dll")]
private static extern int DwmEnableBlurBehindWindow(
IntPtr hWnd,
ref DWM_BLURBEHIND pBlurBehind);
// Window styles
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
// Extended styles
public const int WS_EX_TOPMOST = 0x00000008;
public const int WS_EX_LAYERED = 0x00080000;
public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = -20;
// ----- Magnification API -----
[DllImport("Magnification.dll", SetLastError = true)]
internal static extern bool MagInitialize();
[DllImport("Magnification.dll", SetLastError = true)]
internal static extern bool MagUninitialize();
[DllImport("Magnification.dll", SetLastError = true)]
internal static extern bool MagSetWindowSource(IntPtr hwnd, RECT rect);
[DllImport("Magnification.dll", SetLastError = true)]
internal static extern bool MagSetColorEffect(IntPtr hwnd, ref MAGCOLOREFFECT effect);
// ----- User32 stuff -----
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr CreateWindowEx(
int dwExStyle,
string lpClassName,
string lpWindowName,
int dwStyle,
int x, int y,
int nWidth, int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[DllImport("user32.dll")]
internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
internal static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int X, int Y,
int cx, int cy,
uint uFlags);
// structures
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MAGCOLOREFFECT
{
// 5x5 color matrix (row-major)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]
public float[] transform;
}
/// <summary>
/// Strong privacy colour effect: desaturated + darkened.
/// Not a geometric blur, but kills readability hard.
/// </summary>
internal static MAGCOLOREFFECT CreatePrivacyEffect()
{
var fx = new MAGCOLOREFFECT
{
transform = new float[25]
};
// Well map R,G,B all to the same gray value and darken it.
// Gray = (R+G+B)/3 * factor
float factor = 0.25f; // 0..1, lower = darker
float g = factor / 3f;
// Row 0: R'
fx.transform[0] = g; // R
fx.transform[1] = g; // G
fx.transform[2] = g; // B
// Row 1: G'
fx.transform[5] = g;
fx.transform[6] = g;
fx.transform[7] = g;
// Row 2: B'
fx.transform[10] = g;
fx.transform[11] = g;
fx.transform[12] = g;
// Row 3: A' = A
fx.transform[18] = 1.0f;
// Row 4: w' (bias) leave as identity
fx.transform[24] = 1.0f;
return fx;
}
[DllImport("Magnification.dll", SetLastError = true)]
internal static extern bool MagSetWindowTransform(
IntPtr hwnd,
ref MAGTRANSFORM pTransform);
[StructLayout(LayoutKind.Sequential)]
internal struct MAGTRANSFORM
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
public float[] v; // 3x3 matrix
}
}
}