首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非线性ScaleTransform变换

非线性ScaleTransform变换
EN

Stack Overflow用户
提问于 2010-06-08 11:19:50
回答 1查看 1.9K关注 0票数 0

我使用缩放转换来允许用户调整控件的大小。然而,当你开始移动鼠标时,控件会跳转到一个新的大小,然后奇怪地缩放。鼠标从起始位置移动得越远,大小的增加就越大。

我希望这是我计算要应用的尺度的方式。以下是代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-08 12:51:56

你为什么不使用ResizeContainer.RenderTransfrom而不是ResizeContainer.LayoutTransform呢?即使用

代码语言:javascript
复制
ResizeContainer.LayoutTransform = scale;

如果你想要刻度是线性的,我想你应该用

代码语言:javascript
复制
double scaler = 1 - diff / ResizeContainer.ActualWidth;

编辑:

代码中有一个错误,如果您多次调整大小,则会导致缩放控件的大小“跳转”。我建议你做以下几点:

将RenderTransform添加到ResizeContainer网格中:

代码语言:javascript
复制
<Grid.RenderTransform>
    <ScaleTransform x:Name="transform" ScaleX="1" ScaleY="1" />
</Grid.RenderTransform>

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

代码语言:javascript
复制
if (bottomResize)
{
    double newBtmX = e.GetPosition(this).X;
    double scaler = -(initBtmX - newBtmX) / grid1.ActualWidth;
    initBtmX = newBtmX;

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

通过这种方式,您可以通过鼠标在拖动时移动的任何少量来更改缩放。网格中的所有子控件也应该进行缩放。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2996792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档