DoDragDrop()从另一个线程 [英] DoDragDrop() from another thread

查看:152
本文介绍了DoDragDrop()从另一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我想让用户拖动一个控件,我调用该控件的DoDragDrop。

Every time i want let the user to drag an control, i calling DoDragDrop of that control.

拖动& Drop可以正常工作,但是我有问题:

The drag & drop works fine, but i have problem with things around:


  1. DoDragDrop完全阻止窗体,没有定时器事件跳转,没有画消息处理。

  1. DoDragDrop completely blocking the form, no timer events jumps, no paint messages handled.

DoDragDrop阻止不仅用于拖动&放弃操作,但直到目标程序完成与drop事件(I.E. explorer.exe的吮吸代码)。

DoDragDrop blocking not only for the drag & drop operation, but until target program finishing with the drop event (I.E. explorer.exe's suck code). Depending on other program's code is sucks.

我以为从一个新线程调用DoDragDrop。

I thought to call DoDragDrop from a new thread.

尝试这样:

Thread dragThread = new Thread(() =>
{
    Form frm = new Form();
    frm.DoDragDrop("data", DragDropEffects.All);
});

dragThread.SetApartmentState(ApartmentState.STA);
dragThread.IsBackground = true;
dragThread.Start();

但似乎不起作用。我的意思是:当从其他线程执行DoDragDrop时,我的程序或其他程序中的其他控件不会收到拖放消息。

but it doesn't seems to work. I mean: when doing DoDragDrop from other thread like this, other controls within my program or other programs does not receiving drag&drop messages.

任何其他解决方案? >

Any other solutions?

推荐答案

DoDragDrop 方法停止对事件的处理,直到第一个鼠标事件(例如 EM>)。所以我发现的解决方法很简单 - 你只需要在调用DoDragDrop之前,使用相同的鼠标位置来模拟鼠标事件:

The DoDragDrop method stops processing of events until first mouse event (for example mouse move). So the workaround I found is very simple - you just need to simulate mouse event with the same mouse position just before calling DoDragDrop:


void XYZControl_MouseDown(object sender, MouseEventArgs e)
{
    var senderControl = (Control) sender;
    ...
    Cursor.Position = senderControl.PointToScreen(new Point(e.X, e.Y));   // Workaround!
    if (DoDragDrop(senderControl, DragDropEffects.Move) == DragDropEffects.Move)
    {
    ...
    }
....
}

这篇关于DoDragDrop()从另一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