Skip to content

Commit

Permalink
Merge pull request #926 from Nice3point/development_fix674
Browse files Browse the repository at this point in the history
Fix issue 674: Dragging no longer works when SizeToContent is set on Window
  • Loading branch information
pomianowski authored Feb 3, 2024
2 parents 917f28f + 4638e69 commit 2bfbb99
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Wpf.Ui/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,7 @@ protected virtual void OnLoaded(object sender, RoutedEventArgs e)
_currentWindow =
System.Windows.Window.GetWindow(this) ?? throw new ArgumentNullException("Window is null");
_currentWindow.StateChanged += OnParentWindowStateChanged;

var handle = new WindowInteropHelper(_currentWindow).EnsureHandle();
var windowSource =
HwndSource.FromHwnd(handle) ?? throw new ArgumentNullException("Window source is null");
windowSource.AddHook(HwndSourceHook);
_currentWindow.ContentRendered += OnWindowContentRendered;
}

private void OnUnloaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -583,6 +579,19 @@ private void OnTemplateButtonClick(TitleBarButtonType buttonType)
}
}

/// <summary>
/// Listening window hooks after rendering window content to SizeToContent support
/// </summary>
private void OnWindowContentRendered(object sender, EventArgs e)
{
var window = (Window)sender;
window.ContentRendered -= OnWindowContentRendered;

var handle = new WindowInteropHelper(window).Handle;
var windowSource = HwndSource.FromHwnd(handle) ?? throw new ArgumentNullException("Window source is null");
windowSource.AddHook(HwndSourceHook);
}

private IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
var message = (User32.WM)msg;
Expand Down

0 comments on commit 2bfbb99

Please sign in to comment.