简体   繁体   English

为什么我的表单会在DragDrop上显示背景?

[英]Why is my form going to background on DragDrop?

At the end of a drag&drop operation, I'm showing a form using ShowDialog . 拖放操作结束时,我正在使用ShowDialog显示一个表单。
Problem: When the form is closed, my main form is pushed behind any other application windows. 问题:关闭窗体后,我的主窗体被推到任何其他应用程序窗口的后面。

Code: 码:

private void ctrl_DragDrop(object sender, DragEventArgs e) {
    // ...
    if (e.Effect == DragDropEffects.Move) {
    string name = e.Data.GetData(DataFormats.Text).ToString();
    viewHelperForm.ShowDialog(view.TopLevelControl);
    // ...
}

Question: What can I do that the main form stays on top? 问题:我该怎么做才能使主要表格停留在最前面?

Your ShowDialog() call is blocking the DragDrop event. 您的ShowDialog()调用阻止了DragDrop事件。 That is very, very bad, it gums up the drag source and make it go catatonic and unresponsive to Windows messages. 这非常非常糟糕,它会拖累拖动源并使它变成阳离子,并且对Windows消息无响应。 This has all kinds of side-effects, like your window going catatonic as well or not getting reactivated since the D+D operation isn't completed yet. 这会产生各种副作用,例如您的窗户也会发生阳离子变化,或者由于D + D操作尚未完成而无法重新激活。

Avoid this by only displaying the dialog after the D+D operation is completed. 仅完成d + d操作之后显示的对话框中避免这种情况。 Elegantly done by taking advantage of the Winforms plumbing that allows posting a message to the message queue and get it processed later. 利用Winforms管道的出色表现,该管道允许将消息发布到消息队列中,并在以后进行处理。 Like this: 像这样:

    private void ctl_DragDrop(object sender, DragEventArgs e) {
        //...
        this.BeginInvoke(new Action(() => {
            viewHelperForm.ShowDialog(view.TopLevelControl);
        }));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM