打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF中拖动控件,实现位置随意摆放

WPF中拖动控件,实现位置随意摆放[1]

分类: WPF编程技术2685人阅读评论(5)收藏举报
一般的拖动程序,都是实现 MouseLeftButtonDown,MouseLeftButtonUp,MouseMove 这三个事件,大多数的情况下,拖动过程中,都是在 MouseMove 这个函数里面设置控件的坐标。

以下的代码,只有一点点的不同,在拖动过程中,原控件还是在原来位置,只是新产生了一个按控件外形生成的阴影图片,然后设置该阴影图片的位置,最后,鼠标离开的时候,设置原控件的位置。。。

  1. private void ContainerPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
  2. {  
  3.     if ((e.ClickCount <= 1) && (sender != e.Source))  
  4.     {  
  5.         ElementUI source = e.Source as ElementUI;  
  6.         this.SelectedItem = source;  
  7.         NodeUI eui = e.Source as NodeUI;  
  8.         if ((eui != null) && eui.DataSource.CanDrag)  
  9.         {  
  10.             NodeUI nodeUI = (NodeUI) e.Source;  
  11.             Point point = new Point(nodeUI.CenterX, nodeUI.CenterY);  
  12.             this.startPoint = base.ToPlot(e.GetPosition(this));  
  13.             this.offsetVector = (Vector) (this.startPoint - point);  
  14.             this.CreateDragShade(nodeUI);  
  15.             this.dragShade.CaptureMouse();//阴影捕获鼠标  
  16.             e.Handled = true;  
  17.         }  
  18.     }  
  19. }  
  20.   
  21. private void ContainerPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
  22. {  
  23.     if (e.ClickCount <= 1)  
  24.     {  
  25.         NodeUI selectedItem = this.SelectedItem as NodeUI;  
  26.         if ((selectedItem != null) && this.dragShade.IsMouseCaptured)  
  27.         {  
  28.             Point point = base.ToPlot(e.GetPosition(this));  
  29.             double dx = point.X - this.startPoint.X;  
  30.             double dy = point.Y - this.startPoint.Y;  
  31.             Node dataSource = selectedItem.DataSource;  
  32.             if ((dataSource != null) && dataSource.CanDrag)  
  33.             {  
  34.                 dataSource.PerformDrag(dx, dy);  
  35.             }  
  36.         }  
  37.         this.dragShade.ReleaseMouseCapture();  
  38.         this.dragShade.Visibility = Visibility.Hidden;  
  39.         base.Children.Remove(this.dragShade);  
  40.     }  
  41. }  
  42.   
  43. private void ContainerPanel_MouseMove(object sender, MouseEventArgs e)  
  44. {  
  45.     if ((e.LeftButton == MouseButtonState.Pressed) && this.dragShade.IsMouseCaptured)  
  46.     {  
  47.         Point point = base.ToPlot(e.GetPosition(this));  
  48.         PlotPanel.SetCenterX(this.dragShade, point.X - this.offsetVector.X);  
  49.         PlotPanel.SetCenterY(this.dragShade, point.Y - this.offsetVector.Y);  
  50.     }  
  51. }  
  52.   
  53. private void CreateDragShade(NodeUI nodeUI)  
  54. {  
  55.     VisualBrush brush = new VisualBrush();  
  56.     brush.Stretch = Stretch.Fill;  
  57.     brush.Visual = nodeUI;  
  58.     brush.Opacity = 0.6;  
  59.     this.dragShade.Width = nodeUI.ActualWidth;  
  60.     this.dragShade.Height = nodeUI.ActualHeight;  
  61.     this.dragShade.Stroke = Brushes.Transparent;  
  62.     this.dragShade.Fill = brush;  
  63.     this.dragShade.Stretch = Stretch.Fill;  
  64.     PlotPanel.SetCenterX(this.dragShade, nodeUI.CenterX);  
  65.     PlotPanel.SetCenterY(this.dragShade, nodeUI.CenterY);  
  66.     this.dragShade.Visibility = Visibility.Visible;  
  67.     base.Children.Add(this.dragShade);  
  68. }  



WPF中拖动控件,实现位置随意摆放[2]

分类: WPF编程技术1143人阅读评论(0)收藏举报

在WPF中,除了可以通过处理鼠标事件来实现控件位置的拖动以外,还可以通过定义一些控件的行为,来实现控件的拖动,具体操作步骤如下:


自定义实现拖动的类库

1. 使用VS2010建立一个C#的类库

