.net - 如何在 WPF 中保持窗口靠后?

标签 .net wpf vb.net window z-order

我有一个生成全屏窗口的小型 .NET 程序。我想将此窗口保留在最后面的窗口(即其他窗口应在其顶部打开,并且在单击时不应出现在最前面)。在 Windows Presentation Foundation 下有什么实用的方法可以做到这一点吗?

最佳答案

据我所知,您必须 P/Invoke 才能正确执行此操作。调用 SetWindowPos function ,指定窗口句柄和 HWND_BOTTOM旗帜。

这会将您的窗口移动到 Z 顺序的底部,并防止它遮挡其他窗口。

示例代码:

Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Const SWP_NOACTIVATE As Integer = &H10

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr,
                                     X As Integer, Y As Integer,
                                     cx As Integer, cy As Integer,
                                     uFlags As Integer) As Boolean
End Function


Public Sub SetAsBottomMost(ByVal wnd As Window)
    ' Get the handle to the specified window
    Dim hWnd As IntPtr = New WindowInteropHelper(wnd).Handle

    ' Set the window position to HWND_BOTTOM
    SetWindowPos(hWnd, New IntPtr(1), 0, 0, 0, 0,
                 SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE)
End Sub

关于.net - 如何在 WPF 中保持窗口靠后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5049409/

相关文章:

.net - RouteTable.Routes.MapHubs() 仅在 Windows Azure 模拟器中抛出 ArgumentNullException。为什么?

.net - 层与层之间的 n-Tier 应用程序应该如何进行通信?

c# - 如何重命名动态创建的 TreeView 项目的标题

c# - 如何在长时间运行的 *UI* 操作中让 UI 刷新

vb.net - 选择大小写

.net - Dotnet Nuget/package 文件夹非常大并且对于每个用户

c# - WPF ListBox 未使用 ItemsSource 更新

WPF DataGrid垂直线粗细

android - Visual Studio 在 Eclipse 中的 Android 开发中相当于 Ctrl + Shift + O 的快捷方式是什么?

regex - 用于验证 RGB 字符串值的正则表达式