DoDragDrop() from another thread(来自另一个线程的 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:
DoDragDrop 完全阻塞了表单,没有计时器事件跳转,没有处理任何绘制消息.
DoDragDrop completely blocking the form, no timer events jumps, no paint messages handled.
DoDragDrop 阻止不仅用于拖动 &drop 操作,但直到目标程序完成 drop 事件(即 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.
还有其他解决方案吗?
推荐答案
DoDragDrop 方法会停止处理事件,直到第一个鼠标事件(例如 mouse move).所以我找到的解决方法非常简单——你只需要在调用 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自另一个线程的 DoDragDrop()
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
