c# - WPF:应用程序关闭时关闭辅助窗口而无需 "programmer"干预

标签 c# wpf interop window wndproc

标题中的这一点很难解释,如果有人想更改它也没关系。

我遇到过这样的情况,在 WPF 中,我创建了一个对程序员透明的“隐藏”窗口。 我的意思是,这个窗口是在静态构造函数中创建的,隐藏并移动到屏幕之外,它的宽度和高度都是 0。这是因为我使用这个窗口来执行一些互操作操作,并允许对所有窗口进行某种处理程序WndProcs 重写某人可能需要的(有一个委托(delegate)列表,用于处理应该重写 WndProc 的方法)。

希望您能理解我所说的内容(这并不容易),我的问题是,当我创建一个 WPF 项目并启动它时,如果我关闭主窗口(这不是透明创建的) 给程序员),我希望我的应用程序关闭。但是,使用我创建的代码,除非我使用 Application.Current.Shutdown(); ,否则不会发生这种情况;

有没有办法在不调用该方法的情况下解决这个问题?我想要一种透明的方式,其他程序员甚至不应该注意到(它是一个库,不应该以这种方式改变工作程序的行为)。

感谢您的任何建议,在这里您可以看到一些代码片段:

由库创建的窗口

public class InteropWindow : Window
{
    public HwndSource Source { get; protected set; }

    private static InteropWindow _Instance;

    static InteropWindow()
    {
        _WndProcs = new LinkedList<WndProcHandler>();
        _Instance = new InteropWindow();
    }

    private static WindowInteropHelper _InteropHelper;
    public static WindowInteropHelper InteropHelper
    {
        get
        {
            if (_InteropHelper == null)
            {
                _InteropHelper = new WindowInteropHelper(_Instance);
                _InteropHelper.EnsureHandle();
            }
            return _InteropHelper;
        }
    }

    public static IntPtr Handle { get { return InteropHelper.Handle; } }

    private InteropWindow()
    {
        Opacity = 0.0;
        //We have to "show" the window in order to obtain hwnd to process WndProc messages in WPF
        Top = -10;
        Left = -10;
        Width = 0;
        Height = 0;
        WindowStyle = WindowStyle.None;
        ShowInTaskbar = false;
        ShowActivated = false;
        Show();
        Hide();
    }

    private static LinkedList<WndProcHandler> _WndProcs;
    public static void AddWndProcHandler(WndProcHandler handler)
    {
        _WndProcs.AddLast(handler);
    }
    public static void RemoveWndProcHandler(WndProcHandler handler)
    {
        _WndProcs.Remove(handler);
    }

    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        IntPtr result = IntPtr.Zero;
        foreach (WndProcHandler handler in _WndProcs)
        {
            IntPtr tmp = handler(hwnd, msg, wParam, lParam, ref handled);
            if (tmp != IntPtr.Zero)
            {
                if (result != IntPtr.Zero)
                    throw new InvalidOperationException(string.Format("result should be zero if tmp is non-zero:\nresult: {0}\ntmp: {1}", result.ToInt64().ToString(), tmp.ToInt64().ToString()));
                result = tmp;
            }
        }
        return result;
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        Source = PresentationSource.FromVisual(this) as HwndSource;
        Source.AddHook(WndProc);
        OnWindowInitialized(null, e);
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        if (Source != null)
            Source.RemoveHook(WndProc);
        OnWindowClosed(null, e);
    }

    private static void OnWindowInitialized(object sender, EventArgs e)
    {
        if (WindowInitialized != null) WindowInitialized(sender, e);
    }

    private static void OnWindowClosed(object sender, EventArgs e)
    {
        if (WindowClosed != null) WindowClosed(sender, e);
    }

    public static event EventHandler WindowInitialized;

    public static event EventHandler WindowClosed;
}

使用wpf创建的普通窗口(从项目创建的基本窗口)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ExClipboard.ClipboardUpdate += new RoutedEventHandler(ExClipboard_ClipboardUpdate);
        Closed += new EventHandler(MainWindow_Closed);
    }

    private void MainWindow_Closed(object sender, EventArgs e)
    {
        //InteropWindow.Dispose();
        App.Current.Shutdown(0);
    }
}

更新1:

为了回答你的答案,不,我想避免程序员使用我的库进行任何干预,所以理想的解决方案是在我的库中我订阅一些 Application.Exit 事件并关闭我的窗口,显然我不能使用 Application.Exit 因为应用程序由于我的窗口未关闭而未关闭

也许有一种方法可以计算属于应用程序的所有窗口?我也可以用它做点什么

最佳答案

如果你有主窗口,不能设置Application.ShutdownMode吗?至OnMainWindowClose

默认值为 OnLastWindowClose,这很可能是您看到此行为的原因。

关于c# - WPF:应用程序关闭时关闭辅助窗口而无需 "programmer"干预,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720294/

相关文章:

.net - 导出类中的 std::Vector 成员

c# - 如何将 C# 委托(delegate)函数传递给托管 C++ .Dll?

c# - 仅使用正则表达式在字符串中附加和前置 '#'

c# - 存储过程删除查询不起作用。但是当它自己在编辑器中运行而不调用它时它可以正常工作

c# - ICommand Execute() 任务.ContinueWith()

c# - 如何通过绑定(bind)到复选框来过滤表格项?

c# - Word 2010 Interop PDF 导出缺少边框线

java - 使用 IKVM 将 .jar 导入 .dll 并使用它

c# - 使用 Fluent API 添加唯一标识符

wpf - 如何撤消由绑定(bind)引起的 TextBox 的文本更改?