zoukankan      html  css  js  c++  java
  • Silverlight 如何拖放对象

     

    下面的示例演示如何在基于 Silverlight 的应用程序中拖放对象。出于安全考虑,不能在应用程序之间拖放对象。因此,说成在 Silverlight 插件区域内"滑动"对象更为准确。但是,术语"拖放"更为人知,因此在此处使用。

    XAML 
    <UserControl x:Class="DragAndDropSimple.Page"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Width="400" Height="300">
                <Canvas x:Name="rootCanvas"
                Width="640"
                Height="480"
                Background="Gray"
                >
                <!-- You can drag this rectangle around the canvas. -->
                <Rectangle
                MouseLeftButtonDown="Handle_MouseDown"
                MouseMove="Handle_MouseMove"
                MouseLeftButtonUp="Handle_MouseUp"
                Canvas.Left="30" Canvas.Top="30" Fill="Red"
                Width="50" Height="50" />
                </Canvas>
                </UserControl>
    C# 
    // Global variables used to keep track of the
                // mouse position and whether the object is captured
                // by the mouse.
                bool isMouseCaptured;
                double mouseVerticalPosition;
                double mouseHorizontalPosition;
                public void Handle_MouseDown (object sender, MouseEventArgs args)
                {
                Rectangle item = sender as Rectangle;
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                isMouseCaptured = true;
                item.CaptureMouse();
                }
                public void Handle_MouseMove(object sender, MouseEventArgs args)
                {
                Rectangle item = sender as Rectangle;
                if (isMouseCaptured)
                {
                // Calculate the current position of the object.
                double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
                double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
                double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
                // Set new position of object.
                item.SetValue(Canvas.TopProperty, newTop);
                item.SetValue(Canvas.LeftProperty, newLeft);
                // Update position global variables.
                mouseVerticalPosition = args.GetPosition(null).Y;
                mouseHorizontalPosition = args.GetPosition(null).X;
                }
                }
                public void Handle_MouseUp(object sender, MouseEventArgs args)
                {
                Rectangle item = sender as Rectangle;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mouseVerticalPosition = -1;
                mouseHorizontalPosition = -1;
                }
    Visual Basic 
    ' Global variables used to keep track of the
                ' mouse position and whether the object is captured
                ' by the mouse.
                Dim isMouseCaptured As Boolean
                Dim mouseVerticalPosition As Double
                Dim mouseHorizontalPosition As Double
                Private Sub Handle_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
                Dim item As Rectangle = sender
                mouseVerticalPosition = e.GetPosition(Nothing).Y
                mouseHorizontalPosition = e.GetPosition(Nothing).X
                isMouseCaptured = True
                item.CaptureMouse()
                End Sub
                Private Sub Handle_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
                Dim item As Rectangle = sender
                If (isMouseCaptured) Then
                ' Calculate the current position of the object.
                Dim deltaV As Double = e.GetPosition(Nothing).Y - mouseVerticalPosition
                Dim deltaH As Double = e.GetPosition(Nothing).X - mouseHorizontalPosition
                Dim newTop As Double = deltaV + item.GetValue(Canvas.TopProperty)
                Dim newLeft As Double = deltaH + item.GetValue(Canvas.LeftProperty)
                ' Set new position of object.
                item.SetValue(Canvas.TopProperty, newTop)
                item.SetValue(Canvas.LeftProperty, newLeft)
                ' Update position global variables.
                mouseVerticalPosition = e.GetPosition(Nothing).Y
                mouseHorizontalPosition = e.GetPosition(Nothing).X
                End If
                End Sub
                Private Sub Handle_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
                Dim item As Rectangle = sender
                isMouseCaptured = False
                item.ReleaseMouseCapture()
                mouseVerticalPosition = -1
                mouseHorizontalPosition = -1
                End Sub
                End Class
    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    3 面向过程与面向对象进一步比较
    2 类、实例、属性、方法详解
    1 面向对象介绍
    CentOS5、6 NFS的安装配置及mount方法
    js 冒泡 捕获
    js定义类或对象
    new Option()——实现时间联动
    js闭包(转)
    (转)大型网站系统架构的演化
    30款最好的 Bootstrap 3.0 免费主题和模板
  • 原文地址:https://www.cnblogs.com/starcrm/p/1444764.html
Copyright © 2011-2022 走看看