zoukankan      html  css  js  c++  java
  • SL4.背景图片拖动

    XAML:

            <Grid x:Name="MainGrid">
    <ScrollViewer x:Name="canvasScroller"
    VerticalScrollBarVisibility
    ="Hidden"
    HorizontalScrollBarVisibility
    ="Hidden" >
    <Canvas x:Name="LayoutRoot"
    Width
    ="1024"
    Height
    ="768"
    MouseMove
    ="CanvasMove" MouseLeftButtonDown="CanvasDown" MouseLeftButtonUp="CanvasUp" MouseLeave="LayoutRoot_MouseLeave">
    <Image Source="yi.jpg"
    Width
    ="1024"
    Height
    ="768"/>

    </Canvas>
    </ScrollViewer >
    </Grid>

      XAML.CS

            private double horizontalValue = 0;
            private double verticalValue = 0;
            bool? moving = null;
            Point startingPos = new Point();
            Point endingPos = new Point();
            private double dx = 0;
            private double dy = 0;
            bool posChanged = false;
         private void CanvasDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    moving
    = false;
    LayoutRoot.Cursor
    = Cursors.Arrow;

    startingPos
    = e.GetPosition(LayoutRoot);
    }
    private void CanvasMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
    if ((moving != null) && (moving == false))
    {
    moving
    = true;
    posChanged
    = true;
    LayoutRoot.Cursor
    = Cursors.Hand;
    }

    if (posChanged)
    {
    endingPos
    = e.GetPosition(LayoutRoot);

    dx
    = startingPos.X - endingPos.X;
    dy
    = startingPos.Y - endingPos.Y;

    double newPosY = canvasScroller.VerticalOffset + dy;
    double newPosX = canvasScroller.HorizontalOffset + dx;

    canvasScroller.ScrollToHorizontalOffset(newPosX);
    canvasScroller.ScrollToVerticalOffset(newPosY);
    }
    }

    private void CanvasUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    posChanged
    = false;

    LayoutRoot.Cursor
    = Cursors.Arrow;
    }

    private void LayoutRoot_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
    // 在此处添加事件处理程序实现。
    posChanged = false;

    LayoutRoot.Cursor
    = Cursors.Arrow;
    }

      

  • 相关阅读:
    c++ 函数中的部分代码执行一次
    如何限制对象只能建立在堆上或者栈上
    FFMPEG Qt视频播放器
    C/C++中带可变参数的函数
    柔性数组
    压缩图片网站
    vscode存盘时格式化
    两个i标签之间有缝隙
    node 中process进程argv,argv0,execArgv,execPath
    chalk插件 使终端输出的字带颜色
  • 原文地址:https://www.cnblogs.com/baobao2010/p/2172614.html
Copyright © 2011-2022 走看看