[update] winform magnification api

This commit is contained in:
Dubi906w 2024-07-31 22:01:48 +08:00
parent 00e045a01a
commit ef222bc89d
7 changed files with 84 additions and 29 deletions

View File

@ -7811,8 +7811,8 @@
<TextBlock Foreground="#fafafa" Text="墨迹与截图的保存路径" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="10">
<TextBox Width="320" x:Name="AutoSavedStrokesLocation" Text="D:\Ink Canvas"
TextWrapping="Wrap"
<TextBox ui:TextBoxHelper.IsDeleteButtonVisible="False" Width="320" x:Name="AutoSavedStrokesLocation" Text="D:\Ink Canvas"
TextWrapping="Wrap" AcceptsTab="False" AcceptsReturn="False"
TextChanged="AutoSavedStrokesLocationTextBox_TextChanged" />
<Button Name="AutoSavedStrokesLocationButton" Content="浏览"
Click="AutoSavedStrokesLocationButton_Click" />
@ -8068,7 +8068,7 @@
<ui:ToggleSwitch OnContent="" OffContent=""
Name="ToggleSwitchScreenshotUsingMagnificationAPI"
IsOn="True" FontFamily="Microsoft YaHei UI"
FontWeight="Bold"/>
FontWeight="Bold" Toggled="ToggleSwitchScreenshotUsingMagnificationAPI_OnToggled"/>
</ui:SimpleStackPanel>
<TextBlock Text="# 使用 Magnification API 和弃用的函数 MagSetImageScalingCallback 实现过滤ICC窗口截图。"
TextWrapping="Wrap" Foreground="#a1a1aa" />
@ -8116,8 +8116,8 @@
<TextBlock Foreground="#fafafa" Text="截图文件文件名" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="10">
<TextBox Width="320" x:Name="ScreenshotFileName" Text='Screenshot-[YYYY]-[MM]-[DD]-[HH]-[mm]-[ss].png'
TextWrapping="NoWrap"
<TextBox ui:TextBoxHelper.IsDeleteButtonVisible="False" Width="320" x:Name="ScreenshotFileName" Text='Screenshot-[YYYY]-[MM]-[DD]-[HH]-[mm]-[ss].png'
TextWrapping="NoWrap" AcceptsTab="False" AcceptsReturn="False"
TextChanged="AutoSavedStrokesLocationTextBox_TextChanged" />
<Button Name="ScreenshotFileNameResetButton" Content="浏览"
Click="AutoSavedStrokesLocationButton_Click" />

View File

