今日您是第次访问
设为主页
添加改藏
网站优化
 
了解WPF技术
Silverlight WPF/E教程
WPF应用程序教程
当前位置:网站主页->Silverlight教程->文章浏览

SL中ScrollViewer控件实现“内容施动和缩放”功能!

作者:由灵 日期:2008-3-19     运行版本:2.0 Beta

  Silverlight从1.0版本一直支持对鼠标的施动,这个实例使用了Silverlight2.0开发。并在拖动过程中实现了加速算法,和缩放功能。一些基本知识由灵在此不做详细介绍了,需要的请结合本教程中的基础知识做本文实例。

浏览效果:


    打开Expression Blend 2.5,新建一个Silverlight 2 Application项目,并在page.xaml中施放添加页面所需的控件:
 TextBlock(文本显示控件),命名为percent,然后以可以把内容输入“0%”为了让设计人员在设计中直接看到效果。
    TextBlock(文本显示控件),内容输入为“缩放”,说明Slider控件的功能。
    Slider(滑块控件),Maximum属性为“1000”,Minimum为“0”,值为“100”,Orientation为“Horizontal”。
    ScrollViewer(滚动控件)HorizontalScrollBarVisibility设为Hidden,VerticalScrollBarVisibility设为Hidden。用于定位其子控件的视图位置,并添加子控件:
    Image(图像控件),Source属性中制定显示的图像,Stretch设为Uniform,让图像不变形。并把Cursor属性设为Hand,命名为“img_c”。
如图所示:
图1.0
    图1.0

    在Project项目面板中选中项目名称,右键菜单中点击“Edit Visual Studio”进入Visual Studio添加程序代码(如果VisualStudio中无法可视化编辑请到“最新Silverlight开发环境配置介绍”搭建开发环境吧)!

  主要使用代码功能:

        UIElement.CaptureMouse();   启用鼠标拖动事件(就算鼠标离开窗体后放开鼠标也调用MouseLeftButtonUp和MouseMove事件)

     UIElement.ReleaseMouseCapture(); 释放UIElement.CaptureMouse的鼠标拖动。

  MouseEventArgs.GetPosition    获取事件当前鼠标的相对坐标位置。

        Image.Stretch    Image图像控件的显示方式,None为不缩放,Fill按控件的Width和Height而缩放变化,Uniform则按实际图像比例大小缩放,UniformToFill是按照图原大小比例,以最大的填充Image显示。

        Image.ActualHeight  Image的希望大小,Image.Stretch为None时,Image的大小为显示的图片实际大小!

        Slider.Value                为获取Slider控件的滑块当前值置的值。

        ScrollViewer.ScrollToVerticalOffset 设置ScrollViewer控件的垂直滚动条的位置。

        ScrollViewer.ScrollToHorizontalOffset 设置ScrollViewer控件的水平滚动条的位置。

  现在为Silverlight的Image控件和Slider控件添加事件:

     Slider.ValueChanged="Slider_ValueChanged"

     Image.MouseLeftButtonDown="Image_MouseLeftButtonDown" MouseLeftButtonUp="Image_MouseLeftButtonUp"

代码:
namespace album
{
    public partial class main : UserControl
    {
        MouseEventHandler meh;
        double img_actualWidth = 0;
        double img_actualHeight = 0;
 public main()
 {
     // Required to initialize variables
            meh = new MouseEventHandler(img_c_MouseMove);
   InitializeComponent();
 }
        Point MouseDownAt;
        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            img_c.MouseMove += meh;
            MouseDownAt = e.GetPosition(null);
            img_c.CaptureMouse();
        }

        void img_c_MouseMove(object sender, MouseEventArgs e)
        {
            Point p=e.GetPosition(null);
            img_Scroll.ScrollToVerticalOffset(img_Scroll.VerticalOffset - ((p.Y - MouseDownAt.Y) * 1.2));
            img_Scroll.ScrollToHorizontalOffset(img_Scroll.HorizontalOffset -(( p.X - MouseDownAt.X)*1.2));
            MouseDownAt = p;
            //img_c.SetValue(image.LeftProperty, p.X - MouseDownAt.X);
            //img_c.SetValue(Canvas.TopProperty, p.Y - MouseDownAt.Y);
            //img_translate.X = p.X - MouseDownAt.X;
            //img_translate.Y = p.Y - MouseDownAt.Y;
            //img_c.SetValue(Canvas.LeftProperty, p.X - MouseDownAt.X);
            //img_c.SetValue(Canvas.TopProperty, p.Y - MouseDownAt.Y);
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            img_c.MouseMove -= meh;
            img_c.ReleaseMouseCapture();
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (img_scale != null)
            {
                string t = e.NewValue.ToString();

                img_c.Width = (zoom_slider.Value/100) * img_actualWidth;
                img_c.Height = (zoom_slider.Value/100) * img_actualHeight;

 

                string leftT = "";
                string rightT = "";
                int index=t.IndexOf('.');
                if (index != -1)
                {
                    leftT = t.Substring(0, index);
                    rightT = t.Substring(index + 1, t.Length - index - 1);
                    if (rightT.Length > 1)
                    {
                        rightT = rightT.Substring(0, 1);
                    }
                }
                else
                {
                    leftT = t;
                    rightT = "0";
                }
                if (leftT.Length == 1)
                    leftT = "0" + leftT;
                percent.Text = leftT+"."+rightT+ "%";
                //img_scale.ScaleX = e.NewValue;
                //img_scale.ScaleY = e.NewValue;
            }
            //betwenText.Text = "df";
        }
        private void img_c_Loaded(object sender, RoutedEventArgs e)
        {
            percent.Text = "100%";
            zoom_slider.Value = 100;
            Stretch s = img_c.Stretch;
            img_c.Stretch = Stretch.UniformToFillUniform;
            img_actualHeight = img_c.ActualHeight;
            img_actualWidth = img_c.ActualWidth;
            img_c.Stretch = s;
            //img_c.Width = img_c.DataContext;
            //img_c.Height = img_c.DesiredSize.Height;
        }
    }
}


   

点击下载实例源码