大家好 我想问一下 我把自已的C# 的窗体SetParent到另一个窗口里 但要是那个窗体关闭后我的窗体也被关掉了,这样的情况如何解决呀 防止我的程序不被关闭不知道在C#里能不能HOOK 拦截到那个目标窗口的WM_CLOSE消息 当拦到这个消息时再SETPARENT回来 ?
如果可以话 那个 Setwindowhookex(idhook? 这个参数取值什么呀?
或者,还有其它的什么方法??

解决方案 »

  1.   

    没有多少原来,就是通过打消息日志分析
    看到会接收到两次WM_DESTROY消息,然后排列组合测试的结果
    参考如下代码:
    using System.Runtime.InteropServices;[DllImport("user32.dll")]
    public static extern int SetParent(IntPtr wnd, IntPtr newParentWnd);[DllImport("user32.DLL")]
    public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);[DllImport("user32.dll")]
    private static extern IntPtr GetParent(IntPtr hwnd);IntPtr parentHandle = IntPtr.Zero;private void button1_Click(object sender, EventArgs e)
    {
        parentHandle = FindWindow("Notepad", null);
        if (parentHandle != IntPtr.Zero)
            SetParent(Handle, parentHandle);
    }protected override void WndProc(ref Message m)
    {
        const int WM_DESTROY = 0x0002;
        switch (m.Msg)
        {
            case WM_DESTROY: // 当父窗体析构的时候会收到消息
                if (parentHandle != IntPtr.Zero)
                {
                    SetParent(Handle, IntPtr.Zero);
                    parentHandle = IntPtr.Zero;
                    RecreateHandle(); // 重新复活一次
                    return;
                }
                break;
        }
        base.WndProc(ref m);
    }