如何在WPF应用程序中围绕鼠标光标位置缩放形状? [英] How to scale a shape around the mouse cursor location in a wpf application?

查看:140
本文介绍了如何在WPF应用程序中围绕鼠标光标位置缩放形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在使用WPF开发类似CAD的应用程序.平移和拖动是在包含画布的scrollviewer中实现的.所有的形状都画在画布上.现在我想在画布上缩放形状,但是缩放后光标下方的点有偏移.我只希望形状可以围绕鼠标光标位置缩放,并且可以仅使用C#代码编写而无需使用xaml.这是我的代码:

I was developing a CAD-like application recently with WPF. The panning and dragging are implemented in a scrollviewer which contains a canvas. All the shapes are drew in the canvas. Now I want to scale the shape in canvas but the point under the cursor has a offset after scaled.I just want the shape can scale around the mouse cursor location and can be written in C# code-only without xaml. Here is my code:

<local:sc x:Name="sc1" Height="263" HorizontalAlignment="Left"  Margin="51,34,0,0" VerticalAlignment="Top" Width="477" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Grid.ColumnSpan="2">
            <Canvas Name="canvas2" Height="337" Width="632" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Canvas.Background>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0" />
                        <GradientStop Color="White" Offset="1" />
                    </LinearGradientBrush>
                </Canvas.Background>
                <Canvas.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleY="-1"/>
                        <TranslateTransform Y="{Binding ElementName=canvas2,Path=Height}"/>
                    </TransformGroup>
                </Canvas.RenderTransform>
                <Label Content="duancong" Foreground="White" Canvas.Top="200">
                    <Label.RenderTransform>
                        <ScaleTransform ScaleY="-1"/>
                    </Label.RenderTransform>
                </Label>
            </Canvas>
        </local:sc>


这是后面的代码:


And here is the behind code:

void canvas2_MouseWheel(object sender, MouseWheelEventArgs e)
        {

            if (e.Delta > 0)
                this.scale *= 1.1;
            if (e.Delta < 0)
                this.scale *= 0.9;

            label2.Content = scale;
            scaler();
        }

    void scaler()
        {
            canvas2.Width = sc1.Width * scale;
            canvas2.Height = sc1.Height * scale;
            l1.Scale = scale;
        }

推荐答案

我将同时在X和Y方向上使用缩放变换以及偏移量(也在缩放变换中使用),您应该会做得很好去.
I would use the scale transform in both X and Y direction, together with the offset (also in scale transform) and you should be good to go.


这篇关于如何在WPF应用程序中围绕鼠标光标位置缩放形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