C#枚举桌面窗口并将桌面设为父窗口?

问题:
我想将我的winform的父窗口设为桌面窗口“WorkerW”,于是我用EnumWindows和EnumWindowsProc来枚举桌面窗口,用FindWindow和FindWindowEx来查找真正的WorkerW句柄,然而我用Spy++发现我的Winform没有成为WorkerW的子窗口,搜了好久也没有找到问题所在,现在贴上代码,请大神帮忙看看,多谢啦!
代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyExCSForm
{
    public partial class Form1 : Form
    {
        private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc hWnd, IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);        

        private bool SetMyDesktop(IntPtr hWnd,IntPtr lParam)
        {
            if (hWnd == FindWindow("WorkerW", null))
            {
                if (FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null) != IntPtr.Zero)
                {
                    SetParent(this.Handle, hWnd);
                    return false;
                } else
                {
                    return true;
                }                
            } else
            {
                return true;
            }
        }

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            EnumWindowsProc myEnumWinPro = new EnumWindowsProc(SetMyDesktop);
            EnumWindows(myEnumWinPro, IntPtr.Zero);
        }
    }
}
阅读 5.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进