windows 处理拖动时是否可以更改鼠标光标(来自 DragOver 事件)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6175408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:57:32  来源:igfitidea点击:

Is it possible to change mouse cursor when handling drag (from DragOver event)?

c#.netwindowswinformsdrag-and-drop

提问by Marek

We need to display a feedback to the user when he drags-in items into our application. Our client prefers this feedback to be in form of a custom cursor.

当用户将项目拖入我们的应用程序时,我们需要向用户显示反馈。我们的客户更喜欢以自定义光标的形式提供此反馈。

This is already implemented for drag-out, using a custom cursor that is set in GiveFeedbackevent handler (raised by DoDragDropwhen dragging items out of our app). The GiveFeedbackEventArgsallows us to specify UseDefaultCursorsproperty - setting this to false allows us to override the cursor.

这已经为拖出实现了,使用在GiveFeedback事件处理程序中设置的自定义光标(DoDragDrop在将项目拖出我们的应用程序时引发)。将GiveFeedbackEventArgs允许我们指定UseDefaultCursors属性-设置为false,使我们可以覆盖光标。

However, the DragOverevent handler argument, which is the equivalent of GiveFeedback, does not have UseDefaultCursorsproperty and changing the cursor from there does not have any effect.

但是,与DragOver等效的事件处理程序参数GiveFeedback没有UseDefaultCursors属性,并且从那里更改光标没有任何效果。

Sample (this has no effect):

示例(这没有效果):

private void browser_DragOver(object sender, DragEventArgs e) {
  Cursor.Current = Cursors.WaitCursor;
}

The drag operation originates from outside our app. (for in-app drag, it works using the GiveFeedbackevent.

拖动操作源自我们的应用程序外部。(对于应用内拖动,它使用GiveFeedback事件。

How to change the cursor when receiving a drag? Is this even possible/feasible?

接收拖动时如何更改光标?这甚至可能/可行吗?

回答by Barrakoda

void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e) {
    e.UseDefaultCursors = false;
}

回答by CodeGuru

Yes, you must implement a COM interfaces (IDragSourceHelper and IDropTargetHelper). Take a look HERE.

是的,您必须实现一个 COM 接口(IDragSourceHelper 和 IDropTargetHelper)。看看这里

回答by Jakob M?ll?s

Would this suffice?
This code will change the mouse pointer by adding a [+] sign next to it.

这样就够了吗?
此代码将通过在鼠标指针旁边添加 [+] 符号来更改鼠标指针。

private void Form_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

回答by Pedery

We've addressed a whole range of drag-drop issues here:

我们在这里解决了一系列拖放问题:

Drag and Drop between Instances of the same Windows Forms Application

在同一 Windows 窗体应用程序的实例之间拖放

Hopefully this will point you in the right direction.

希望这将为您指明正确的方向。