zoukankan      html  css  js  c++  java
  • 拖放 UI 布局中的对象

    UI
    <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>
    Behide Code
    // 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;
    }

  • 相关阅读:
    探讨GTK+应用程序的优化方法
    X Window研究笔记(9)
    X Window研究笔记(10)
    X Window研究笔记(7)
    用gdbserver调试共享库(改进版)
    X Window研究笔记(11)
    Gtkminimo中的几个BUG
    编译基于DirectFB的Phoneme(Advance)
    X Window研究笔记(8)
    X Window研究笔记(12)
  • 原文地址:https://www.cnblogs.com/landexia/p/1984937.html
Copyright © 2011-2022 走看看