WindowStyle属性为None不起作用
时间: 2024-02-20 20:57:41 浏览: 138
如果WindowStyle属性设置为None没有隐藏窗口的边框和标题栏,可能是因为应用程序的框架不允许窗口样式被覆盖。在这种情况下,可以尝试使用以下方法:
1. 在App.xaml文件中添加以下样式:
```xaml
<Style TargetType="Window">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
```
这个样式将应用于所有的窗口,它将WindowStyle属性设置为None,AllowsTransparency属性设置为True,以及Background属性设置为Transparent。这将导致窗口被渲染为透明的,从而隐藏窗口的边框和标题栏。
2. 在Window的Loaded事件处理程序中使用Win32 API来隐藏窗口的边框和标题栏。示例代码如下:
```c#
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_THICKFRAME = 0x40000;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOACTIVATE = 0x0010;
private readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Hide the window border and title bar
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
int style = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, style & ~(WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME));
// Set the window position to cover the entire screen
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, (int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
}
}
}
```
这个方法使用SetWindowLong函数来移除窗口的菜单、最小化和最大化按钮、改变窗口大小的边框等,并使用SetWindowPos函数将窗口移动到整个屏幕的位置。这将导致窗口的边框和标题栏被隐藏。
阅读全文