WPF Canvas ZoomIn ZoomOut via set Background="Transparent"

  <Canvas Grid.Column="1" Background="Transparent" x:Name="cvs" ClipToBounds="True" MouseWheel="cvs_MouseWheel" MouseDown="cvs_MouseDown"
  MouseUp="cvs_MouseUp" MouseMove="cvs_MouseMove">
      <Canvas.LayoutTransform>
          <TransformGroup>
              <ScaleTransform x:Name="scaler"/>
              <TranslateTransform x:Name="translater"/>
          </TransformGroup>
      </Canvas.LayoutTransform>
  </Canvas>



 private void cvs_MouseWheel(object sender, MouseWheelEventArgs e)
 {
     if(e.Delta>0)
     {
         scaler.ScaleX *= 1.2;
         scaler.ScaleY *= 1.2;
     }
     else
     {
         scaler.ScaleX /= 1.2;
         scaler.ScaleY /= 1.2;
     }
     scaler.CenterX = e.GetPosition(this).X;
     scaler.CenterY= e.GetPosition(this).Y;
 }

 

 

 

 

posted @ 2024-07-13 21:44  FredGrit  阅读(5)  评论(0编辑  收藏  举报