wpf拖动编程示例
来源:互联网 发布:域名name server 编辑:程序博客网 时间:2024/06/05 06:49
.xaml文件
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation
<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();
}
}
}
- wpf拖动编程示例
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- 精通Silverlight——12.2.6 拖动编程示例
- WPF拖动绘制
- WPF拖动绘制
- WPF 鼠标拖动界面
- WPF拖动总结
- WPF实现控件拖动
- WPF Popup实现拖动
- WPF 无边框拖动
- WPF-009:WPF窗体的拖动
- HTML元素拖动示例
- WPF自制可拖动标题
- WPF拖动无边框窗体
- 华丽的开博~!
- SSO(单点登录)
- 不打开文件求文件大小
- 当函数返回对象的引用?
- test
- wpf拖动编程示例
- iframe自适应代码
- apache serverName 的启动问题
- 世界编程大赛第一名程序
- 监测指定路径下的文件创建活动
- Java - Array Initialization
- SQL无名表的探索(待续)
- SDE:空间数据库引擎
- 常来看看自己的缺点