[英]WPF Borderless window resize
我正在 WPF 中設計我自己的自定義窗口,並且我一直在嘗試實現我以前在 WinForms 中使用過的調整大小功能。 出於某種原因,我的 WndProc 的返回值沒有給我正確的結果。
我的所有 WndProc 消息和結果都有一個 NativeMethods 類:
public class NativeMethods
{
public const int WM_NCHITTEST = 0x84;
public const int HTCAPTION = 2;
public const int HTLEFT = 10;
public const int HTRIGHT = 11;
public const int HTTOP = 12;
public const int HTTOPLEFT = 13;
public const int HTTOPRIGHT = 14;
public const int HTBOTTOM = 15;
public const int HTBOTTOMLEFT = 16;
public const int HTBOTTOMRIGHT = 17;
}
這是我的窗口背后的代碼:
public partial class MainWindow : Window
{
const int GripSize = 16;
const int BorderSize = 7;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
HwndSource windowSource = HwndSource.FromHwnd(windowHandle);
windowSource.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg,
IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == NativeMethods.WM_NCHITTEST)
{
int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16;
Point pos = PointFromScreen(new Point(x, y));
if (pos.X > GripSize &&
pos.X < ActualWidth - GripSize &&
pos.Y >= ActualHeight - BorderSize)
{
return (IntPtr)NativeMethods.HTBOTTOM; // This doesn't work?
}
// Top, Left, Right, Corners, Etc.
}
return IntPtr.Zero;
}
}
我希望光標更改為“向下調整大小箭頭”,並且調整大小功能可以像在我的 WinForms 項目中一樣工作。 我已經設置了斷點,當光標位於預期位置時,HTBOTTOM 返回正在觸發。 在 XAML 中,我將 ResizeMode 設置為 CanResize,將 WindowStyle 設置為 None。 我究竟做錯了什么?
也許分配WindowChrome更為簡單。根據您的注釋,您必須能夠從所有側面調整大小以及使用握柄。可以通過將WindowStyle設置為None並將ResizeMode設置為CanResizeWithGrip或CanResize來完成所有這些操作達到
<Window x:Class="MVVMProtoType.View.Test.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300" WindowStyle="None" AllowsTransparency="False" ResizeMode="CanResizeWithGrip">
在隱藏的代碼中,您必須為window設置Window Chrome。 您可以這樣做:
WindowChrome.SetWindowChrome(this, new WindowChrome());
或者您可以將setter用於窗口樣式,例如:
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False"/>
</Setter.Value>
</Setter>
請注意,WindowChrome類是.NET 4.5 Framework的一部分。 對於.NET 4.0用戶,請簽出archive.msdn.microsoft.com/WPFShell
我在另一篇文章中寫了一個解決方案,可以調整窗口大小,需要使用.NET 4.5或WPFShell
您也可以像這樣將WindowChrome代碼直接放在MainWindow.xaml上,並且無需放置setter即可完美運行。
<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>
<YOUR CODE HERE
</Grid>
您可以在這里查看完整的帖子。
這是之前和之后
好吧,這是一個愚蠢的錯誤。 我忘了添加handled = true;
在我返回結果之前。 現在,該窗口將正常運行。 請注意,如果將ResizeMode設置為NoResize,則此代碼將完全不起作用。
我還向您可能感興趣的完全正常工作的(相當大的)WPF無邊界窗口提供了源代碼。可以在這里找到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.