c 拖動功能

2022-02-07 03:46:39 字數 1554 閱讀 8439

需求:放在圖層上乙個,要實現滑鼠可以選中,並實現拖放功能。

1、採用winform方式實現;

2、需要乙個picturebox物件,對該picturebox新增mousemove,mousedown,mouseup事件。

3、mousedown事件中:當滑鼠在picturebox物件中左鍵按下時,記錄下滑鼠相對picturebox左上角的座標點mousedownpoint;

4、mousemove事件中:先獲取到當前滑鼠所在位置,並減去滑鼠左鍵按下時相對picturebox左上角的座標點,就是當前座標要停留的座標點位置。

在vs2010建立winform project,設定窗體如下:

執行結果:

儲存當滑鼠在picturebox中左鍵按下的位置,相對於picturebox左上角座標點的位置來講。

34private point mousedownpoint = new

point();

35//

就當前被操作的物件,實際上這裡也可以不記錄,統一使用事件中的sender物件轉化。

36private control selectedcontrol = new

control();

3738

///39

///當滑鼠在picturebox範圍內,並且按下左鍵時,記錄滑鼠相對於picturebox左上角座標點的位置(相對picturebox的座標點)。

40///

41///

42///

43void mousedown(object

sender, mouseeventargs e)

4453}54

55void mousemove(object

sender, mouseeventargs e)

5665}66

67void mouseup(object

sender, mouseeventargs e)

6871

}72 }

廣告區域的拖動功能

廣告區域的拖動功能就是一塊可以拖動的可以放置廣告的地方。其中關鍵點在於滑鼠的三個事件onmousedown,onmousemove和onmouseup。另外乙個關鍵點在於獲取滑鼠按下時,滑鼠離廣告區域的邊框的距離。依舊是先上效果圖,再上解決思路,最後上源 效果圖 初始進入頁面的效果 滑鼠拖動後的效果...

WPF實現滑鼠拖動框選功能

本例項主要是使用wpf實現拖動滑鼠畫出矩形框同時框選控制項的功能,效果如下 1 首先canvas裡放一些矩形控制項來做示例,新增滑鼠按下 抬起 移動事件 2 新增全域性變數 private border currentboxselectedborder null 拖動展示的提示框 private b...

拖拽功能 拖動列表中的項

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...