微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wpf自定义窗口,Windows边缘resizefunction

一个自定义的WPF窗口(WindowStyle = None,AllowTransparancy = true),并想知道如何让Windows边缘resizefunction工作..你知道什么时候拖动窗口和鼠标触摸屏幕的左边,右边或顶部边缘(甚至W10的angular落)。

试图调查WM通知,但没有一个似乎是什么即时通讯寻找..

要清除,而不是普通的窗口resize..但拖到屏幕边缘,让Windows调整到半/全/季度(认为其称为Aero快照)。 我可以用普通的resize调用它,但不会显示透明的预览窗口或触摸边缘上的鼠标放下animation。

谢谢

C#Windows应用程序阻止Windowsclosures/注销

JavaFxvideo尺寸输出屏幕

如何检查dll的引用计数? 如何知道DLL在哪里加载?

正则expression式查找tasklist -v在窗口中输出的过程

列出所有打开的文件

使用Git为Windows接收未知的协议错误

? 在我的文件

C拆分string函数

如何将TCHAR转换为int

如何获取设备pipe理器Windows中带有黄色感叹号的设备列表 – c ++

步骤1

为矩形创建样式(在<Window1.Resources> )作为窗口周围的夹点区域:

<Style x:Key="RectBorderStyle" targettype="Rectangle"> <Setter Property="Focusable" Value="False" /> <Setter Property="Fill" Value="Transparent" /> <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" /> <EventSetter Event="MouseLeftButtonDown" Handler="Resize_Init"/> <EventSetter Event="MouseLeftButtonUp" Handler="Resize_End"/> <EventSetter Event="MouseMove" Handler="Resizeing_Form"/> </Style>

第2步

将这些风格的矩形添加到您的窗口。 (你可以将它们添加到窗口内的简单网格中)

<Rectangle x:Name="leftSizegrip" Width="7" HorizontalAlignment="Left" Cursor="SizeWE" {StaticResource RectBorderStyle}" /> <Rectangle x:Name="rightSizegrip" Width="7" HorizontalAlignment="Right" Cursor="SizeWE" {StaticResource RectBorderStyle}" /> <Rectangle x:Name="topSizegrip" Height="7" VerticalAlignment="Top" Cursor="SizeNS" {StaticResource RectBorderStyle}" /> <Rectangle x:Name="bottomSizegrip" Height="7" VerticalAlignment="Bottom" Cursor="SizeNS" {StaticResource RectBorderStyle}" /> <!-- Corners --> <Rectangle Name="topLeftSizegrip" Width="7" Height="7" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE" {StaticResource RectBorderStyle}" /> <Rectangle Name="bottomrightSizegrip" Width="7" Height="7" HorizontalAlignment="Right" VerticalAlignment="Bottom" Cursor="SizeNWSE" {StaticResource RectBorderStyle}" /> <Rectangle Name="topRightSizegrip" Width="7" Height="7" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="SizenesW" {StaticResource RectBorderStyle}" /> <Rectangle Name="bottomLeftSizegrip" Width="7" Height="7" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizenesW" {StaticResource RectBorderStyle}" />

第3步

将这些代码添加到窗口后面的代码(window1.xaml.cs) (或者如果您正在使用窗口模板,并且已经在模板内添加了8个矩形,则添加到MyStyle.xaml.cs中)

#region ResizeWindows bool ResizeInProcess = false; private void Resize_Init(object sender,MouseButtonEventArgs e) { Rectangle senderRect = sender as Rectangle; if (senderRect != null) { ResizeInProcess = true; senderRect.CaptureMouse(); } } private void Resize_End(object sender,MouseButtonEventArgs e) { Rectangle senderRect = sender as Rectangle; if (senderRect != null) { ResizeInProcess = false; ; senderRect.ReleaseMouseCapture(); } } private void Resizeing_Form(object sender,MouseEventArgs e) { if (ResizeInProcess) { Rectangle senderRect = sender as Rectangle; Window mainWindow = senderRect.Tag as Window; if (senderRect != null) { double width = e.GetPosition(mainWindow).X; double height = e.GetPosition(mainWindow).Y; senderRect.CaptureMouse(); if (senderRect.Name.ToLower().Contains("right")) { width += 5; if (width > 0) mainWindow.Width = width; } if (senderRect.Name.ToLower().Contains("left")) { width -= 5; mainWindow.Left += width; width = mainWindow.Width - width; if (width > 0) { mainWindow.Width = width; } } if (senderRect.Name.ToLower().Contains("bottom")) { height += 5; if (height > 0) mainWindow.Height = height; } if (senderRect.Name.ToLower().Contains("top")) { height -= 5; mainWindow.Top += height; height = mainWindow.Height - height; if (height > 0) { mainWindow.Height = height; } } } } } #endregion

步骤4

按F5,享受!

注意:

这8个矩形是透明的。 如果您无法正常工作,只需将样式的Fill值更改为Red即可查看它们的位置。

一个说明:

最大化时,您可能需要禁用所有这些矩形。 最简单的方法是处理WindowStateChanged事件并禁用包含它们的网格。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