2010-06-08 42 views
0

我正在使用縮放變換來允許用戶調整控件的大小。但是,當你開始移動鼠標時,控件會跳到一個新的大小,然後奇怪地縮放。您將鼠標從起始位置移動得越遠,尺寸的增加就越大。ScaleTransform非線性變換

我期望它能夠計算出應用的比例。下面是代碼:

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; 
    } 
} 

更新:現在用你爲什麼不使用的,而不是ResizeContainer.LayoutTransform ResizeContainer.RenderTransfrom 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> 
+0

其他控件放置在哪裏?它們是否在您正在調整大小的用戶控件中?你可以張貼更多的代碼來澄清? – 2010-06-08 14:13:03

+0

我更新了我的代碼。當你說兒童控制不能縮放時,我不確定你的意思。 :-) – 2010-06-08 16:25:45

回答

0

的原因嗎?即使用

ResizeContainer.LayoutTransform = scale; 

如果你想規模是線性的,我認爲你應該使用

double scaler = 1 - diff/ResizeContainer.ActualWidth; 

編輯:

存在這樣使縮放的代碼中的錯誤如果您嘗試調整一次以上,則可以控制大小「跳躍」。我建議你做到以下幾點:在你的MouseMove事件處理程序

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

更改代碼如下:

添加的RenderTransform您ResizeContainer電網

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

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

這樣你改變鼠標在拖動時移動的量很小。網格內的所有子控件也應該縮放。

+0

我使用了LayoutTransform,因爲我希望所有的孩子也可以調整大小,有沒有一種方法可以很好地與RenderTransform做到這一點? – Chris 2010-06-08 13:14:52

+0

更新:我剛剛嘗試RenderTransform與新的縮放器方程,它的工作完美...現在我需要的是其他控件與它一起擴展嗎? – Chris 2010-06-08 13:19:14

+0

其他控件放置在哪裏?它們是否在您正在調整大小的用戶控件中?你可以張貼更多的代碼來澄清? – 2010-06-08 13:33:51