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