当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net DoDragDrop 方法的使用

asp.net DoDragDrop 方法的使用

2017年12月12日  | 移动技术网IT编程  | 我要评论

雪橇犬哈士奇,感谢师恩手抄报图片,金城武资料

在类库中的定义为:
复制代码 代码如下:

[uipermissionattribute(securityaction.demand, clipboard = uipermissionclipboard.ownclipboard)]
public dragdropeffects dodragdrop(
object data,
dragdropeffects allowedeffects
)

其中data参数为要拖放的数据,如果拖动操作需要于另一个进程的应用程序相互操作,data代表的数据应该是基本托管类(string,bitmap,或metafile),或者是实现 iserializable 或idataobject的对象。 allowedeffects参数表示拖放的效果,为一个枚举值(dragdropeffects).返回值也为dragdropeffects枚举值。
  当开始调用dodragdrop方法拖动一个数据对象时,dodragdrops在拖放过程中,检测当前光标位置下的控件是不是有效的放置目标。如果当前光标下的控件是有效的放置目标,则givefeedback事件以指定的拖放效果引发。在检测当前位置光标是否为有效的拖放目标时,dodragdrops方法同时跟踪光标位置,键盘状态和鼠标状态的更改。
   (1)如果用于移出了一个窗口,则引发dragleave事件。
  (2)如果移入了另外一个控件,则引发该控件的dragenter事件。
  (3)如果鼠标移动,但是停留在一个控件中,则引发dragover事件。
如果检测到更改了键盘或者鼠标状态,则引发拖放源的querycontinuedrag事件, 并根据事件的querycontinuedrageventargs的action属性值确定继续拖动,放置数据或取消操作。
(1)如果action属性指定为continue,则将引发dragover事件。
(2)如果action属性指定为drop,则将放置效果返回给源,以便应用程序对数据进行适当的操作;例如,如果是移动操作,则剪切数据。
(3)如果是dragaction的值为cancel,则引发dragleave事件
从csdn上摘抄一段示例代码:
  下面的代码示例演示在两个 listbox 控件之间的拖放操作。当拖动动作启动时,该示例调用 dodragdrop 方法。在 mousedown 事件期间,如果从鼠标位置起鼠标移动的距离大于 systeminformation..::.dragsize,则启动拖动动作。indexfrompoint 方法用于确定在 mousedown 事件期间要拖动的项的索引。
  该示例还演示如何对拖放操作使用自定义光标。该示例要求应用程序目录中存在两个光标文件:3dwarro.cur 和 3dwno.cur,分别用于自定义拖动光标和禁止停放光标。如果选中 usecustomcursorscheckcheckbox,则使用自定义光标。自定义光标在 givefeedback 事件处理程序中设置。
  键盘状态在右 listbox 的 dragover 事件处理程序中计算,以确定基于 shift、ctrl、alt 或 ctrl+alt 键的状态将发生哪种拖动操作。放置动作在 listbox 中发生的位置也在 dragover 事件期间确定。如果要放置的数据不是 string,则 dragdropeffects 中将把 drageventargs.seffect 设置为 none。最后,停放状态在 droplocationlabellabel 中显示。
  要放置的用于右 listbox 的数据在 dragdrop 事件处理程序中确定,并且在 listbox 中的适当位置添加该 string 值。如果拖动操作移动到窗体边框的外面,则 querycontinuedrag 事件处理程序中将取消拖放操作
复制代码 代码如下:

