调整窗口大小的跳跃反而很顺利
本文关键字:窗口大小 跳跃 调整 | 更新日期: 2023-09-27 18:28:01
我在尝试实现用于调整窗口大小的自定义按钮时遇到问题。这个视频展示了正在发生的事情http://screencast.com/t/Dtl5PyLcN3TE
我使用thumb元素来实现窗口的自定义大小调整按钮。
<Thumb Style="{StaticResource ThumbStyle}" DragStarted="Thumb_OnDragStarted" DragDelta="Thumb_OnDragDeltanDragDelta"/>
这是背后的代码
private void Thumb_OnDragDeltanDragDelta(object sender, DragDeltaEventArgs e)
{
LogWindow.Height = m_BaseSize.Y + e.VerticalChange;
LogWindow.Width = m_BaseSize.X + e.HorizontalChange;
}
private void Thumb_OnDragStarted(object sender, DragStartedEventArgs e)
{
m_BaseSize.X = LogWindow.Width;
m_BaseSize.Y = LogWindow.Height;
}
如何使自定义调整大小与本机实现更相似?
编辑:看来我明白问题出在哪里了。Thumb_OnDragDeltanDragDelta在某些时候获得无效的垂直更改
我不再依赖此事件的数据,而是使用鼠标坐标。现在一切看起来都很好。
private void Thumb_OnDragDeltanDragDelta(object sender, DragDeltaEventArgs e)
{
var source = PresentationSource.FromVisual(this);
Point position = Mouse.GetPosition(this);
var hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
Matrix transformToDevice = source.CompositionTarget.TransformToDevice;
Point[] p = { new Point(Left, Top), new Point(position.X, position.Y) };
transformToDevice.Transform(p);
SetWindowPos(hwndSource.Handle, IntPtr.Zero, Convert.ToInt32(p[0].X), Convert.ToInt32(p[0].Y), Convert.ToInt32(p[1].X), Convert.ToInt32(p[1].Y), SetWindowPosFlags.SWP_SHOWWINDOW);
}
并使用winApi调整大小
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[Flags]
public enum SetWindowPosFlags : uint
{
SWP_ASYNCWINDOWPOS = 0x4000,
SWP_DEFERERASE = 0x2000,
SWP_DRAWFRAME = 0x0020,
SWP_FRAMECHANGED = 0x0020,
SWP_HIDEWINDOW = 0x0080,
SWP_NOACTIVATE = 0x0010,
SWP_NOCOPYBITS = 0x0100,
SWP_NOMOVE = 0x0002,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOREDRAW = 0x0008,
SWP_NOREPOSITION = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_NOSIZE = 0x0001,
SWP_NOZORDER = 0x0004,
SWP_SHOWWINDOW = 0x0040,
}