improve:图片插入

This commit is contained in:
2025-08-31 09:20:35 +08:00
parent b8fe5bbd66
commit 33948c604c
+33 -27
View File
@@ -141,19 +141,6 @@ namespace Ink_Canvas
element.RenderTransform = transformGroup; element.RenderTransform = transformGroup;
} }
// 设置位置
if (position.HasValue)
{
// 在指定位置居中显示
InkCanvas.SetLeft(image, position.Value.X - image.Width / 2);
InkCanvas.SetTop(image, position.Value.Y - image.Height / 2);
}
else
{
// 使用与文件选择相同的居中和缩放逻辑
CenterAndScaleElement(image);
}
// 设置图片属性,避免被InkCanvas选择系统处理 // 设置图片属性,避免被InkCanvas选择系统处理
image.IsHitTestVisible = true; image.IsHitTestVisible = true;
image.Focusable = false; image.Focusable = false;
@@ -171,23 +158,42 @@ namespace Ink_Canvas
// 添加到画布 // 添加到画布
inkCanvas.Children.Add(image); inkCanvas.Children.Add(image);
// 绑定事件处理 // 等待图片加载完成后再进行居中处理
if (image is FrameworkElement elementForEvents) image.Loaded += (sender, e) =>
{ {
// 鼠标事件 // 确保在UI线程中执行
elementForEvents.MouseLeftButtonDown += Element_MouseLeftButtonDown; Dispatcher.BeginInvoke(new Action(() =>
elementForEvents.MouseLeftButtonUp += Element_MouseLeftButtonUp; {
elementForEvents.MouseMove += Element_MouseMove; // 先进行缩放居中处理
elementForEvents.MouseWheel += Element_MouseWheel; CenterAndScaleElement(image);
// 触摸事件 // 如果有指定位置,调整到指定位置
elementForEvents.IsManipulationEnabled = true; if (position.HasValue)
elementForEvents.ManipulationDelta += Element_ManipulationDelta; {
elementForEvents.ManipulationCompleted += Element_ManipulationCompleted; // 在指定位置居中显示
InkCanvas.SetLeft(image, position.Value.X - image.Width / 2);
InkCanvas.SetTop(image, position.Value.Y - image.Height / 2);
}
// 设置光标 // 绑定事件处理器
elementForEvents.Cursor = Cursors.Hand; if (image is FrameworkElement elementForEvents)
} {
// 鼠标事件
elementForEvents.MouseLeftButtonDown += Element_MouseLeftButtonDown;
elementForEvents.MouseLeftButtonUp += Element_MouseLeftButtonUp;
elementForEvents.MouseMove += Element_MouseMove;
elementForEvents.MouseWheel += Element_MouseWheel;
// 触摸事件
elementForEvents.IsManipulationEnabled = true;
elementForEvents.ManipulationDelta += Element_ManipulationDelta;
elementForEvents.ManipulationCompleted += Element_ManipulationCompleted;
// 设置光标
elementForEvents.Cursor = Cursors.Hand;
}
}), System.Windows.Threading.DispatcherPriority.Loaded);
};
// 提交到历史记录 // 提交到历史记录
timeMachine.CommitElementInsertHistory(image); timeMachine.CommitElementInsertHistory(image);