6

我正在创建一个桌面小工具,但遇到了问题。该窗口将被“显示桌面”命令隐藏 - 停止,我知道你在想什么,不需要“你不应该这样做”的评论 - 我想停止它。毕竟,桌面小工具的全部意义在于它粘在桌面上。

只是为了澄清 - 我不想要 TopMost 窗口。我不想真正停止“显示桌面”命令,只是忽略它。我想要的只是让我的桌面小工具在桌面上保持可见,像往常一样破坏正常功能。

有任何想法吗?我当前的方法是我在 Google 上找到的 P/Invoke 片段,将表单的父级设置为 Progman 或其他东西。问题是这似乎强制在任务栏中显示窗口,这是我不想要的。

4

2 回答 2

8

回答你的问题可能有点晚了,但我似乎已经找到了答案:

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hP, IntPtr hC, string sC, string sW);

    void MakeWin()
    {
        IntPtr nWinHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
        nWinHandle = FindWindowEx(nWinHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
        SetParent(Handle, nWinHandle);
    }

“MakeWin”应该在表单的构造函数中调用,最好在“InitializeComponent”之前调用。至少在Win7下对我有用。

于 2015-09-15T14:59:18.783 回答
0

为 WPF 表单添加我的转折。由于 WPF 窗口句柄,上述代码不起作用。所以完整的代码适用于WPF(win 10):

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hP, IntPtr hC, string sC, string sW);

void MakeWin()
{
    IntPtr nWinHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null);
    nWinHandle = FindWindowEx(nWinHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
    var interop = new WindowInteropHelper(this);
    interop.EnsureHandle();
    interop.Owner = nWinHandle;
}
于 2021-03-27T14:36:32.267 回答