gpt4 book ai didi

wpf - 如何在拖动 Thumb 时捕获 MouseEnter?

转载 作者:行者123 更新时间:2023-12-02 03:55:45 25 4
gpt4 key购买 nike

我在 Canvas 中有一个 Thumb(包含一个 TextBlock)和一个 Border。Thumb 接收到拖动事件,并且正在正确拖动。

我需要的是在 Thumb 拖动时捕获 Border 上的 MouseEnter 事件。

问题是 MouseEnter 在 DragCompleted 事件之后触发(在释放鼠标按钮之后)。当鼠标按钮按下时,我如何知道鼠标何时进入边框?

Xaml:

<Grid>
<Canvas Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Canvas Name="dragTarget">
<Thumb
DragStarted="Thumb_DragStarted"
DragDelta="Thumb_DragDelta"
DragCompleted="Thumb_DragCompleted">
<Thumb.Template>
<ControlTemplate>
<TextBlock
Text="Some Text"
Foreground="White"
Background="DimGray"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
<Border Width="100" Height="100" Margin="50,50,0,0"
BorderBrush="AliceBlue" BorderThickness="1" Background="Silver"
MouseEnter="Border_MouseEnter"/>
</Canvas>

Xaml.cs:

bool isDragging = false;
double x = 0;
double y = 0;

private void Border_MouseEnter(object sender, MouseEventArgs e)
{
if(isDragging)
Title = "Captured";
}

private void Thumb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
isDragging = true;
x = e.HorizontalOffset;
y = e.VerticalOffset;
}

private void Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
isDragging = false;
}

private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
x += e.HorizontalChange;
y += e.VerticalChange;
dragTarget.Margin = new Thickness(x , y , 0, 0);
}

最佳答案

鼠标在拖动时会被捕获,因此您可能不会收到消息。

在 Thumb_DragDelta 事件中,使用边框获取鼠标位置。它将相对于边界。检查鼠标点是否在边界内。

Point point = Mouse.GetPosition(border);

Rect rect = new Rect(0, 0, border.ActualWidth, border.ActualHeight);
Boolean mouseInBorder = rect.Contains(point);

if (mouseInBorder && !mouseLastInBorder)
{
// Mouse enter "event";
}
if (!mouseInBorder && mouseLastInBorder)
{
// Mouse exit "event";
}
mouseLastInBorder = mouseInBorder;

关于wpf - 如何在拖动 Thumb 时捕获 MouseEnter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12742732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com