去掉图像显示区的放大、缩小、适应按钮;改为鼠标右键弹出选择菜单

This commit is contained in:
李伟
2026-04-23 16:15:52 +08:00
parent 3e337cf04f
commit 3aa64843c8
2 changed files with 20 additions and 45 deletions
@@ -17,24 +17,17 @@
</UserControl.Resources>
<Border BorderBrush="Transparent" BorderThickness="1" ClipToBounds="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- 左侧控制按钮 -->
<Border Grid.Column="0" Background="White" Padding="5">
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
<Button x:Name="btnZoomIn" Content="+" Background="White" BorderBrush="LightGray" Width="40" Height="40" Margin="2" Click="BtnZoomIn_Click" />
<Button x:Name="btnZoomOut" Content="-" Background="White" BorderBrush="LightGray" Width="40" Height="40" Margin="2" Click="BtnZoomOut_Click" />
<Button x:Name="btnReset" Content="适应" Background="White" BorderBrush="LightGray" Width="40" Height="40" Margin="2" Click="BtnReset_Click" />
</StackPanel>
</Border>
<!-- 图像显示区域 -->
<Grid Grid.Column="1" x:Name="imageDisplayGrid" ClipToBounds="True">
<!-- 图像显示区域(去掉左侧按钮列,全部空间给图像) -->
<Grid x:Name="imageDisplayGrid" ClipToBounds="True">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="放大" Click="BtnZoomIn_Click" />
<MenuItem Header="缩小" Click="BtnZoomOut_Click" />
<MenuItem Header="适应窗口" Click="BtnReset_Click" />
</ContextMenu>
</Grid.ContextMenu>
<Grid x:Name="transformGrid"
RenderTransformOrigin="0,0"
RenderTransformOrigin="0.5,0.5"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.RenderTransform>
@@ -124,6 +124,8 @@ namespace XP.ImageProcessing.RoiControl.Controls
{
control.CanvasWidth = imageSource.Width;
control.CanvasHeight = imageSource.Height;
// 图像加载后自动适应窗口居中显示
control.ResetView();
}
}
@@ -334,39 +336,15 @@ namespace XP.ImageProcessing.RoiControl.Controls
private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
// 获取鼠标在 imageDisplayGrid 中的位置
Point mousePos = e.GetPosition(imageDisplayGrid);
// 获取鼠标在 Canvas 中的位置(缩放前)
Point mousePosOnCanvas = e.GetPosition(mainCanvas);
double oldZoom = ZoomScale;
double newZoom = oldZoom;
if (e.Delta > 0)
{
newZoom = oldZoom * ZoomStep;
}
else
{
newZoom = oldZoom / ZoomStep;
}
// 限制缩放范围
double newZoom = e.Delta > 0 ? oldZoom * ZoomStep : oldZoom / ZoomStep;
newZoom = Math.Max(0.1, Math.Min(10.0, newZoom));
if (Math.Abs(newZoom - oldZoom) > 0.001)
{
// 计算缩放比例变化
double scale = newZoom / oldZoom;
// 更新缩放
ZoomScale = newZoom;
// 调整平移偏移,使鼠标位置保持不变
// 新的偏移 = 旧偏移 + 鼠标位置 - 鼠标位置 * 缩放比例
PanOffsetX = mousePos.X - (mousePos.X - PanOffsetX) * scale;
PanOffsetY = mousePos.Y - (mousePos.Y - PanOffsetY) * scale;
// RenderTransformOrigin="0.5,0.5" 保证以图像中心等比缩放
// 拖拽平移偏移保持不变
}
e.Handled = true;
@@ -414,7 +392,7 @@ namespace XP.ImageProcessing.RoiControl.Controls
{
// 右键点击完成多边形
OnRightClick();
e.Handled = true;
// 不设 e.Handled,让 ContextMenu 正常弹出
}
private void ROI_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
@@ -470,6 +448,8 @@ namespace XP.ImageProcessing.RoiControl.Controls
if (newZoom <= 10.0)
{
ZoomScale = newZoom;
PanOffsetX = 0;
PanOffsetY = 0;
}
}
@@ -479,6 +459,8 @@ namespace XP.ImageProcessing.RoiControl.Controls
if (newZoom >= 0.1)
{
ZoomScale = newZoom;
PanOffsetX = 0;
PanOffsetY = 0;
}
}