本例項主要是使用wpf實現拖動滑鼠畫出矩形框同時框選控制項的功能,效果如下:
1、首先canvas裡放一些矩形控制項來做示例,新增滑鼠按下、抬起、移動事件
2、新增全域性變數
private border currentboxselectedborder = null;//拖動展示的提示框
private bool iscanmove = false;//滑鼠是否移動
private point tempstartpoint;//起始座標
3、實現滑鼠按下記錄起始點,拖動繪製選框,抬起選中邏輯
/// /// 滑鼠按下記錄起始點
///
private void maincanvas_mouseleftbuttondown(object sender, mousebuttoneventargs e)
/// /// 框選邏輯
///
private void maincanvas_mousemove(object sender, mouseeventargs e)
}/// /// 滑鼠抬起時清除選框
///
private void maincanvas_mouseleftbuttonup(object sender, mousebuttoneventargs e)
this.maincanvas.children.remove(currentboxselectedborder);
currentboxselectedborder = null;
}iscanmove = false;
}/// /// 繪製跟隨滑鼠移動的方框
///
private void drawmultiselectborder(point endpoint, point startpoint)
currentboxselectedborder.width = math.abs(endpoint.x - startpoint.x);
currentboxselectedborder.height = math.abs(endpoint.y - startpoint.y);
if (endpoint.x - startpoint.x >= 0)
canvas.setleft(currentboxselectedborder, startpoint.x);
else
canvas.setleft(currentboxselectedborder, endpoint.x);
if (endpoint.y - startpoint.y >= 0)
canvas.settop(currentboxselectedborder, startpoint.y);
else
canvas.settop(currentboxselectedborder, endpoint.y);
}/// /// 獲得所有子控制項
///
public static listgetchildobjects(system.windows.dependencyobject obj) where t : system.windows.frameworkelement
childlist.addrange(getchildobjects(child));
}return childlist;
}/// /// 恢復原來狀態
///
private void reset_click(object sender, routedeventargs e)
}
Wpf 滑鼠拖動元素例項
1.wpf中滑鼠捕獲和釋放 以矩形為例 建立滑鼠捕獲 mouse.capture rectone 釋放滑鼠捕獲 rectone.releasemousecapture 2.wpf中在定位布局canvas下獲取或設定元素的位置 在指定元素的滑鼠事件中例如,mouseleftbuttondown,mou...
WPF實現控制項拖動
實現控制項拖動的基本原理是對滑鼠位置的捕獲,同時根據滑鼠按鍵的按下 釋放確定控制項移動的幅度和時機。簡單示例 在grid中有乙個button,通過滑鼠事件改編button的margin屬性,從而改變button在grid中的相對位置。gd 90 height 30 name btn button 為...
qml滑鼠拖動 qml實現視窗拖動
在去掉視窗標題欄後視窗會失去滑鼠拖動效果,所以需要自己新增拖動效果。實現 id mainwindow visible true width 900 height 600 title qstr hello world flags qt.window qt.framelesswindowhint 去標題...