Reputation: 1434
I have problem when trying to implement custom button for window resizing. This video show's what happening http://screencast.com/t/Dtl5PyLcN3TE
I use thumb element to implement custom resizing button for window.
<Thumb Style="{StaticResource ThumbStyle}" DragStarted="Thumb_OnDragStarted" DragDelta="Thumb_OnDragDeltanDragDelta"/>
And it's code behind
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;
}
How to make custom resizing more simmilar to native implementation?
EDIT: Looks like i understand what's the problem. Thumb_OnDragDeltanDragDelta get invalid vertical change some times
Upvotes: 0
Views: 312
Reputation: 1434
I no longer rely on the data from this event, and use mouse coordinates. All looks good now.
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);
}
And use winApi resize
[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,
}
Upvotes: 2