问题:
我想将我的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);
}
}
}
SHELLDLL_DefView
是explorer.exe进程的一个窗口真正的桌面窗口是#32769,GetDesktopWindow可以获取其句柄
SetParent(this.Handle, IntPtr.Zero)
即可设置Parent为桌面窗口事实上非WS_CHILD窗口的Parent默认都是桌面窗口