wpf拖动编程示例

来源:互联网 发布:域名name server 编辑:程序博客网 时间:2024/06/05 06:49

.xaml文件

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="_2006060101.Window1" x:Name="Window" Title="Window1" Width="640" Height="480" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Canvas x:Name="LayoutRoot">   

<Canvas    MouseLeftButtonDown="onMouseDown"    MouseLeftButtonUp="onMouseUp"    MouseMove="onMouseMove" Width="107" Height="102" Canvas.Left="216" Canvas.Top="123">   

<Ellipse     Height="100" Width="100"     Fill="Gold"     Stroke="Black" StrokeThickness="4" />    <Ellipse     Height="50" Width="50"     Canvas.Top="25" Canvas.Left="25"     Fill="Black" />   </Canvas>  

 <Canvas    MouseLeftButtonDown="onMouseDown"    MouseLeftButtonUp="onMouseUp"    MouseMove="onMouseMove"    Width="111.612" Height="100" Canvas.Left="174" Canvas.Top="126">    <!--使用旋转渐变让该画布旋转45度-->    <Canvas.RenderTransform>     <RotateTransform Angle="45" />    </Canvas.RenderTransform>      <Rectangle     Height="100" Width="100"     Fill="Coral"     Stroke="Black" StrokeThickness="4" />   </Canvas> </Canvas> </Window>

 

对应的.c#代码

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

namespace _2006060101

{

/// <summary> /// Interaction logic for Window1.xaml /// </summary>

 public partial class Window1 : Window {         

//beginX和beginY用于保存鼠标上一次的位置         

private double beginX;        

 private double beginY;        

 //isMouseDown获取和设置鼠标是否按下的布尔变量         private bool isMouseDown = false;         public Window1()         {             this.InitializeComponent();             // Insert code required on object creation below this point.         }         //鼠标左键按下时的事件代码         public void onMouseDown(object sender, MouseEventArgs e)         {             //将当前的鼠标位置传给私有变量             beginX = e.GetPosition(null).X;             beginY = e.GetPosition(null).Y;             //将isMouseDown设为true表示鼠标按下。             isMouseDown = true;             //由于CaptureMouse都定义在UIElement中,所以这里、转换为UIElement类             ((UIElement)sender).CaptureMouse();         }         //鼠标移动         public void onMouseMove(object sender, MouseEventArgs e)         {             //如果鼠标处理按下状态             if (isMouseDown == true)             {                 //获取鼠标当前位置                 double currX = e.GetPosition(null).X;                 double currY = e.GetPosition(null).Y;                 //获取当前图形的位置                 double currLeft = (double)((UIElement)sender).GetValue(Canvas.LeftProperty);                 double currTop = (double)((UIElement)sender).GetValue(Canvas.TopProperty);                 //移动当前图形的位置                 ((UIElement)sender).SetValue(Canvas.LeftProperty, currLeft + currX - beginX);                 ((UIElement)sender).SetValue(Canvas.TopProperty, currTop + currY - beginY);                 //将当前位置保存起来,继续拖动。                 beginX = currX;                 beginY = currY;             }         }       

  //释放鼠标左键         

public void onMouseUp(object sender, MouseEventArgs e)        

 {             isMouseDown = false;             //停止鼠标捕捉             ((UIElement)sender).ReleaseMouseCapture();       

  }

 }

}