From e9b745d1257cfcc1112f3d6ace6266cf02ea1d67 Mon Sep 17 00:00:00 2001 From: John Popovici Date: Tue, 17 Jun 2025 17:00:55 +0300 Subject: [PATCH] Added event to forward HwndProc to avoid adding additional hooks and timing issues. --- .../Controls/TitleBar/HwndProcEventArgs.cs | 33 +++++++++++++++++++ src/Wpf.Ui/Controls/TitleBar/TitleBar.cs | 11 +++++++ 2 files changed, 44 insertions(+) create mode 100644 src/Wpf.Ui/Controls/TitleBar/HwndProcEventArgs.cs diff --git a/src/Wpf.Ui/Controls/TitleBar/HwndProcEventArgs.cs b/src/Wpf.Ui/Controls/TitleBar/HwndProcEventArgs.cs new file mode 100644 index 000000000..dab7a6d14 --- /dev/null +++ b/src/Wpf.Ui/Controls/TitleBar/HwndProcEventArgs.cs @@ -0,0 +1,33 @@ +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. +// Copyright (C) Leszek Pomianowski and WPF UI Contributors. +// All Rights Reserved. + +// ReSharper disable once CheckNamespace +namespace Wpf.Ui.Controls; + +public class HwndProcEventArgs : EventArgs +{ + public bool Handled { get; set; } + + public IntPtr? ReturnValue { get; set; } + + public bool IsMouseOverDetectedHeaderContent { get; } + + public IntPtr HWND { get; } + + public int Message { get; } + + public IntPtr WParam { get; } + + public IntPtr LParam { get; } + + internal HwndProcEventArgs(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, bool isMouseOverDetectedHeaderContent) + { + HWND = hwnd; + Message = msg; + WParam = wParam; + LParam = lParam; + IsMouseOverDetectedHeaderContent = isMouseOverDetectedHeaderContent; + } +} diff --git a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs b/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs index 471ba3bfc..98dfd845e 100644 --- a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs +++ b/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs @@ -37,6 +37,8 @@ public class TitleBar : System.Windows.Controls.Control, IThemeControl private DependencyObject? _parentWindow; + public event EventHandler? WndProcInvoked; + /// Identifies the dependency property. public static readonly DependencyProperty ApplicationThemeProperty = DependencyProperty.Register( nameof(ApplicationTheme), @@ -682,6 +684,15 @@ or User32.WM.NCLBUTTONUP } } + var e = new HwndProcEventArgs(hwnd, msg, wParam, lParam, isMouseOverHeaderContent); + WndProcInvoked?.Invoke(this, e); + + if (e.ReturnValue != null) + { + handled = e.Handled; + return e.ReturnValue ?? IntPtr.Zero; + } + switch (message) { case User32.WM.NCHITTEST when CloseWindowByDoubleClickOnIcon && _icon.IsMouseOverElement(lParam):