using system;
using system.drawing;
using system.windows.forms;
namespace snip_dragndrop
{
public class form1 : system.windows.forms.form
{
private system.windows.forms.listbox listdragsource;
private system.windows.forms.listbox listdragtarget;
private system.windows.forms.checkbox usecustomcursorscheck;
private system.windows.forms.label droplocationlabel;
private int indexofitemundermousetodrag;
private int indexofitemundermousetodrop;
private rectangle dragboxfrommousedown;
private point screenoffset;
private cursor mynodropcursor;
private cursor mynormalcursor;
/// the main entry point for the application.
[stathread]
static void main()
{
application.run(new form1());
}
public form1()
{
this.listdragsource = new system.windows.forms.listbox();
this.listdragtarget = new system.windows.forms.listbox();
this.usecustomcursorscheck = new system.windows.forms.checkbox();
this.droplocationlabel = new system.windows.forms.label();
this.suspendlayout();
// listdragsource
this.listdragsource.items.addrange(new object[] {"one", "two", "three", "four",
"five", "six", "seven", "eight",
"nine", "ten"});
this.listdragsource.location = new system.drawing.point(10, 17);
this.listdragsource.size = new system.drawing.size(120, 225);
this.listdragsource.mousedown += new system.windows.forms.mouseeventhandler(this.listdragsource_mousedown);
this.listdragsource.querycontinuedrag += new system.windows.forms.querycontinuedrageventhandler(this.listdragsource_querycontinuedrag);
this.listdragsource.mouseup += new system.windows.forms.mouseeventhandler(this.listdragsource_mouseup);
this.listdragsource.mousemove += new system.windows.forms.mouseeventhandler(this.listdragsource_mousemove);
this.listdragsource.givefeedback += new system.windows.forms.givefeedbackeventhandler(this.listdragsource_givefeedback);
// listdragtarget
this.listdragtarget.allowdrop = true;
this.listdragtarget.location = new system.drawing.point(154, 17);
this.listdragtarget.size = new system.drawing.size(120, 225);
this.listdragtarget.dragover += new system.windows.forms.drageventhandler(this.listdragtarget_dragover);
this.listdragtarget.dragdrop += new system.windows.forms.drageventhandler(this.listdragtarget_dragdrop);
this.listdragtarget.dragenter += new system.windows.forms.drageventhandler(this.listdragtarget_dragenter);
this.listdragtarget.dragleave += new system.eventhandler(this.listdragtarget_dragleave);
// usecustomcursorscheck
this.usecustomcursorscheck.location = new system.drawing.point(10, 243);
this.usecustomcursorscheck.size = new system.drawing.size(137, 24);
this.usecustomcursorscheck.text = "use custom cursors";
// droplocationlabel
this.droplocationlabel.location = new system.drawing.point(154, 245);
this.droplocationlabel.size = new system.drawing.size(137, 24);
this.droplocationlabel.text = "none";
// form1
this.clientsize = new system.drawing.size(292, 270);
this.controls.addrange(new system.windows.forms.control[] {this.listdragsource,
this.listdragtarget, this.usecustomcursorscheck,
this.droplocationlabel});
this.text = "drag-and-drop example";
this.resumelayout(false);
}
private void listdragsource_mousedown(object sender, system.windows.forms.mouseeventargs e)
{
// get the index of the item the mouse is below.
indexofitemundermousetodrag = listdragsource.indexfrompoint(e.x, e.y);
if (indexofitemundermousetodrag != listbox.nomatches) {
// remember the point where the mouse down occurred. the dragsize indicates
// the size that the mouse can move before a drag event should be started.
size dragsize = systeminformation.dragsize;
// create a rectangle using the dragsize, with the mouse position being
// at the center of the rectangle.
dragboxfrommousedown = new rectangle(new point(e.x - (dragsize.width /2),
e.y - (dragsize.height /2)), dragsize);
} else
// reset the rectangle if the mouse is not over an item in the listbox.
dragboxfrommousedown = rectangle.empty;
}
private void listdragsource_mouseup(object sender, system.windows.forms.mouseeventargs e) {
// reset the drag rectangle when the mouse button is raised.
dragboxfrommousedown = rectangle.empty;
}
private void listdragsource_mousemove(object sender, system.windows.forms.mouseeventargs e)
{
if ((e.button & mousebuttons.left) == mousebuttons.left) {
// if the mouse moves outside the rectangle, start the drag.
if (dragboxfrommousedown != rectangle.empty &&
!dragboxfrommousedown.contains(e.x, e.y)) {
// create custom cursors for the drag-and-drop operation.
try {
mynormalcursor = new cursor("3dwarro.cur");
mynodropcursor = new cursor("3dwno.cur");
} catch {
// an error occurred while attempting to load the cursors, so use
// standard cursors.
usecustomcursorscheck.checked = false;
}finally {
// the screenoffset is used to account for any desktop bands
// that may be at the top or left side of the screen when
// determining when to cancel the drag drop operation.
screenoffset = systeminformation.workingarea.location;
// proceed with the drag-and-drop, passing in the list item.
dragdropeffects dropeffect = listdragsource.dodragdrop(listdragsource.items[indexofitemundermousetodrag], dragdropeffects.all | dragdropeffects.link);
// if the drag operation was a move then remove the item.
if (dropeffect == dragdropeffects.move) {
listdragsource.items.removeat(indexofitemundermousetodrag);
// selects the previous item in the list as long as the list has an item.
if (indexofitemundermousetodrag > 0)
listdragsource.selectedindex = indexofitemundermousetodrag -1;
else if (listdragsource.items.count > 0)
// selects the first item.
listdragsource.selectedindex =0;
}
// dispose of the cursors since they are no longer needed.
if (mynormalcursor != null)
mynormalcursor.dispose();
if (mynodropcursor != null)
mynodropcursor.dispose();
}
}
}
}
private void listdragsource_givefeedback(object sender, system.windows.forms.givefeedbackeventargs e)
{
// use custom cursors if the check box is checked.
if (usecustomcursorscheck.checked) {
// sets the custom cursor based upon the effect.
e.usedefaultcursors = false;
if ((e.effect & dragdropeffects.move) == dragdropeffects.move)
cursor.current = mynormalcursor;
else
cursor.current = mynodropcursor;
}
}
private void listdragtarget_dragover(object sender, system.windows.forms.drageventargs e)
{
// determine whether string data exists in the drop data. if not, then
// the drop effect reflects that the drop cannot occur.
if (!e.data.getdatapresent(typeof(system.string))) {
e.effect = dragdropeffects.none;
droplocationlabel.text = "none - no string data.";
return;
}
// set the effect based upon the keystate.
if ((e.keystate & (8+32)) == (8+32) &&
(e.allowedeffect & dragdropeffects.link) == dragdropeffects.link) {
// keystate 8 + 32 = ctl + alt
// link drag-and-drop effect.
e.effect = dragdropeffects.link;
} else if ((e.keystate & 32) == 32 &&
(e.allowedeffect & dragdropeffects.link) == dragdropeffects.link) {
// alt keystate for link.
e.effect = dragdropeffects.link;
} else if ((e.keystate & 4) == 4 &&
(e.allowedeffect & dragdropeffects.move) == dragdropeffects.move) {
// shift keystate for move.
e.effect = dragdropeffects.move;
} else if ((e.keystate & 8) == 8 &&
(e.allowedeffect & dragdropeffects.copy) == dragdropeffects.copy) {
// ctl keystate for copy.
e.effect = dragdropeffects.copy;
} else if ((e.allowedeffect & dragdropeffects.move) == dragdropeffects.move) {
// by default, the drop action should be move, if allowed.
e.effect = dragdropeffects.move;
} else
e.effect = dragdropeffects.none;
// get the index of the item the mouse is below.
// the mouse locations are relative to the screen, so they must be
// converted to client coordinates.
indexofitemundermousetodrop =
listdragtarget.indexfrompoint(listdragtarget.pointtoclient(new point(e.x, e.y)));
// updates the label text.
if (indexofitemundermousetodrop != listbox.nomatches){
droplocationlabel.text = "drops before item #" + (indexofitemundermousetodrop + 1);
} else
droplocationlabel.text = "drops at the end.";
}
private void listdragtarget_dragdrop(object sender, system.windows.forms.drageventargs e)
{
// ensure that the list item index is contained in the data.
if (e.data.getdatapresent(typeof(system.string))) {
object item = (object)e.data.getdata(typeof(system.string));
// perform drag-and-drop, depending upon the effect.
if (e.effect == dragdropeffects.copy ||
e.effect == dragdropeffects.move) {
// insert the item.
if (indexofitemundermousetodrop != listbox.nomatches)
listdragtarget.items.insert(indexofitemundermousetodrop, item);
else
listdragtarget.items.add(item);
}
}
// reset the label text.
droplocationlabel.text = "none";
}
private void listdragsource_querycontinuedrag(object sender, system.windows.forms.querycontinuedrageventargs e) {
// cancel the drag if the mouse moves off the form.
listbox lb = sender as listbox;
if (lb != null) {
form f = lb.findform();
// cancel the drag if the mouse moves off the form. the screenoffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((control.mouseposition.x - screenoffset.x) < f.desktopbounds.left) ||
((control.mouseposition.x - screenoffset.x) > f.desktopbounds.right) ||
((control.mouseposition.y - screenoffset.y) < f.desktopbounds.top) ||
((control.mouseposition.y - screenoffset.y) > f.desktopbounds.bottom)) {
e.action = dragaction.cancel;
}
}
}
private void listdragtarget_dragenter(object sender, system.windows.forms.drageventargs e) {
// reset the label text.
droplocationlabel.text = "none";
}
private void listdragtarget_dragleave(object sender, system.eventargs e) {
// reset the label text.
droplocationlabel.text = "none";
}
}
}

对用这种拖放操作和微软的服务,容器模式的关系,留在以后再学习。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网