none
用鼠标滚轮实现缩放图像的问题 RRS feed

  • 问题

  • 请问一下VB2010中怎么实现用鼠标滚轮实现图像缩放的功能,网上找了很多也没有一个完整能运行的,请热心的人帮帮忙
    2011年7月28日 7:27

答案

  • 你好,

    以下是一个简单的例子:

     在MainPage.xmal里:

        <Grid x:Name="LayoutRoot" Background="White">
            <Image Source="你的图片路径"
                   MouseEnter="Image_MouseEnter"
                   MouseWheel="Image_MouseWheel"
                   MouseLeave="Image_MouseLeave">
                <Image.RenderTransform>
                    <ScaleTransform x:Name="ImgScale" ScaleX="0.1" ScaleY="0.1"></ScaleTransform>
                </Image.RenderTransform>
            </Image>
        </Grid>

    在MainPage.xaml.cs里(后台代码):

            bool isMouseCaptured;
            double delta = 0.1;

            private void Image_MouseEnter(object sender, MouseEventArgs e)
            {
                Image img = sender as Image;
                isMouseCaptured = true;
                img.CaptureMouse();
            }

            private void Image_MouseWheel(object sender, MouseWheelEventArgs e)
            {
                Image img = sender as Image;
                if (isMouseCaptured)
                {
                    if (e.Delta > 0) // 放大
                    {
                        ImgScale.ScaleX += delta;
                        ImgScale.ScaleY += delta;
                    }
                    else if (e.Delta < 0) // 缩小
                    {
                        ImgScale.ScaleX -= delta;
                        ImgScale.ScaleY -= delta;
                    }
                }
            }

            private void Image_MouseLeave(object sender, MouseEventArgs e)
            {
                Image img = sender as Image;
                isMouseCaptured = false;
                img.ReleaseMouseCapture();
            }

    另外你还可以使用Deep Zoom,以下是详细信息:

    http://msdn.microsoft.com/zh-cn/library/cc645050%28VS.95%29.aspx


    Daoping Liu - MSFT
    2011年8月1日 2:26
    版主