• WPF : 以鼠标指针为中心缩放


    XMAL:

    <Canvas
        x:Name="mCanvas"
        MouseWheel="mCanvas_MouseWheel"
        Background="Wheat"
    >
    <Canvas.RenderTransform>
        <TransformGroup>
            <ScaleTransform x:Name="mScaleTf" ScaleX="1.0" CenterX="0"></ScaleTransform>        
            <TranslateTransform x:Name="mTranslateTf" X="10" Y="20"></TranslateTransform>
        </TransformGroup>
    </Canvas.RenderTransform>
    </Canvas>

    ------------------------------------------------------------------------------------------------------

    Code:

    private void mCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {

               Point zoomCenter = e.GetPosition(this.mCanvas);//参数必须是this.mCanvas, e.GetPosition和RenderTransform有关?

                //或者:
                //Point screenPos = e.GetPosition(this);
                //Point zoomCenter = this.mCanvas.RenderTransform.Inverse.Transform(screenPos);

                Point pt = this.mCanvas.RenderTransform.Inverse.Transform(zoomCenter);
                this.mTranslateTf.X = (zoomCenter.X - pt.X) * this.mScaleTf.ScaleX ;
                this.mTranslateTf.Y = (zoomCenter.Y - pt.Y) * this.mScaleTf.ScaleY;
                this.mScaleTf.CenterX = zoomCenter.X;
                this.mScaleTf.CenterY = zoomCenter.Y;
                this.mScaleTf.ScaleX += e.Delta / 1000.0;
                this.mScaleTf.ScaleY += e.Delta / 1000.0;

                Console.WriteLine("MouseWheel : " + zoomCenter.ToString());

    }

    或者

    private void mCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        Point mousePos = e.GetPosition(this);

        //oldCenter : untransformed and transformed
        Point untransformedOldCenter = new Point(this.mScaleTf.CenterX, this.mScaleTf.CenterY);
        Point transformedOldCenter = this.mScaleTf.Transform(untransformedOldCenter);

        //newCenter : untransformed and transformed
        Point untransformedNewCenter = this.mCanvas.RenderTransform.Inverse.Transform(mousePos);
        Point transformedNewCenter = mousePos;

        double adjustX = transformedNewCenter.X - transformedOldCenter.X - untransformedNewCenter.X + untransformedOldCenter.X;

        //update transforms
        this.mTranslateTf.X = adjustX;
        this.mScaleTf.CenterX = untransformedNewCenter.X;
        this.mScaleTf.ScaleX += 0.1;
    }

  • 相关阅读:
    2019中国爱分析数据智能高峰论坛(北京)
    GIEC2019第六届全球互联网经济大会北京站震撼来袭!
    iDigital2019数字营销广告主峰会
    DOIS 2019 DevOps国际峰会北京站来袭~
    QCon2019全球软件开发大会广州站即将来袭
    DTCC2019第十届中国数据库技术大会将于5月在北京召开
    2019第十四届中国竞争情报国际年会将于4月在上海召开
    第十二届GOPS全球运维大会2019深圳站即将开幕
    MTSC2019第五届中国移动互联网测试开发大会北京站震撼来袭!
    第八节:分支开发之合并到master
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/1239581.html
Copyright © 2020-2023  润新知