improve:插入图片
This commit is contained in:
@@ -5335,6 +5335,94 @@
|
||||
</ui:SimpleStackPanel>
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
<!-- Screenshot Options Panel -->
|
||||
<Border ClipToBounds="True" Name="ScreenshotOptionsPanel" Visibility="Collapsed"
|
||||
Margin="-80,-90,-14,33.5"
|
||||
CornerRadius="5" Background="#fafafa" Opacity="1"
|
||||
BorderThickness="1"
|
||||
BorderBrush="#2563eb"
|
||||
Panel.ZIndex="1000">
|
||||
<ui:SimpleStackPanel Margin="0">
|
||||
<Border BorderBrush="#1e3a8a" BorderThickness="0,0,0,1"
|
||||
CornerRadius="5,5,0,0" Background="#2563eb"
|
||||
Padding="8,4">
|
||||
<ui:SimpleStackPanel Orientation="Horizontal"
|
||||
HorizontalAlignment="Stretch">
|
||||
<TextBlock Text="截图选项" Foreground="White"
|
||||
FontSize="10" FontWeight="Bold"
|
||||
VerticalAlignment="Center" />
|
||||
<Image Margin="8,0,0,0"
|
||||
Source="/Resources/new-icons/close-white.png"
|
||||
RenderOptions.BitmapScalingMode="HighQuality"
|
||||
Height="12" Width="12"
|
||||
MouseDown="Border_MouseDown"
|
||||
MouseUp="CloseScreenshotOptionsPanel_MouseUp" />
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
<ui:SimpleStackPanel Margin="6,4,6,4" Spacing="2">
|
||||
<!-- Full Screen Screenshot Option -->
|
||||
<Border MouseDown="Border_MouseDown" MouseUp="FullScreenScreenshot_MouseUp"
|
||||
Background="Transparent" CornerRadius="3" Padding="6,4">
|
||||
<Border.Style>
|
||||
<Style TargetType="Border">
|
||||
<Setter Property="Background" Value="Transparent"/>
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Background" Value="#f0f9ff"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Border.Style>
|
||||
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="6">
|
||||
<Image Height="16" Width="16">
|
||||
<Image.Source>
|
||||
<DrawingImage>
|
||||
<DrawingImage.Drawing>
|
||||
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
|
||||
<GeometryDrawing Brush="#2563eb"
|
||||
Geometry="F0 M24,24z M0,0z M9,3C8.46957,3 7.96086,3.21071 7.58579,3.58579 7.21071,3.96086 7,4.46957 7,5 7,5.26522 6.89464,5.51957 6.70711,5.70711 6.51957,5.89464 6.26522,6 6,6L5,6C4.20435,6 3.44129,6.31607 2.87868,6.87868 2.31607,7.44129 2,8.20435 2,9L2,18C2,18.7956 2.31607,19.5587 2.87868,20.1213 3.44129,20.6839 4.20435,21 5,21L19,21C19.7957,21 20.5587,20.6839 21.1213,20.1213 21.6839,19.5587 22,18.7957 22,18L22,9C22,8.20435 21.6839,7.44129 21.1213,6.87868 20.5587,6.31607 19.7957,6 19,6L18,6C17.7348,6 17.4804,5.89464 17.2929,5.70711 17.1054,5.51957 17,5.26522 17,5 17,4.46957 16.7893,3.96086 16.4142,3.58579 16.0391,3.21071 15.5304,3 15,3L9,3z M9,5L15,5C15,5.79565 15.3161,6.55871 15.8787,7.12132 16.4413,7.68393 17.2044,8 18,8L19,8C19.2652,8 19.5196,8.10536 19.7071,8.29289 19.8946,8.48043 20,8.73478 20,9L20,18C20,18.2652 19.8946,18.5196 19.7071,18.7071 19.5196,18.8946 19.2652,19 19,19L5,19C4.73478,19 4.48043,18.8946 4.29289,18.7071 4.10536,18.5196 4,18.2652 4,18L4,9C4,8.73478 4.10536,8.48043 4.29289,8.29289 4.48043,8.10536 4.73478,8 5,8L6,8C6.79565,8 7.55871,7.68393 8.12132,7.12132 8.68393,6.55871 9,5.79565 9,5z M12,9C10.9391,9 9.92172,9.42143 9.17157,10.1716 8.42143,10.9217 8,11.9391 8,13 8,14.0609 8.42143,15.0783 9.17157,15.8284 9.92172,16.5786 10.9391,17 12,17 13.0609,17 14.0783,16.5786 14.8284,15.8284 15.5786,15.0783 16,14.0609 16,13 16,11.9391 15.5786,10.9217 14.8284,10.1716 14.0783,9.42143 13.0609,9 12,9z M10.5858,11.5858C10.9609,11.2107 11.4696,11 12,11 12.5304,11 13.0391,11.2107 13.4142,11.5858 13.7893,11.9609 14,12.4696 14,13 14,13.5304 13.7893,14.0391 13.4142,14.4142 13.0391,14.7893 12.5304,15 12,15 11.4696,15 10.9609,14.7893 10.5858,14.4142 10.2107,14.0391 10,13.5304 10,13 10,12.4696 10.2107,11.9609 10.5858,11.5858z" />
|
||||
</DrawingGroup>
|
||||
</DrawingImage.Drawing>
|
||||
</DrawingImage>
|
||||
</Image.Source>
|
||||
</Image>
|
||||
<TextBlock Text="全屏截图" FontSize="11" FontWeight="SemiBold"
|
||||
Foreground="#1f2937" VerticalAlignment="Center"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
<!-- Area Screenshot Option -->
|
||||
<Border MouseDown="Border_MouseDown" MouseUp="AreaScreenshot_MouseUp"
|
||||
Background="Transparent" CornerRadius="3" Padding="6,4">
|
||||
<Border.Style>
|
||||
<Style TargetType="Border">
|
||||
<Setter Property="Background" Value="Transparent"/>
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Background" Value="#f0f9ff"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Border.Style>
|
||||
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="6">
|
||||
<Image Height="16" Width="16">
|
||||
<Image.Source>
|
||||
<DrawingImage>
|
||||
<DrawingImage.Drawing>
|
||||
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
|
||||
<GeometryDrawing Brush="#2563eb"
|
||||
Geometry="F0 M24,24z M0,0z M19,3H5C3.9,3 3,3.9 3,5v14c0,1.1 0.9,2 2,2h14c1.1,0 2-0.9 2-2V5C21,3.9 20.1,3 19,3zM19,19H5V5h14V19z M17,7c-1.1,0-2,0.9-2,2s0.9,2 2,2 2-0.9 2-2S18.1,7 17,7zM7,17l2.5-3.01 1.96,2.36 2.54-3.21L17,17H7z" />
|
||||
</DrawingGroup>
|
||||
</DrawingImage.Drawing>
|
||||
</DrawingImage>
|
||||
</Image.Source>
|
||||
</Image>
|
||||
<TextBlock Text="任意截图" FontSize="11" FontWeight="SemiBold"
|
||||
Foreground="#1f2937" VerticalAlignment="Center"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
</ui:SimpleStackPanel>
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</Border>
|
||||
<Border Width="60" Height="50" MouseDown="Border_MouseDown"
|
||||
|
||||
@@ -209,6 +209,7 @@ namespace Ink_Canvas {
|
||||
BoardBorderLeftPageListView.Visibility = Visibility.Collapsed;
|
||||
BoardBorderRightPageListView.Visibility = Visibility.Collapsed;
|
||||
BoardImageOptionsPanel.Visibility = Visibility.Collapsed;
|
||||
ScreenshotOptionsPanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -281,6 +282,7 @@ namespace Ink_Canvas {
|
||||
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
|
||||
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
|
||||
AnimationsHelper.HideWithSlideAndFade(BoardImageOptionsPanel);
|
||||
AnimationsHelper.HideWithSlideAndFade(ScreenshotOptionsPanel);
|
||||
|
||||
// 隐藏背景设置面板
|
||||
var bgPalette = LogicalTreeHelper.FindLogicalNode(this, "BackgroundPalette") as Border;
|
||||
@@ -733,9 +735,57 @@ namespace Ink_Canvas {
|
||||
private async void SymbolIconScreenshot_MouseUp(object sender, MouseButtonEventArgs e) {
|
||||
HideSubPanelsImmediately();
|
||||
await Task.Delay(50);
|
||||
|
||||
// 显示截图选项面板
|
||||
ShowScreenshotOptionsPanel();
|
||||
}
|
||||
|
||||
private void ShowScreenshotOptionsPanel() {
|
||||
// 隐藏其他面板
|
||||
HideSubPanelsImmediately();
|
||||
|
||||
// 显示截图选项面板
|
||||
if (ScreenshotOptionsPanel.Visibility == Visibility.Collapsed) {
|
||||
// 添加调试信息
|
||||
LogHelper.WriteLogToFile("显示截图选项面板", LogHelper.LogType.Info);
|
||||
|
||||
// 直接设置可见性,不使用动画
|
||||
ScreenshotOptionsPanel.Visibility = Visibility.Visible;
|
||||
|
||||
// 显示通知确认面板已显示
|
||||
ShowNotification("截图选项面板已显示");
|
||||
} else {
|
||||
LogHelper.WriteLogToFile("隐藏截图选项面板", LogHelper.LogType.Info);
|
||||
ScreenshotOptionsPanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseScreenshotOptionsPanel_MouseUp(object sender, MouseButtonEventArgs e) {
|
||||
AnimationsHelper.HideWithSlideAndFade(ScreenshotOptionsPanel);
|
||||
}
|
||||
|
||||
private async void FullScreenScreenshot_MouseUp(object sender, MouseButtonEventArgs e) {
|
||||
// 隐藏选项面板
|
||||
AnimationsHelper.HideWithSlideAndFade(ScreenshotOptionsPanel);
|
||||
|
||||
// 等待面板隐藏
|
||||
await Task.Delay(100);
|
||||
|
||||
// 执行全屏截图
|
||||
SaveScreenShotToDesktop();
|
||||
}
|
||||
|
||||
private async void AreaScreenshot_MouseUp(object sender, MouseButtonEventArgs e) {
|
||||
// 隐藏选项面板
|
||||
AnimationsHelper.HideWithSlideAndFade(ScreenshotOptionsPanel);
|
||||
|
||||
// 等待面板隐藏
|
||||
await Task.Delay(100);
|
||||
|
||||
// 执行区域截图
|
||||
await CaptureScreenshotToClipboard();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void ImageCountdownTimer_MouseUp(object sender, MouseButtonEventArgs e) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,6 +15,19 @@ using Clipboard = System.Windows.Clipboard;
|
||||
using Size = System.Drawing.Size;
|
||||
|
||||
namespace Ink_Canvas {
|
||||
// 截图结果结构体
|
||||
public struct ScreenshotResult
|
||||
{
|
||||
public System.Drawing.Rectangle Area;
|
||||
public List<System.Windows.Point> Path;
|
||||
|
||||
public ScreenshotResult(System.Drawing.Rectangle area, List<System.Windows.Point> path = null)
|
||||
{
|
||||
Area = area;
|
||||
Path = path;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class MainWindow : Window {
|
||||
private void SaveScreenShot(bool isHideNotification, string fileName = null) {
|
||||
var savePath = Settings.Automation.IsSaveScreenshotsInDateFolders
|
||||
@@ -99,20 +114,28 @@ namespace Ink_Canvas {
|
||||
await Task.Delay(200);
|
||||
|
||||
// 启动区域选择截图
|
||||
var selectedArea = await ShowScreenshotSelector();
|
||||
var screenshotResult = await ShowScreenshotSelector();
|
||||
|
||||
// 恢复窗口显示
|
||||
this.Visibility = originalVisibility;
|
||||
|
||||
if (selectedArea.HasValue && selectedArea.Value.Width > 0 && selectedArea.Value.Height > 0)
|
||||
if (screenshotResult.HasValue && screenshotResult.Value.Area.Width > 0 && screenshotResult.Value.Area.Height > 0)
|
||||
{
|
||||
// 截取选定区域
|
||||
using (var bitmap = CaptureScreenArea(selectedArea.Value))
|
||||
using (var originalBitmap = CaptureScreenArea(screenshotResult.Value.Area))
|
||||
{
|
||||
if (bitmap != null)
|
||||
if (originalBitmap != null)
|
||||
{
|
||||
Bitmap finalBitmap = originalBitmap;
|
||||
|
||||
// 如果有路径信息,应用形状遮罩
|
||||
if (screenshotResult.Value.Path != null && screenshotResult.Value.Path.Count > 0)
|
||||
{
|
||||
finalBitmap = ApplyShapeMask(originalBitmap, screenshotResult.Value.Path, screenshotResult.Value.Area);
|
||||
}
|
||||
|
||||
// 将截图复制到剪贴板
|
||||
CopyBitmapToClipboard(bitmap);
|
||||
CopyBitmapToClipboard(finalBitmap);
|
||||
|
||||
// 等待窗口完全显示后自动粘贴
|
||||
await Task.Delay(100);
|
||||
@@ -132,9 +155,9 @@ namespace Ink_Canvas {
|
||||
}
|
||||
|
||||
// 显示截图区域选择器
|
||||
private async Task<Rectangle?> ShowScreenshotSelector()
|
||||
private async Task<ScreenshotResult?> ShowScreenshotSelector()
|
||||
{
|
||||
Rectangle? selectedArea = null;
|
||||
ScreenshotResult? result = null;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -143,7 +166,10 @@ namespace Ink_Canvas {
|
||||
var selectorWindow = new ScreenshotSelectorWindow();
|
||||
if (selectorWindow.ShowDialog() == true)
|
||||
{
|
||||
selectedArea = selectorWindow.SelectedArea;
|
||||
result = new ScreenshotResult(
|
||||
selectorWindow.SelectedArea.Value,
|
||||
selectorWindow.SelectedPath
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -152,11 +178,11 @@ namespace Ink_Canvas {
|
||||
LogHelper.WriteLogToFile($"显示截图选择器失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
|
||||
return selectedArea;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 截取指定屏幕区域
|
||||
private Bitmap CaptureScreenArea(Rectangle area)
|
||||
private Bitmap CaptureScreenArea(System.Drawing.Rectangle area)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -225,6 +251,62 @@ namespace Ink_Canvas {
|
||||
}
|
||||
}
|
||||
|
||||
// 应用形状遮罩到截图
|
||||
private Bitmap ApplyShapeMask(Bitmap bitmap, List<System.Windows.Point> path, System.Drawing.Rectangle area)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建遮罩位图
|
||||
using (var maskBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb))
|
||||
using (var maskGraphics = Graphics.FromImage(maskBitmap))
|
||||
{
|
||||
// 设置高质量渲染
|
||||
maskGraphics.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
maskGraphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
|
||||
// 创建路径
|
||||
using (var pathGraphics = new GraphicsPath())
|
||||
{
|
||||
// 转换WPF坐标到GDI+坐标
|
||||
var points = new PointF[path.Count];
|
||||
for (int i = 0; i < path.Count; i++)
|
||||
{
|
||||
points[i] = new PointF(
|
||||
(float)(path[i].X - area.X),
|
||||
(float)(path[i].Y - area.Y)
|
||||
);
|
||||
}
|
||||
|
||||
// 添加路径
|
||||
pathGraphics.AddPolygon(points);
|
||||
|
||||
// 填充路径内部为白色(保留区域)
|
||||
maskGraphics.FillPath(Brushes.White, pathGraphics);
|
||||
}
|
||||
|
||||
// 创建结果位图
|
||||
var resultBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
|
||||
using (var resultGraphics = Graphics.FromImage(resultBitmap))
|
||||
{
|
||||
// 设置高质量渲染
|
||||
resultGraphics.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
resultGraphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
|
||||
// 使用遮罩合成图像
|
||||
resultGraphics.DrawImage(bitmap, 0, 0);
|
||||
resultGraphics.DrawImage(maskBitmap, 0, 0);
|
||||
}
|
||||
|
||||
return resultBitmap;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"应用形状遮罩失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
return bitmap; // 如果失败,返回原始图像
|
||||
}
|
||||
}
|
||||
|
||||
// 将System.Drawing.Bitmap转换为WPF BitmapSource
|
||||
private BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap) {
|
||||
using (var memory = new MemoryStream()) {
|
||||
|
||||
@@ -22,13 +22,20 @@
|
||||
|
||||
<!-- 选择区域容器 -->
|
||||
<Canvas Name="SelectionCanvas">
|
||||
<!-- 选择区域 -->
|
||||
<!-- 矩形选择模式 -->
|
||||
<Rectangle Name="SelectionRectangle"
|
||||
Stroke="Red"
|
||||
StrokeThickness="2"
|
||||
Fill="Transparent"
|
||||
Visibility="Collapsed" />
|
||||
|
||||
<!-- 任意形状选择模式 -->
|
||||
<Path Name="SelectionPath"
|
||||
Stroke="Red"
|
||||
StrokeThickness="2"
|
||||
Fill="Transparent"
|
||||
Visibility="Collapsed" />
|
||||
|
||||
<!-- 尺寸信息显示 -->
|
||||
<Border Name="SizeInfoBorder"
|
||||
Background="Black"
|
||||
@@ -43,6 +50,29 @@
|
||||
</Border>
|
||||
</Canvas>
|
||||
|
||||
<!-- 模式切换按钮 -->
|
||||
<StackPanel Orientation="Horizontal"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Margin="0,100,0,0">
|
||||
<Button Name="RectangleModeButton"
|
||||
Content="矩形模式"
|
||||
Margin="5,0"
|
||||
Padding="10,5"
|
||||
Background="#2563eb"
|
||||
Foreground="White"
|
||||
BorderThickness="0"
|
||||
Click="RectangleModeButton_Click" />
|
||||
<Button Name="FreehandModeButton"
|
||||
Content="自由绘制"
|
||||
Margin="5,0"
|
||||
Padding="10,5"
|
||||
Background="#6b7280"
|
||||
Foreground="White"
|
||||
BorderThickness="0"
|
||||
Click="FreehandModeButton_Click" />
|
||||
</StackPanel>
|
||||
|
||||
<!-- 提示文字 -->
|
||||
<Border Background="Black"
|
||||
Opacity="0.8"
|
||||
@@ -50,9 +80,9 @@
|
||||
Padding="15,8"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Margin="0,50,0,0">
|
||||
Margin="0,150,0,0">
|
||||
<TextBlock Name="HintText"
|
||||
Text="拖拽鼠标选择截图区域,按ESC取消"
|
||||
Text="拖拽鼠标选择矩形区域,或使用自由绘制模式"
|
||||
Foreground="White"
|
||||
FontSize="16" />
|
||||
</Border>
|
||||
|
||||
@@ -1,22 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using Brushes = System.Windows.Media.Brushes;
|
||||
using Color = System.Windows.Media.Color;
|
||||
|
||||
// 为了避免命名冲突,使用别名
|
||||
using WpfCanvas = System.Windows.Controls.Canvas;
|
||||
using DrawingRectangle = System.Drawing.Rectangle;
|
||||
using WpfRectangle = System.Windows.Shapes.Rectangle;
|
||||
|
||||
namespace Ink_Canvas
|
||||
{
|
||||
public partial class ScreenshotSelectorWindow : Window
|
||||
{
|
||||
private bool _isSelecting = false;
|
||||
private bool _isFreehandMode = false;
|
||||
private System.Windows.Point _startPoint;
|
||||
private System.Windows.Point _currentPoint;
|
||||
private List<System.Windows.Point> _freehandPoints;
|
||||
private Polyline _freehandPolyline;
|
||||
|
||||
public Rectangle? SelectedArea { get; private set; }
|
||||
public DrawingRectangle? SelectedArea { get; private set; }
|
||||
public List<System.Windows.Point> SelectedPath { get; private set; }
|
||||
|
||||
public ScreenshotSelectorWindow()
|
||||
{
|
||||
@@ -25,9 +36,12 @@ namespace Ink_Canvas
|
||||
// 设置窗口覆盖所有屏幕
|
||||
SetupFullScreenOverlay();
|
||||
|
||||
// 初始化自由绘制模式
|
||||
InitializeFreehandMode();
|
||||
|
||||
// 隐藏提示文字的定时器
|
||||
var timer = new System.Windows.Threading.DispatcherTimer();
|
||||
timer.Interval = TimeSpan.FromSeconds(3);
|
||||
timer.Interval = TimeSpan.FromSeconds(5);
|
||||
timer.Tick += (s, e) =>
|
||||
{
|
||||
HintText.Visibility = Visibility.Collapsed;
|
||||
@@ -36,6 +50,18 @@ namespace Ink_Canvas
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
private void InitializeFreehandMode()
|
||||
{
|
||||
_freehandPoints = new List<System.Windows.Point>();
|
||||
_freehandPolyline = new Polyline
|
||||
{
|
||||
Stroke = Brushes.Red,
|
||||
StrokeThickness = 2,
|
||||
Fill = Brushes.Transparent
|
||||
};
|
||||
SelectionCanvas.Children.Add(_freehandPolyline);
|
||||
}
|
||||
|
||||
private void SetupFullScreenOverlay()
|
||||
{
|
||||
// 获取所有屏幕的虚拟屏幕边界
|
||||
@@ -66,11 +92,39 @@ namespace Ink_Canvas
|
||||
{
|
||||
// 取消截图
|
||||
SelectedArea = null;
|
||||
SelectedPath = null;
|
||||
DialogResult = false;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void RectangleModeButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_isFreehandMode = false;
|
||||
RectangleModeButton.Background = new SolidColorBrush(Color.FromRgb(37, 99, 235)); // 蓝色
|
||||
FreehandModeButton.Background = new SolidColorBrush(Color.FromRgb(107, 114, 128)); // 灰色
|
||||
HintText.Text = "拖拽鼠标选择矩形区域";
|
||||
|
||||
// 清除自由绘制的内容
|
||||
_freehandPoints.Clear();
|
||||
_freehandPolyline.Points.Clear();
|
||||
SelectionPath.Visibility = Visibility.Collapsed;
|
||||
SelectionRectangle.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void FreehandModeButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_isFreehandMode = true;
|
||||
FreehandModeButton.Background = new SolidColorBrush(Color.FromRgb(37, 99, 235)); // 蓝色
|
||||
RectangleModeButton.Background = new SolidColorBrush(Color.FromRgb(107, 114, 128)); // 灰色
|
||||
HintText.Text = "按住鼠标左键绘制任意形状,松开完成";
|
||||
|
||||
// 清除矩形选择的内容
|
||||
SelectionRectangle.Visibility = Visibility.Collapsed;
|
||||
_freehandPoints.Clear();
|
||||
_freehandPolyline.Points.Clear();
|
||||
}
|
||||
|
||||
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
_isSelecting = true;
|
||||
@@ -80,24 +134,49 @@ namespace Ink_Canvas
|
||||
// 隐藏提示文字
|
||||
HintText.Visibility = Visibility.Collapsed;
|
||||
|
||||
// 显示选择矩形
|
||||
if (_isFreehandMode)
|
||||
{
|
||||
// 自由绘制模式
|
||||
_freehandPoints.Clear();
|
||||
_freehandPolyline.Points.Clear();
|
||||
_freehandPoints.Add(_startPoint);
|
||||
_freehandPolyline.Points.Add(_startPoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 矩形模式
|
||||
SelectionRectangle.Visibility = Visibility.Visible;
|
||||
SizeInfoBorder.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
// 捕获鼠标
|
||||
CaptureMouse();
|
||||
|
||||
if (!_isFreehandMode)
|
||||
{
|
||||
UpdateSelection();
|
||||
}
|
||||
}
|
||||
|
||||
private void Window_MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (_isSelecting)
|
||||
{
|
||||
_currentPoint = e.GetPosition(this);
|
||||
|
||||
if (_isFreehandMode)
|
||||
{
|
||||
// 自由绘制模式:添加点到路径
|
||||
_freehandPoints.Add(_currentPoint);
|
||||
_freehandPolyline.Points.Add(_currentPoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 矩形模式
|
||||
UpdateSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
@@ -106,7 +185,41 @@ namespace Ink_Canvas
|
||||
_isSelecting = false;
|
||||
ReleaseMouseCapture();
|
||||
|
||||
// 计算选择区域
|
||||
if (_isFreehandMode)
|
||||
{
|
||||
// 自由绘制模式:完成路径
|
||||
if (_freehandPoints.Count > 3) // 至少需要3个点形成有效路径
|
||||
{
|
||||
// 闭合路径
|
||||
_freehandPoints.Add(_startPoint);
|
||||
_freehandPolyline.Points.Add(_startPoint);
|
||||
|
||||
// 保存选择的路径
|
||||
SelectedPath = new List<System.Windows.Point>(_freehandPoints);
|
||||
|
||||
// 计算边界矩形用于截图
|
||||
var bounds = CalculatePathBounds(_freehandPoints);
|
||||
var dpiScale = GetDpiScale();
|
||||
var virtualScreen = System.Windows.Forms.SystemInformation.VirtualScreen;
|
||||
|
||||
int screenX = (int)((bounds.X * dpiScale) + virtualScreen.Left);
|
||||
int screenY = (int)((bounds.Y * dpiScale) + virtualScreen.Top);
|
||||
int screenWidth = (int)(bounds.Width * dpiScale);
|
||||
int screenHeight = (int)(bounds.Height * dpiScale);
|
||||
|
||||
SelectedArea = new DrawingRectangle(screenX, screenY, screenWidth, screenHeight);
|
||||
DialogResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedArea = null;
|
||||
SelectedPath = null;
|
||||
DialogResult = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 矩形模式
|
||||
var rect = GetSelectionRectangle();
|
||||
if (rect.Width > 5 && rect.Height > 5) // 最小尺寸检查
|
||||
{
|
||||
@@ -120,7 +233,7 @@ namespace Ink_Canvas
|
||||
int screenWidth = (int)(rect.Width * dpiScale);
|
||||
int screenHeight = (int)(rect.Height * dpiScale);
|
||||
|
||||
SelectedArea = new Rectangle(screenX, screenY, screenWidth, screenHeight);
|
||||
SelectedArea = new DrawingRectangle(screenX, screenY, screenWidth, screenHeight);
|
||||
DialogResult = true;
|
||||
}
|
||||
else
|
||||
@@ -128,6 +241,7 @@ namespace Ink_Canvas
|
||||
SelectedArea = null;
|
||||
DialogResult = false;
|
||||
}
|
||||
}
|
||||
|
||||
Close();
|
||||
}
|
||||
@@ -164,5 +278,26 @@ namespace Ink_Canvas
|
||||
|
||||
return new Rect(x, y, width, height);
|
||||
}
|
||||
|
||||
private Rect CalculatePathBounds(List<System.Windows.Point> points)
|
||||
{
|
||||
if (points == null || points.Count == 0)
|
||||
return new Rect();
|
||||
|
||||
double minX = points[0].X;
|
||||
double minY = points[0].Y;
|
||||
double maxX = points[0].X;
|
||||
double maxY = points[0].Y;
|
||||
|
||||
foreach (var point in points)
|
||||
{
|
||||
minX = Math.Min(minX, point.X);
|
||||
minY = Math.Min(minY, point.Y);
|
||||
maxX = Math.Max(maxX, point.X);
|
||||
maxY = Math.Max(maxY, point.Y);
|
||||
}
|
||||
|
||||
return new Rect(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user