如果在WPF Window
上设置ResizeMode="CanResizeWithGrip"
,则右下角将显示调整大小夹点,如下所示:
如果还设置了WindowStyle="None"
,则标题栏将消失,但在设置ResizeMode="NoResize"
之前,灰色倒角边缘将一直保留。不幸的是,设置了这些属性的组合后,调整大小夹点也会消失。
我已经通过一个自定义的Style
重写了Window
的ControlTemplate
,我想自己指定窗口的边框,我不需要用户能够从四个方面调整窗口的大小,但我需要一个调整大小的手柄。
有人能详细介绍一种简单的方法来满足所有这些标准吗?
除了我在ControlTemplate
.
Window
上还有一个边框。我在右下角有一个有效的调整大小手柄corner.
发布于 2009-03-04 16:21:08
如果在Window
上设置了AllowsTransparency
特性(即使没有设置任何透明度值),边框也会消失,并且只能通过夹点调整大小。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480"
WindowStyle="None"
AllowsTransparency="True"
ResizeMode="CanResizeWithGrip">
<!-- Content -->
</Window>
结果看起来像这样:
发布于 2016-03-28 16:50:19
我试图用WindowStyle="None"
创建一个无边框的窗口,但当我测试它时,似乎在顶部出现了一个白条,经过一些研究,它似乎是一个“调整边框”,这是一个图像(我用黄色注释):
经过互联网上的一些研究,以及许多困难的非xaml解决方案,我找到的所有解决方案都是C#中的代码隐藏和大量代码行,我间接地在这里找到了解决方案:Maximum custom window loses drop shadow effect
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="0"
ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
注意:您需要使用.NET 4.5框架,或者如果您使用的是较旧版本的WPFShell,只需引用外壳并使用Shell:WindowChrome.WindowChrome
即可。
我使用了Window的WindowChrome
属性,如果你使用这个属性,白色的“调整边框”会消失,但你需要定义一些属性才能正常工作。
CaptionHeight:这是标题区域(标题栏)的高度,它允许像正常标题栏一样进行航空捕捉、双击行为。将其设置为0(零)以使按钮工作。
ResizeBorderThickness:这是窗口边缘的厚度,您可以在此调整窗口的大小。我设置为5是因为我喜欢这个数字,而且如果您设置为0,则很难调整窗口大小。
使用此简短代码后,结果如下所示:
现在,在没有使用ResizeMode="NoResize"
和AllowsTransparency="True"
的情况下,白色边框消失了,它还在窗口中显示了一个阴影。
稍后我将解释如何使用简单和简短的代码轻松地制作按钮(我没有使用图像作为按钮),我是新手,我认为我可以发布到codeproject,因为在这里我找不到发布教程的地方。
也许还有另一种解决方案(我知道对于像我这样的新手来说,有很难的解决方案),但这对我的个人项目有效。
下面是完整的代码
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Concursos"
mc:Ignorable="d"
Title="Concuros" Height="350" Width="525"
WindowStyle="None"
WindowState="Normal"
ResizeMode="CanResize"
>
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="0"
ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
<Grid>
<Rectangle Fill="#D53736" HorizontalAlignment="Stretch" Height="35" VerticalAlignment="Top" PreviewMouseDown="Rectangle_PreviewMouseDown" />
<Button x:Name="Btnclose" Content="r" HorizontalAlignment="Right" VerticalAlignment="Top" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
<Button x:Name="Btnmax" Content="2" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,35,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
<Button x:Name="Btnmin" Content="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,70,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
</Grid>
谢谢!
发布于 2014-08-02 20:25:55
虽然公认的答案是非常正确的,但我只想指出AllowTransparency有一些缺点。它不允许显示子窗口控件,例如WebBrowser,而且它通常会强制软件渲染,这可能会对性能产生负面影响。
不过,还有一种更好的解决办法。
当你想创建一个不带边框、可调整大小并且能够承载WebBrowser控件或框架控件的窗口时,该控件的内容将显示为空。
不过我找到了一个解决办法;在窗口中,如果你将WindowStyle设置为None,将ResizeMode设置为NoResize (请耐心等待,一旦完成,你仍然可以调整大小),然后确保你取消选中AllowsTransparency,你将得到一个没有边框的静态大小的窗口,并显示浏览器控件。
现在,您可能仍然希望能够调整大小,对吧?我们可以通过一个互操作调用来实现:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
//Attach this to the MouseDown event of your drag control to move the window in place of the title bar
private void WindowDrag(object sender, MouseButtonEventArgs e) // MouseDown
{
ReleaseCapture();
SendMessage(new WindowInteropHelper(this).Handle,
0xA1, (IntPtr)0x2, (IntPtr)0);
}
//Attach this to the PreviewMousLeftButtonDown event of the grip control in the lower right corner of the form to resize the window
private void WindowResize(object sender, MouseButtonEventArgs e) //PreviewMousLeftButtonDown
{
HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
SendMessage(hwndSource.Handle, 0x112, (IntPtr)61448, IntPtr.Zero);
}
瞧,WPF窗口没有边框,仍然可以移动和调整大小,而不会失去与WebBrowser等控件的兼容性
https://stackoverflow.com/questions/611298
复制相似问题