@ -8,13 +8,17 @@ using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shell;
using Acornima.Ast;
using OSVersionExtension;
using Vanara.PInvoke;
using static Vanara.PInvoke.Gdi32;
using Color = System.Drawing.Color;
using OperatingSystem = OSVersionExtension.OperatingSystem;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
@ -68,19 +72,25 @@ namespace Ink_Canvas {
if (!Magnification.MagInitialize()) return;
// 創建宿主窗體
var mainWinMag = new Window();
mainWinMag.WindowState = WindowState.Maximized;
mainWinMag.WindowStyle = WindowStyle.None;
mainWinMag.ResizeMode = ResizeMode.NoResize;
mainWinMag.Background = new SolidColorBrush(Colors.Transparent);
mainWinMag.AllowsTransparency = true;
var mainWinMag = new Form();
mainWinMag.Show();
var handle = new HWND(new WindowInteropHelper(mainWinMag).Handle);
var handle = new HWND(mainWinMag.Handle);
User32.SetWindowPos(handle, HWND.HWND_NOTOPMOST, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, User32.SetWindowPosFlags.SWP_HIDEWINDOW); // SWP_HIDEWINDOW
SetWindowLongPtr(handle.DangerousGetHandle(), -20, new IntPtr((int)GetWindowLongPtr(handle.DangerousGetHandle(), -20) | 0x00080000));
Trace.WriteLine(handle);
User32.SetWindowLong(handle, User32.WindowLongFlags.GWL_STYLE,
new IntPtr((int)User32.WindowStyles.WS_SIZEBOX | (int)User32.WindowStyles.WS_SYSMENU | (int)User32.WindowStyles.WS_CLIPCHILDREN | (int)User32.WindowStyles.WS_CAPTION | (int)User32.WindowStyles.WS_MAXIMIZEBOX));
Trace.WriteLine(handle);
var exptr = User32.GetWindowLong(handle, User32.WindowLongFlags.GWL_EXSTYLE);
Trace.WriteLine(exptr);
User32.SetWindowLong(handle, User32.WindowLongFlags.GWL_EXSTYLE,
new IntPtr(exptr |
(int)User32.WindowStylesEx.WS_EX_LAYERED | (int)User32.WindowStylesEx.WS_EX_TOPMOST));
Trace.WriteLine(handle);
User32.SetLayeredWindowAttributes(handle,0, 255, User32.LayeredWindowAttributes.LWA_ALPHA);
Trace.WriteLine(handle);
// 創建放大鏡窗體使用Win32方法
var hwndMag = User32.CreateWindow(Magnification.WC_MAGNIFIER, "ICCMagnifierWindow",
User32.WindowStyles.WS_CHILD | User32.WindowStyles.WS_VISIBLE, 0, 0,
@ -115,8 +125,8 @@ namespace Ink_Canvas {
bmp.Save(savePath + @"\" + DateTime.Now.ToString("u").Replace(':', '-') + ".png", ImageFormat.Png);
// 關閉宿主窗體
Magnification.MagUninitialize();
mainWinMag.Close();
//Magnification.MagUninitialize();
//mainWinMag.Close();
}
public unsafe void SaveScreenshotToDesktopByMagnificationAPI(bool isExcludeMode, HWND[] hwndsList,
@ -248,7 +258,7 @@ namespace Ink_Canvas {
return icn;
}
public struct WindowInformation {
public class WindowInformation {
public string Title { get; set; }
public Bitmap WindowBitmap { get; set; }
public Icon AppIcon { get; set; }
@ -330,12 +340,20 @@ namespace Ink_Canvas {
Placement = placement,
});
memoryGraphics.ReleaseHdc(hdc);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
return true;
}),
IntPtr.Zero)) return new WindowInformation[] { };
return windows.ToArray();
}
public async Task<WindowInformation[]> GetAllWindowsAsync(HWND[] excludedHwnds)
{
var windows = await Task.Run(() => GetAllWindows(excludedHwnds));
return windows;
}
#endregion
private void SaveScreenShotToDesktop() {

View File

@ -2047,7 +2047,9 @@ namespace Ink_Canvas {
#region Screenshot
private void ToggleSwitchScreenshotUsingMagnificationAPI_OnToggled(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
Settings.Snapshot.ScreenshotUsingMagnificationAPI = ToggleSwitchScreenshotUsingMagnificationAPI.IsOn;
SaveSettingsToFile();
}
#endregion

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -189,7 +190,7 @@ namespace Ink_Canvas.Popups
private WindowScreenshotGridWindow _screenshotGridWindow = null;
private void IconMouseUp(object sender, MouseButtonEventArgs e) {
private async void IconMouseUp(object sender, MouseButtonEventArgs e) {
if (lastDownIcon == null) return;
IconMouseLeave(sender, null);
var index = Array.IndexOf(iconList, (Border)sender);
@ -197,13 +198,20 @@ namespace Ink_Canvas.Popups
UpdateModeIconSelection();
if (selectedMode == 1) {
_screenshotGridWindow = new WindowScreenshotGridWindow(mainWindow.GetAllWindows(new HWND[] {
try {
MainWindow.WindowInformation[] windows = await mainWindow.GetAllWindowsAsync(new HWND[] {
new HWND(new WindowInteropHelper(this).Handle), new HWND(new WindowInteropHelper(mainWindow).Handle)
}), mainWindow);
});
_screenshotGridWindow = new WindowScreenshotGridWindow(windows, mainWindow);
_screenshotGridWindow.Show();
} else if (_screenshotGridWindow != null) {
}
catch (TaskCanceledException) {}
catch (Exception ex) {}
} else {
try {
_screenshotGridWindow.Close();
_screenshotGridWindow = null;
} catch (Exception ex) { }
}
}

View File

@ -17,9 +17,12 @@
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<modern:SimpleStackPanel Width="164" Height="164" Tag="{Binding}" MouseUp="WindowItem_MouseUp">
<Image Width="148" Height="148" Source="{Binding Bitmap}"/>
<TextBlock FontSize="16" Foreground="White" Text="{Binding Title}"/>
<modern:SimpleStackPanel Tag="{Binding}" MouseUp="WindowItem_MouseUp">
<Image Width="256" Source="{Binding Bitmap}"/>
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Width="36" Height="36" Source="{Binding Icon}"/>
<TextBlock VerticalAlignment="Center" Margin="8,0,0,0" FontSize="16" Foreground="White" Text="{Binding Title}"/>
</StackPanel>
</modern:SimpleStackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>

View File

@ -13,6 +13,7 @@ using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
@ -43,9 +44,20 @@ namespace Ink_Canvas.Popups
}
}
private static ImageSource IconToImageSource(Icon icon)
{
ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
private class Win {
public string Title { get; set; }
public BitmapImage Bitmap { get; set; }
public ImageSource Icon { get; set; }
public HWND Handle { get; set; }
public Bitmap OriginBitmap { get; set; }
}
@ -56,7 +68,6 @@ namespace Ink_Canvas.Popups
public WindowScreenshotGridWindow(MainWindow.WindowInformation[] wins, MainWindow mainWindow) {
InitializeComponent();
_windows.Clear();
WindowsItemControl.ItemsSource = _windows;
foreach (var windowInformation in wins) {
_windows.Add(new Win() {
@ -64,6 +75,7 @@ namespace Ink_Canvas.Popups
Bitmap = BitmapToImageSource(windowInformation.WindowBitmap),
Handle = windowInformation.hwnd,
OriginBitmap = windowInformation.WindowBitmap,
Icon = IconToImageSource(windowInformation.AppIcon)
});
Trace.WriteLine(windowInformation.Title);
}
@ -71,6 +83,11 @@ namespace Ink_Canvas.Popups
this.mainWindow = mainWindow;
}
protected override void OnClosed(EventArgs e) {
base.OnClosed(e);
_windows.Clear();
}
private void WindowItem_MouseUp(object sender, MouseButtonEventArgs e) {
var item = ((SimpleStackPanel)sender).Tag as Win;
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

View File

@ -23,6 +23,13 @@ namespace Ink_Canvas
public Startup Startup { get; set; } = new Startup();
[JsonProperty("randSettings")]
public RandSettings RandSettings { get; set; } = new RandSettings();
[JsonProperty("snapshot")]
public Snapshot Snapshot { get; set; } = new Snapshot();
}
public class Snapshot {
[JsonProperty("usingMagnificationAPI")]
public bool ScreenshotUsingMagnificationAPI { get; set; } = false;
}
public class Canvas