Cri*_*lin 10 .net c# wpf cursor window-resize
我正在WPF中设计自己的自定义窗口,我一直在尝试实现我以前在WinForms中使用的调整大小功能.由于某种原因,我的WndProc的返回值没有给我正确的结果.
我有一个NativeMethods类用于我所有的WndProc消息和结果:
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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的窗口背后的代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望光标更改为"调整大小向下箭头"和调整大小功能,就像在WinForms项目中一样.我设置了断点,当光标位于预期位置时,HTBOTTOM返回被触发.在XAML中,我将ResizeMode设置为CanResize,将WindowStyle设置为None.我究竟做错了什么?
kir*_*ran 14
也许分配WindowChrome更简单.根据你的评论,你必须能够从所有方面调整大小以及使用grip.You可以通过将WindowStyle设置为None和ResizeMode设置为CanResizeWithGrip或CanResize(无论你想要什么)来完成所有这些操作. acheive)
<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">
Run Code Online (Sandbox Code Playgroud)
在代码behid中,您必须为窗口设置Window Chrome.你可以这样做:
WindowChrome.SetWindowChrome(this, new WindowChrome());
Run Code Online (Sandbox Code Playgroud)
或者 您可以使用setter作为窗口样式,如:
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CornerRadius="0" GlassFrameThickness="1" UseAeroCaptionButtons="False"/>
</Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)
请注意,WindowChrome类是.NET 4.5 Framework的一部分.对于.NET 4.0用户,请查看 archive.msdn.microsoft.com/WPFShell
小智 7
我在另一篇文章中写了一个解决方案,您可以调整窗口大小,您需要使用 .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>
Run Code Online (Sandbox Code Playgroud)
你可以去这里查看完整的帖子。
这是之前和之后

嗯,这是一个愚蠢的错误。handled = true;在返回结果之前我忘记添加了。现在窗口可以正常工作了。请注意,如果您将 ResizeMode 设置为 NoResize,则此代码将根本不起作用。
| 归档时间: |
|
| 查看次数: |
8048 次 |
| 最近记录: |