gpt4 book ai didi

C# WPF 主窗口始终位于桌面前面

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:27 24 4
gpt4 key购买 nike

我有一个小的 WPF 应用程序,我总是希望它显示在桌面前面,但显示在所有其他应用程序的后面。我尝试了几种方法来创建它,但窗口要么最小化 (WindKey + D),要么显示在所有应用程序的前面。

基本上我想在主窗口中运行一个标签。它的桌面背景是透明的,消息文本每周都会更改。 (我工作场所的所有用户安全信息)。

using System.Windows;



namespace ESLMessage
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitialiseScreenSettings();
this.ShowInTaskbar = false;

}

public void InitialiseScreenSettings()
{

double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
//this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
//this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
//this.Topmost = true;

if (!IsVisible)
{
Show();
}

if (WindowState == WindowState.Minimized)
{
WindowState = WindowState.Normal;
}
Activate();
Topmost = true; // important
Topmost = false; // important
Focus(); // important
}
}
}

最佳答案

试试下面的代码:

[DllImport("user32.dll")]
static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
uint uFlags);

const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;

static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

static void SendWpfWindowBack(Window window)
{
var hWnd = new WindowInteropHelper(window).Handle;
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}

来源:http://www.aeroxp.org/board/lofiversion/index.php?t4983.html

编辑:

对不起。您必须处理 MainWindow 的加载事件并将其发送回。试试这个:

代码隐藏:

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
SendWpfWindowBack(Application.Current.MainWindow);
}

Xaml:

    Loaded="MainWindow_OnLoaded"

仍然按 Win+D 将隐藏它。但它与您的要求有些相似。如果您想永久向所有员工显示特定消息,请尝试考虑使用墙纸。您可以为每个人设置相同的墙纸,并禁止员工更改它。

关于C# WPF 主窗口始终位于桌面前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com