月更
This commit is contained in:
37
AntDesignWPF/Extensions/UiElementExtensions.cs
Normal file
37
AntDesignWPF/Extensions/UiElementExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
using System.Windows;
|
||||
|
||||
namespace AntDesignWPF.Extensions;
|
||||
|
||||
internal static class UiElementExtensions
|
||||
{
|
||||
private static int Get_X_LParam(IntPtr lParam) { return (short)(lParam.ToInt32() & 0xFFFF); }
|
||||
|
||||
private static int Get_Y_LParam(IntPtr lParam) { return (short)(lParam.ToInt32() >> 16); }
|
||||
|
||||
/// <summary>
|
||||
/// 不要在 NCHITTEST、NCLBUTTONUP、NCLBUTTONDOWN 消息之外调用它!
|
||||
/// </summary>
|
||||
/// <returns>如果鼠标位于元素上<see langword="true"/>。 否则,<see langword="false"/> </returns>
|
||||
public static bool IsMouseOverElement(this UIElement element, IntPtr lParam)
|
||||
{
|
||||
// 此方法将非常频繁地调用,并且必须尽可能简单。
|
||||
if (lParam == IntPtr.Zero)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var mousePosScreen = new Point(Get_X_LParam(lParam), Get_Y_LParam(lParam));
|
||||
var bounds = new Rect(default, element.RenderSize);
|
||||
|
||||
Point mousePosRelative = element.PointFromScreen(mousePosScreen);
|
||||
|
||||
return bounds.Contains(mousePosRelative);
|
||||
} catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user