Silverlight 中用鼠标同时选中和移动多个控件

  在设计 WinForm 程序时,我们可以很方便的同时选择窗体上的多个控件来调整控件的位置。在 Silverlight 应用程序中有时我们也想实现同样的功能,以提供更好的用户体验。本文将要介绍的就是在 Silverlight 程序中实现同时选中和移动多个控件。

  1、实现鼠标拖动选择时显示所选区域

  2、移动所选区域时同时移动在该区域内的控件

  要实现鼠标拖动选择时显示所选区域功能,可以在鼠标拖动时在 Canvas 容器中动态添加一个 Rectangle 来显示类似在 Windows 资源管理器拖动选择文件时的选择框。实现前面所述功能的操作:在 Canvas 容器中按下鼠标左键并拖动鼠标来修改选择区域,选定目标区域后松开鼠标按键,这时显示一个表示所选区域的矩形选框。由实现的操作可知,我们需要在鼠标左键按下时在 Canvas 容器中添加一个矩形框,然后在鼠标移动时根据鼠标的位置更新矩形框的大小,最后在鼠标左键弹起显示最终的选择区域并查找出在选择区域中的控件。

private Point origPoint;  /* 鼠标点击的位置   */
private Rectangle rect; /* 选择的区域 */
private Rect selectedRect; /* 选择的矩形区域 */
private bool isMultipleSelected;
private List<FrameworkElement> selectedElements = new List<FrameworkElement>();

public MainPage()
{
InitializeComponent();

this.rootCanvas.MouseLeftButtonDown += Handle_MouseLeftButtonDown;
}

private void Handle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is Canvas)
{
if (rect != null)
{
isMultipleSelected
= false;

rootCanvas.Children.Remove(rect);
rect
= null;
}
else
{
isMultipleSelected
= true;

rect
= new Rectangle();
origPoint
= e.GetPosition(rootCanvas);

rootCanvas.Children.Add(rect);
rect.SetValue(Canvas.LeftProperty, origPoint.X);
rect.SetValue(Canvas.TopProperty, origPoint.Y);

rect.Fill
= new SolidColorBrush(Colors.LightGray);
rect.Stroke
= new SolidColorBrush(Colors.Black);
rect.StrokeThickness
= 3;
rect.Opacity
= .5;

rect.MouseLeftButtonDown
+= Handle_MouseLeftButtonDown;

rootCanvas.MouseMove
+= Handle_MouseMove;
rootCanvas.MouseLeftButtonUp
+= Handle_MouseLeftButtonUp;
}
}
else if (e.OriginalSource is Rectangle)
{
isMultipleSelected
= false;

origPoint
= e.GetPosition(rootCanvas);

rect.MouseMove
+= Handle_MouseMove;
rect.MouseLeftButtonUp
+= Handle_MouseLeftButtonUp;
rect.CaptureMouse();

/*
* 查找在选择范围内的控件
*
*/

double rLeft = (double)rect.GetValue(Canvas.LeftProperty);
double rTop = (double)rect.GetValue(Canvas.TopProperty);

foreach (FrameworkElement item in rootCanvas.Children)
{
double cLeft = (double)item.GetValue(Canvas.LeftProperty);
double cTop = (double)item.GetValue(Canvas.TopProperty);

Rect rc1
= new Rect(selectedRect.X, selectedRect.Y, selectedRect.Width, selectedRect.Height);
Rect rc2
= new Rect(cLeft, cTop, item.ActualWidth, item.ActualHeight);
rc1.Intersect(rc2);
/* 判断控件所在的矩形区域与选择的矩形区域是否相交 */
if (rc1 != Rect.Empty)
{
if (!selectedElements.Contains(item))
selectedElements.Add(item);
}
}
}
}

private void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isMultipleSelected)
{
/* 所选区域的矩形 */
selectedRect
= new Rect((double)rect.GetValue(Canvas.LeftProperty),
(
double)rect.GetValue(Canvas.TopProperty), rect.Width, rect.Height);

rootCanvas.MouseLeftButtonUp
-= Handle_MouseLeftButtonUp;
rootCanvas.MouseMove
-= Handle_MouseMove;
}
}

private void Handle_MouseMove(object sender, MouseEventArgs e)
{
if (isMultipleSelected)
{
Point curPoint
= e.GetPosition(rootCanvas);
if (curPoint.X > origPoint.X)
{
rect.Width
= curPoint.X - origPoint.X;
}
if (curPoint.X < origPoint.X)
{
rect.SetValue(Canvas.LeftProperty, curPoint.X);
rect.Width
= origPoint.X - curPoint.X;
}
if (curPoint.Y > origPoint.Y)
{
rect.Height
= curPoint.Y - origPoint.Y;
}
if (curPoint.Y < origPoint.Y)
{
rect.SetValue(Canvas.TopProperty, curPoint.Y);
rect.Height
= origPoint.Y - curPoint.Y;
}
}
}

NET技术Silverlight 中用鼠标同时选中和移动多个控件,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。