Home > Backend Development > C++ > How Can I Achieve 'On Desktop' and 'On Bottom' Window Placement in WPF?

How Can I Achieve 'On Desktop' and 'On Bottom' Window Placement in WPF?

Linda Hamilton
Release: 2024-12-29 02:00:11
Original
966 people have browsed it

How Can I Achieve

Window Placement Control in WPF: "On Desktop" Option

Rainlendar's ability to place windows "on desktop" allows them to behave as bottomMost windows. This is not a built-in feature of WPF, but it can be achieved using Win32 API calls, which require P/Invoke from C#.

Rainlendar's Options:

Rainlendar offers two options for window placement:

  • On Desktop: The window becomes a child of the Explorer desktop window, similar to setting the parent using the SetParent API.
  • On Bottom: The window remains at the bottom of the Z-order, just above the desktop.

Achieving On Desktop Placement:

To place a WPF window "on desktop," you can use the SetParent API:

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Copy after login

Setting hWndNewParent to IntPtr.Zero removes the parent and makes the window a desktop child.

Achieving On Bottom Placement:

To keep a window "on bottom," you need to intercept the WM_WINDOWPOSCHANGING message:

protected override void WndProc(ref Message m)
{
    if (m.Msg == (int) WM.WINDOWPOSCHANGING)
    {
        var pos = (WINDOWPOS) Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
        // Check if the window is being promoted to the top
        if ((pos.flags & SWP_NOMOVE) == 0 && (pos.flags & SWP_NOSIZE) == 0)
        {
            pos.hwndInsertAfter = HWND_BOTTOM;
            pos.flags |= SWP_NOACTIVATE;
            Marshal.StructureToPtr(pos, m.LParam, true);
        }
    }
    base.WndProc(ref m);
}
Copy after login

By setting hwndInsertAfter to HWND_BOTTOM and adding the SWP_NOACTIVATE flag, the window will always remain at the bottom and will not activate when clicked.

The above is the detailed content of How Can I Achieve 'On Desktop' and 'On Bottom' Window Placement in WPF?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template