[update] 已禁用管理员批准模式的警告、窗口截图支持DWM拉伸

This commit is contained in:
Dubi906w 2024-08-03 10:16:27 +08:00
parent 75abf73089
commit 8a1bcc3ffa
3 changed files with 41 additions and 5 deletions

View File

@ -5753,6 +5753,13 @@
</StackPanel>
</Button>
</ui:SimpleStackPanel>
<ui:InfoBar
IsOpen="True"
Name="CannotSwitchToUserPrivNotification"
IsClosable="False"
Severity="Warning"
Title="可能无法切换到普通用户权限"
Message="检测到您禁用了组策略项目:“用户帐户控制:以管理员批准模式运行所有管理员”,这可能会导致无法切换到普通用户权限" />
<Line HorizontalAlignment="Center" X1="0" Y1="0" X2="400" Y2="0" Stroke="#3f3f46"
StrokeThickness="1" Margin="0,4,0,4" />
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">

View File

@ -18,6 +18,7 @@ using Encoder = System.Drawing.Imaging.Encoder;
using OperatingSystem = OSVersionExtension.OperatingSystem;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using System.Management;
using System.Reflection;
using System.Windows.Shapes;
using Path = System.IO.Path;
using Rectangle = System.Drawing.Rectangle;
@ -319,11 +320,10 @@ namespace Ink_Canvas {
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint crKey, out byte bAlpha, out uint dwFlags);
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
[DllImport("user32.dll")]
internal static extern int GetDpiForWindow(IntPtr hWnd);
enum DwmWindowAttribute : uint {
NCRenderingEnabled = 1,
@ -382,6 +382,9 @@ namespace Ink_Canvas {
public RECT RealRect { get; set; }
public Rectangle ContentRect { get; set; }
public IntPtr Handle { get; set; }
public int WindowDPI { get; set; }
public int SystemDPI { get; set; }
public double DPIScale { get; set; }
}
public struct WINDOWPLACEMENT {
@ -426,6 +429,18 @@ namespace Ink_Canvas {
if ((windowLong & 0x00080000) != 0 && (dwFlags & 0x00000002) != 0 && bAlpha == 0) return true; //分层窗口且全透明
DwmGetWindowAttribute(hwnd, (int)DwmWindowAttribute.Cloaked, out bool isCloacked, Marshal.SizeOf(typeof(bool)));
DwmGetWindowAttribute(hwnd, DwmWindowAttribute.ExtendedFrameBounds, out RECT realRect, Marshal.SizeOf(typeof(RECT)));
var pidRes = GetWindowThreadProcessId(hwnd, out uint pid);
if (pid == 0 || pidRes == 0) return true;
var dpiForHwnd = GetDpiForWindow(hwnd);
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
var dpiX = (int)dpiXProperty.GetValue(null, null);
var dpiY = (int)dpiYProperty.GetValue(null, null);
var dpi = (dpiX + dpiY) / 2;
double scale = 1;
if (dpi > dpiForHwnd) { // 说明该应用是win32应用靠DWM的拉伸放大到高DPI
scale = dpi / (double)dpiForHwnd;
}
if (isCloacked) return true;
var icon = GetAppIcon(hwnd);
var length = GetWindowTextLength(hwnd) + 1;
@ -441,7 +456,6 @@ namespace Ink_Canvas {
GetWindowRect(hwnd, out rect);
var w = rect.Width;
var h = rect.Height;
Trace.WriteLine($"x: {realRect.X - rect.X} y: {realRect.Y - rect.Y} w: {realRect.Width} h: {realRect.Height}");
if (w == 0 || h == 0) return true;
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
Graphics memoryGraphics = Graphics.FromImage(bmp);
@ -458,7 +472,11 @@ namespace Ink_Canvas {
Placement = placement,
RealRect = realRect,
Handle = hwnd,
ContentRect = new Rectangle(realRect.X - rect.X, realRect.Y - rect.Y, realRect.Width, realRect.Height),
ContentRect = new Rectangle(realRect.X - rect.X, realRect.Y - rect.Y, (int)Math.Round(
realRect.Width / scale ,0), (int)Math.Round(realRect.Height / scale, 0)),
WindowDPI = dpiForHwnd,
SystemDPI = dpi,
DPIScale = scale
});
memoryGraphics.ReleaseHdc(hdc);
System.GC.Collect();

View File

@ -14,6 +14,7 @@ using System.Windows.Ink;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using File = System.IO.File;
using OperatingSystem = OSVersionExtension.OperatingSystem;
@ -88,6 +89,16 @@ namespace Ink_Canvas {
AdministratorPrivilegeIndicateText.Text = "icc目前以管理员身份运行";
RunAsAdminButton.Visibility = Visibility.Collapsed;
RunAsUserButton.Visibility = Visibility.Visible;
RegistryKey localKey;
if(Environment.Is64BitOperatingSystem)
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
else
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
int LUAValue = (int)(localKey.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows")
.OpenSubKey("CurrentVersion").OpenSubKey("Policies").OpenSubKey("System")
.GetValue("EnableLUA", 1));
CannotSwitchToUserPrivNotification.IsOpen = LUAValue == 0;
} else {
AdministratorPrivilegeIndicateText.Text = "icc目前以非管理员身份运行";
RunAsAdminButton.Visibility = Visibility.Visible;