ScaleTransform 非线性变换

发布于 2024-09-04 21:58:19 字数 2481 浏览 9 评论 0原文

我正在使用缩放变换来允许用户调整控件的大小。但会发生的情况是,当您开始移动鼠标时,控件会跳转到新的大小,然后奇怪地缩放。将鼠标从起始位置移得越远,尺寸的增加就越大。

我希望这是我计算要应用的比例的方式。代码如下:

private void ResizeGrip_MouseDown(object sender, MouseButtonEventArgs e)
{
    ResizeHandle.CaptureMouse();

    //Get the initial coordinate cursor location on the window
    initBtmX = e.GetPosition(this).X;

    bottomResize = true;
}

private void ResizeGrip_MouseUp(object sender, MouseButtonEventArgs e)
{
    bottomResize = false;
    ResizeHandle.ReleaseMouseCapture();
}

private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
{
    if( bottomResize == true)
    {
        //Get the new Y coordinate cursor location
        double newBtmX = e.GetPosition(this).X;


        //Get the smallest change between the initial and new cursor location
        double diffX = initBtmX - newBtmX;

        // Let our rectangle capture the mouse
        ResizeHandle.CaptureMouse();

        double newWidth = e.GetPosition(this).X - diffX;

        double scaler = newWidth / ResizeContainer.ActualWidth;

        Console.WriteLine("newWidth: {0}, scalar: {1}", newWidth, scaler);


        if (scaler < 0.75 || scaler > 3)
            return;

        ScaleTransform scale = new ScaleTransform(scaler, scaler);

        ResizeContainer.LayoutTransform = scale;
    }
}

更新:现在使用 XAML

<wtk:IToolDialog x:Name="VideoPlayer" ParentControl="{Binding ElementName=Stage}" DialogTitle="Video Player" Margin="90,5,0,0">
    <Grid> 
        <Grid x:Name="ResizeContainer" ClipToBounds="True" Width="320" Height="240" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,1">
            <!-- The video frame -->
            <Image Stretch="Fill" Source="{Binding CurrentFrameImage}" x:Name="VideoImage" />
            <Grid>
                <pplcontrols:VideoGroundPlane Foreground="Black" GridSize="20" GroundPlane="{Binding GroundPlane}">
                </pplcontrols:VideoGroundPlane>
            </Grid>
            <Grid x:Name="HitMask" IsHitTestVisible="False"/>
        </Grid>
        <ResizeGrip Cursor="SizeNWSE" x:Name="ResizeHandle" VerticalAlignment="Bottom" HorizontalAlignment="Right" Mouse.MouseDown="ResizeGrip_MouseDown" Mouse.MouseUp="ResizeGrip_MouseUp" Mouse.MouseMove="ResizeGrip_MouseMove"></ResizeGrip>
    </Grid>
</wtk:IToolDialog>

I am using scale transform to allow a user to resize a control. What happens though is that when you start to move the mouse the control jumps to a new size, and then scales oddly. The further you move your mouse from the starting location the larger the increase in size becomes.

I expect its the way I calculate the scale to be applied. Here is the code:

private void ResizeGrip_MouseDown(object sender, MouseButtonEventArgs e)
{
    ResizeHandle.CaptureMouse();

    //Get the initial coordinate cursor location on the window
    initBtmX = e.GetPosition(this).X;

    bottomResize = true;
}

private void ResizeGrip_MouseUp(object sender, MouseButtonEventArgs e)
{
    bottomResize = false;
    ResizeHandle.ReleaseMouseCapture();
}

private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
{
    if( bottomResize == true)
    {
        //Get the new Y coordinate cursor location
        double newBtmX = e.GetPosition(this).X;


        //Get the smallest change between the initial and new cursor location
        double diffX = initBtmX - newBtmX;

        // Let our rectangle capture the mouse
        ResizeHandle.CaptureMouse();

        double newWidth = e.GetPosition(this).X - diffX;

        double scaler = newWidth / ResizeContainer.ActualWidth;

        Console.WriteLine("newWidth: {0}, scalar: {1}", newWidth, scaler);


        if (scaler < 0.75 || scaler > 3)
            return;

        ScaleTransform scale = new ScaleTransform(scaler, scaler);

        ResizeContainer.LayoutTransform = scale;
    }
}

Update: Now with XAML

<wtk:IToolDialog x:Name="VideoPlayer" ParentControl="{Binding ElementName=Stage}" DialogTitle="Video Player" Margin="90,5,0,0">
    <Grid> 
        <Grid x:Name="ResizeContainer" ClipToBounds="True" Width="320" Height="240" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,1">
            <!-- The video frame -->
            <Image Stretch="Fill" Source="{Binding CurrentFrameImage}" x:Name="VideoImage" />
            <Grid>
                <pplcontrols:VideoGroundPlane Foreground="Black" GridSize="20" GroundPlane="{Binding GroundPlane}">
                </pplcontrols:VideoGroundPlane>
            </Grid>
            <Grid x:Name="HitMask" IsHitTestVisible="False"/>
        </Grid>
        <ResizeGrip Cursor="SizeNWSE" x:Name="ResizeHandle" VerticalAlignment="Bottom" HorizontalAlignment="Right" Mouse.MouseDown="ResizeGrip_MouseDown" Mouse.MouseUp="ResizeGrip_MouseUp" Mouse.MouseMove="ResizeGrip_MouseMove"></ResizeGrip>
    </Grid>
</wtk:IToolDialog>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-09-11 21:58:19

您不使用 ResizeContainer.RenderTransfrom 而不是 ResizeContainer.LayoutTransform 的任何原因?即使用

ResizeContainer.LayoutTransform = scale;

如果您希望比例是线性的,我认为您应该使用

double scaler = 1 - diff / ResizeContainer.ActualWidth;

编辑:

代码中存在一个错误,如果您尝试多次调整大小,则会导致缩放控件的大小“跳跃” 。我建议您执行以下操作:

将 RenderTransform 添加到 ResizeContainer 网格:

<Grid.RenderTransform>
    <ScaleTransform x:Name="transform" ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>

将 MouseMove 事件处理程序中的代码更改为以下内容:

if (bottomResize)
{
    double newBtmX = e.GetPosition(this).X;
    double scaler = -(initBtmX - newBtmX) / grid1.ActualWidth;
    initBtmX = newBtmX;

    transform.ScaleX += scaler;
    transform.ScaleY += scaler;
}

这样,您可以通过拖动时鼠标移动的任何少量来更改比例。网格内的所有子控件也应该缩放。

Any reason why you are not using ResizeContainer.RenderTransfrom instead of ResizeContainer.LayoutTransform? I.e. use

ResizeContainer.LayoutTransform = scale;

If you want the scale to be linear I think you should use

double scaler = 1 - diff / ResizeContainer.ActualWidth;

EDIT:

There is a bug in the code that causes the scaled control to "jump" in size if you try to resize more than once. I suggest you do the following:

Add a RenderTransform to your ResizeContainer grid:

<Grid.RenderTransform>
    <ScaleTransform x:Name="transform" ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>

Change the code in your MouseMove event handler to the following:

if (bottomResize)
{
    double newBtmX = e.GetPosition(this).X;
    double scaler = -(initBtmX - newBtmX) / grid1.ActualWidth;
    initBtmX = newBtmX;

    transform.ScaleX += scaler;
    transform.ScaleY += scaler;
}

This way you change the scale by whatever small amount the mouse has moved while dragging. All child controls within the grid should scale as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文