用于放大屏幕内容的 WPF 应用程序

问题描述 投票:0回答:1

当前正在检查可缩放 wpf 应用程序屏幕上的内容的控件。 敲击校准部分,因为需要在此控件中显示光标下方内容的缩放内容。

<Border Name="MagBorder" Width="175" Height="100" BorderBrush="Black" BorderThickness="1"   >
    <Border.RenderTransform>
        <TranslateTransform x:Name="MagTransform" />
    </Border.RenderTransform>
    <Border.Background>
        <VisualBrush x:Name="MagBrush" Stretch="UniformToFill">
            <VisualBrush.RelativeTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="10" ScaleY="10" />
                    <TranslateTransform x:Name="MagContentTransform" />
                </TransformGroup>
            </VisualBrush.RelativeTransform>
        </VisualBrush>
    </Border.Background>
</Border>

当前使用鼠标移动事件通过获取当前鼠标位置和屏幕大小来设置 TranslateTransform 值。使用以下公式设置平移变换。

  MagContentTransform.X = (-position.X  + element.Width / 2)/10;
        MagContentTransform.Y =  (-position.Y  + element.Height / 2)/10;

虽然这会缩放内容,但光标下方的确切内容并未显示。 需要帮助完善公式或任何其他替代方法来做到这一点。

c# .net wpf wpf-controls .net-framework-version
1个回答
0
投票

我使用之前的答案这里做了类似的事情,这创建了一个允许缩放的新边框类型。

然后,您可以通过执行以下操作来实现(我包括了图像控件)

<local:ZoomBorder
x:Name="ImageZoomBorder"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
    <Image
    x:Name="ImageDisplay"
    Margin="0"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    IsEnabled="True"
    Stretch="Uniform" />
</local:ZoomBorder>

我添加了一些额外的属性来将边界绑定到缩放

    public int ZoomPercentage
    {
        get { return (int)GetValue(ZoomPercentageProperty); }
        private set
        {
            SetValue(ZoomPercentageProperty, value);
            CommandManager.InvalidateRequerySuggested();
        }
    }

    public static readonly DependencyProperty ZoomPercentageProperty = DependencyProperty.Register("ZoomPercentage",
                                                                                                   typeof(int),
                                                                                                   typeof(ZoomBorder),
                                                                                                   new PropertyMetadata(100));
    public int ZoomMinimum
    {
        get { return (int)GetValue(ZoomMinimumProperty); }
        set { SetValue(ZoomMinimumProperty, value); }
    }

    public static readonly DependencyProperty ZoomMinimumProperty = DependencyProperty.Register("ZoomMinimum",
                                                                                                   typeof(int),
                                                                                                   typeof(ZoomBorder),
                                                                                                   new PropertyMetadata(100));
    public int ZoomMaximum
    {
        get { return (int)GetValue(ZoomMaximumProperty); }
        set { SetValue(ZoomMaximumProperty, value); }
    }

    public static readonly DependencyProperty ZoomMaximumProperty = DependencyProperty.Register("ZoomMaximum",
                                                                                                   typeof(int),
                                                                                                   typeof(ZoomBorder),
                                                                                                   new PropertyMetadata(1000));

这样我就可以对MouseWheel控件进行更稳健的操作

    private void Child_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (child != null)
        {
            ScaleTransform st = GetScaleTransform(child);
            TranslateTransform tt = GetTranslateTransform(child);

            double zoom = e.Delta > 0 ? .2 : -.2;
            if (e.Delta < 0 && (st.ScaleX + zoom < ZoomMinimum / 100.0 || st.ScaleY + zoom < ZoomMinimum / 100.0))
                return;
            if (e.Delta > 0 && (st.ScaleX + zoom > ZoomMaximum / 100.0 || st.ScaleY + zoom > ZoomMaximum / 100.0))
                return;

            Point relative = e.GetPosition(child);
            double absoluteX;
            double absoluteY;

            absoluteX = relative.X * st.ScaleX + tt.X;
            absoluteY = relative.Y * st.ScaleY + tt.Y;

            st.ScaleX += zoom;
            st.ScaleY += zoom;
            st.ScaleX = Math.Round(st.ScaleX, 2);
            st.ScaleY = Math.Round(st.ScaleY, 2);
            ZoomPercentage = (int)(st.ScaleX * 100.0);

            tt.X = absoluteX - relative.X * st.ScaleX;
            tt.Y = absoluteY - relative.Y * st.ScaleY;
            TransformChanged?.Invoke(this, new TransformEventArgs(st, tt));
        }
    }

我还添加了活动

public event TransformEventHandler TransformChanged;

因此一些外部服务可以基于转换来做事。

© www.soinside.com 2019 - 2024. All rights reserved.