在WPF中设置窗体的Topmost属性可以将窗体永远置于顶部,但是没有提供Bottommost属性将窗体置底。若果要将窗体置于桌面的最底部,就需要使用Windows API来实现了。解决方案如下:
1,引入Windows API
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);
public const UInt32 SWP_NOSIZE = 0x0001;
public const UInt32 SWP_NOMOVE = 0x0002;
public const UInt32 SWP_NOACTIVATE = 0x0010;
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
2,使用API
private void SetBottom(Window window)
{
IntPtr hWnd = new WindowInteropHelper(window).Handle;
Win32.SetWindowPos(hWnd, Win32.HWND_BOTTOM, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_NOMOVE | Win32.SWP_NOACTIVATE);
}
3,将WCF窗体置底
在窗体看的Activated和StateChanged两个事件中都调用 SetBottom方法