为什么我的表单会在DragDrop上显示背景? [英] Why is my form going to background on DragDrop?

查看:122
本文介绍了为什么我的表单会在DragDrop上显示背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在拖放操作结束时,我正在显示使用ShowDialog的表单.
问题:关闭表单后,我的主表单被推到任何其他应用程序窗口的后面.

At the end of a drag&drop operation, I'm showing a form using ShowDialog.
Problem: When the form is closed, my main form is pushed behind any other application windows.

代码:

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);
    // ...
}

问题:我该怎么做才能使主要表单停留在最前面?

推荐答案

您的ShowDialog()调用 blocking DragDrop事件.这非常非常糟糕,它会拖累拖动源并使它变成阳离子,并且对Windows消息无响应.这具有各种副作用,例如您的窗口也会发生阳离子变化,或者由于D + D操作尚未完成而没有被重新激活.

Your ShowDialog() call is blocking the DragDrop event. That is very, very bad, it gums up the drag source and make it go catatonic and unresponsive to Windows messages. 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操作之后显示 对话框来避免这种情况.利用Winforms管道的出色表现,该管道允许将消息发布到消息队列中,并在以后进行处理.像这样:

Avoid this by only displaying the dialog after the D+D operation is completed. Elegantly done by taking advantage of the Winforms plumbing that allows posting a message to the message queue and get it processed later. Like this:

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

这篇关于为什么我的表单会在DragDrop上显示背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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