2. 增加"System.Windows.Interactivity.dll"库的引用\

如果使用的是Blend4,则位置为:"C:\Program Files\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries"

如果使用的是Blend3,则位置为:"C:\Program Files\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF"

3. 向项目中添加一个新的类文件.并命名为"DrapControlLibrary.cs",该文件的源码如下

  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Windows.Interactivity;
  12. namespace DrapControlLibrary
  13. {
  14. public class DragInCanvasBehavior : Behavior<UIElement>
  15. {
  16. private Canvas canvas;
  17. protected override void OnAttached()
  18. {
  19. base.OnAttached();
  20. // Hook up event handlers.
  21. this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
  22. this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;
  23. this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
  24. }
  25. protected override void OnDetaching()
  26. {
  27. base.OnDetaching();
  28. // Detach event handlers.
  29. this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
  30. this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
  31. this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
  32. }
  33. // Keep track of when the element is being dragged.
  34. private bool isDragging = false;
  35. // When the element is clicked, record the exact position
  36. // where the click is made.
  37. private Point mouseOffset;
  38. private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  39. {
  40. // Find the canvas.
  41. if (canvas == null) canvas = VisualTreeHelper.GetParent(this.AssociatedObject) as Canvas;
  42. // Dragging mode begins.
  43. isDragging = true;
  44. // Get the position of the click relative to the element
  45. // (so the top-left corner of the element is (0,0).
  46. mouseOffset = e.GetPosition(AssociatedObject);
  47. // Capture the mouse. This way you'll keep receiveing
  48. // the MouseMove event even if the user jerks the mouse
  49. // off the element.
  50. AssociatedObject.CaptureMouse();
  51. }
  52. private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
  53. {
  54. if (isDragging)
  55. {
  56. // Get the position of the element relative to the Canvas.
  57. Point point = e.GetPosition(canvas);
  58. // Move the element.
  59. AssociatedObject.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);
  60. AssociatedObject.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);
  61. }
  62. }
  63. private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  64. {
  65. if (isDragging)
  66. {
  67. AssociatedObject.ReleaseMouseCapture();
  68. isDragging = false;
  69. }
  70. }
  71. }
  72. }

4. 编译生成类库文件,类库文件名称为"DrapControlLibrary.dll"


制作测试程序

1. 使用Blend4新建一个WPF程序

2. 修改主XAML文件,修改后的结果如下

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  5. xmlns:custom="clr-namespace:DrapControlLibrary;assembly=DrapControlLibrary" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
  6. x:Class="DrapControlTest.MainWindow"
  7. x:Name="Window"
  8. Title="DrapControlTest"
  9. Width="300" Height="250" ResizeMode="CanMinimize" Background="White">
  10. <Canvas>
  11. <TreeView Canvas.Left="10" Canvas.Top="80" Width="200" Height="50">
  12. <i:Interaction.Behaviors>
  13. <custom:DragInCanvasBehavior/>
  14. </i:Interaction.Behaviors>
  15. </TreeView>
  16. <ListView Canvas.Left="10" Canvas.Top="15" Width="200" Height="50">
  17. <i:Interaction.Behaviors>
  18. <custom:DragInCanvasBehavior/>
  19. </i:Interaction.Behaviors>
  20. </ListView>
  21. <Rectangle Canvas.Left="10" Canvas.Top="10" Fill="Yellow" Width="40" Height="60"/>
  22. <Ellipse Canvas.Left="10" Canvas.Top="70" Fill="Blue" Width="80" Height="60">
  23. <i:Interaction.Behaviors>
  24. <custom:DragInCanvasBehavior/>
  25. </i:Interaction.Behaviors>
  26. </Ellipse>
  27. <Ellipse Canvas.Left="80" Canvas.Top="70" Fill="OrangeRed" Width="40" Height="70">
  28. <i:Interaction.Behaviors>
  29. <custom:DragInCanvasBehavior/>
  30. </i:Interaction.Behaviors>
  31. </Ellipse>
  32. </Canvas>
  33. </Window>

3. 编译文件,能够看到效果了吧?是不是很帅

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【WPF学习】第三十八章 行为
一步一步学Silverlight 2系列(5):实现简单的拖放功能
使用silverlight构建一个工作流设计器(二)
Silverlight MMORPG网页游戏开发课程[一期] 第一课:控制对象移动
MVVM中轻松实现Command绑定(五)获取事件参数EventArgs(2)
180行JavaScript代码实现的小球随机移动代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服