四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

Silverlight 中用鼠標(biāo)同時選中和移動多個控件

  在設(shè)計 WinForm 程序時,我們可以很方便的同時選擇窗體上的多個控件來調(diào)整控件的位置。在 Silverlight 應(yīng)用程序中有時我們也想實現(xiàn)同樣的功能,以提供更好的用戶體驗。本文將要介紹的就是在 Silverlight 程序中實現(xiàn)同時選中和移動多個控件。

  1、實現(xiàn)鼠標(biāo)拖動選擇時顯示所選區(qū)域

  2、移動所選區(qū)域時同時移動在該區(qū)域內(nèi)的控件

  要實現(xiàn)鼠標(biāo)拖動選擇時顯示所選區(qū)域功能,可以在鼠標(biāo)拖動時在 Canvas 容器中動態(tài)添加一個 Rectangle 來顯示類似在 Windows 資源管理器拖動選擇文件時的選擇框。實現(xiàn)前面所述功能的操作:在 Canvas 容器中按下鼠標(biāo)左鍵并拖動鼠標(biāo)來修改選擇區(qū)域,選定目標(biāo)區(qū)域后松開鼠標(biāo)按鍵,這時顯示一個表示所選區(qū)域的矩形選框。由實現(xiàn)的操作可知,我們需要在鼠標(biāo)左鍵按下時在 Canvas 容器中添加一個矩形框,然后在鼠標(biāo)移動時根據(jù)鼠標(biāo)的位置更新矩形框的大小,最后在鼠標(biāo)左鍵彈起顯示最終的選擇區(qū)域并查找出在選擇區(qū)域中的控件。

private Point origPoint;  /* 鼠標(biāo)點擊的位置   */
private Rectangle rect; /* 選擇的區(qū)域 */
private Rect selectedRect; /* 選擇的矩形區(qū)域 */
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();

/*
* 查找在選擇范圍內(nèi)的控件
*
*/

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);
/* 判斷控件所在的矩形區(qū)域與選擇的矩形區(qū)域是否相交 */
if (rc1 != Rect.Empty)
{
if (!selectedElements.Contains(item))
selectedElements.Add(item);
}
}
}
}

private void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isMultipleSelected)
{
/* 所選區(qū)域的矩形 */
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技術(shù)Silverlight 中用鼠標(biāo)同時選中和移動多個控件,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 原创视频| 乔治爸爸去哪儿| 改变自己吉他谱| 恶女台剧| 三级大片在线观看| 多少周开始做胎心监护| 搜狐视频官网| 疯狂试爱2| 绝路逢生| 心经原文全文| 搜狐网站官网| 1987年美国电影| 情人电影在线观看高清完整版泰剧| 8号房间| outlander| 1998年槟榔西施| 世界轮廓图| 视频爱爱| 曲丹个人资料简介| 古灵精探演员表| 都市频道节目表| 刘慧| 佐格| 长安长安朗诵报名入口| 爱你的主题曲| 生死瞬间演员表| 国产伦理女村支书| 德国老太性视频播放| www.douyin.com| 救急战队| 时来运转电影| 免费观看美国破釜沉舟电影| 白皮书电影| 大决战全部演员表介绍图片| 战长沙每个人的结局| ab变频器中文说明书| 德国老太性视频播放| 十三刺客| 东方卫视节目表| 风间由美电影影片| 帕瓦德奥特曼|