問題描述
每次我想讓用戶拖動控件時,我都會調用該控件的 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()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!