关闭父时关闭子对话框 [英] Close child dialogs when closing the parent

查看:120
本文介绍了关闭父时关闭子对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Windows外壳扩展在C#中使用 EZShellExtensions.NET

I'm writing a Windows shell extension in C# using EZShellExtensions.NET.

我贡献的上下文菜单显示的对话框。

I contribute a context menu that shows dialogs.

假设我展示一个资源管理器窗口(A)。然后,我使用上下文菜单显示非模态窗口(B)。

Suppose that I show an Explorer window (A). Then I use the context menu to show non modal window (B).

在Windows XP和Windows Vista中,当我关闭A,B被关闭(我想这种行为)。然而,在Windows 7中,当我关闭A,B没有关闭,但它并没有对事件做出响应。我的问题是:

In Windows XP and Windows Vista, when I close A, B is closed (I want this behavior). However, in Windows 7, when I close A, B is not closed but it doesn't respond to events. My questions are:

  • 你知道为什么Windows 7的管理表现形式作为一个子窗体?
  • 有没有办法维持消息循环,如果关闭一个?

修改:如果我设置一个作为所有者的B,当我关闭A,B也将关闭。但它创建一个<一个href="http://stackoverflow.com/questions/7008411/how-to-completely-bring-to-front-a-winform-when-activated">new发行。 B是始终在一个。

EDIT: If I set A as owner of B, when I close A, B is also closed. But it creates a new issue. B is always over A.

推荐答案

最后,我在下面的方式来实现它。使用的ShowDialog()显示该对话框,但启动(并创造了一个线程)。 的ShowDialog()实现自己的消息循环,这样的形式在一个线程推出的主要形式,对事件作出响应,也可以关闭主窗体和子形式仍然对事件作出响应。这为ShellExtension应用非常有用的。

Finally I have implemented it in the following way. The dialog is shown using ShowDialog() but is launched (and created in a thread). ShowDialog() implements its own message loop, so as the form is launched in a thread, the main form respond to events, and also you can close the main form and the child form still respond to events. This is very useful for a ShellExtension application.

记住处置所有的表格上,以释放线程,而且外壳扩展线程(每个外壳扩展窗口和孩子的在一个线程中执行)。

Remember dispose all on your form, in order to free the thread, and also the shell extension thread (each shell extension window and childs are executed in a thread).

下面code固定我的问题:

The following code fixed my issue:

    protected virtual void SetupViewControl()
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoSetupViewControl));

        while (!mViewControlCreated)
        {
            // wait for view control created
            Thread.Sleep(100);
        }
    }

    private bool mViewControlCreated = false;

    protected virtual void DoSetupViewControl(object state)
    {
        mViewControl = ViewControlFactory.CreateViewControl();

        mViewControl.Dock = DockStyle.Fill;
        mViewControl.Initialize();

        this.Controls.Clear();
        this.Controls.Add(mViewControl);

        IntPtr wHnd = GetActiveWindow();
        IWin32Window owner = GetOwner(wHnd);

        mViewControlCreated = true;

        ShowDialog(owner);

        this.Dispose();
    }

    private IWin32Window GetOwner(IntPtr wHnd)
    {
        if (wHnd == IntPtr.Zero) return null;

        return new WindowWrapper(wHnd);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern IntPtr GetActiveWindow();

    private class WindowWrapper : IWin32Window
    {
        private IntPtr mHwnd;

        public WindowWrapper(IntPtr handle)
        {
            mHwnd = handle;
        }

        public IntPtr Handle
        {
            get { return mHwnd; }
        }
    }

这篇关于关闭父时关闭子对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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