0

We're developing an WPF application, that opens under some circumstances an additional window, that should always be docked to the parent (main) window. We're currently hooking the main application and using the window messages:

private void OnLoaded()
{
    //...
    HwndSource.FromHwnd(handle).RemoveHook(OwnerWindowProc);
}

private IntPtr OwnerWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_MOVE:
        case WM_MOVING:
            RefreshPosition();
            break;
    }

    return (IntPtr)0;
}

private void RefreshPosition()
{
    var newLeft = Owner.Width + Owner.Left;
    var newTop = VerticalOffset + Owner.Top;

    Left = newLeft;
    Top = newTop;
}

This works quite good, except for some rare cases, when you'll drop the window very fast - the additional Window just stays at an outdated position, which is obviously incorrect.

Another / An additional approach came to my mind. Either checking the windows position additionally every x MS, or starting / restarting a timer each time a window message is processed, in order to delay the updating process. Any other ideas?

ElGaucho
  • 408
  • 2
  • 14
  • 1
    How about using LocationChanged as done here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9daab290-cf3a-4777-b046-3dc156b184c0/how-to-make-a-wpf-child-window-follow-its-parent?forum=wpf ? – o_weisman Apr 21 '15 at 10:47
  • Excellent, that does the trick. Sometimes the easiest solution is also the best. Thanks a lot. – ElGaucho Apr 21 '15 at 11:05

0 Answers0