鼠标左键在需要的时候没有被触发
本文关键字:候没 鼠标 | 更新日期: 2023-09-27 18:05:14
任务:我有一个简单的任务要执行:在椭圆内围绕椭圆的中心移动一根针。它是成为一个角度计指示/控制。
状态:
当左键在椭圆内时,我想根据鼠标位置移动此指针。
这是好的。
我想要停止移动,如果鼠标左键被释放。
可以。
我问题:但问题是,如果我退出椭圆区域与鼠标左键按下(即偶然,当移动指针),那么它是"持有"。如果您曾经回到椭圆与鼠标左键向上,针将移动,如果它被点击。所以我需要的是禁用"拖动"当我的鼠标离开椭圆,无论鼠标左键被点击与否。此时,如果指针处于"锁定"状态,则需要到椭圆外,点击鼠标左键,进入椭圆区域保持按下。如果你现在释放鼠标左键,它将恢复到正常/想要的行为。
下面是我的XAML "code":
<Window x:Class="test_gauge.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid MouseLeftButtonUp="Grid_MouseLeftButtonUp">
<Ellipse Name="ellipse1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Canvas.Left="0" Canvas.Top="0" Margin="280,180,0,0" MouseMove="onEllipseMouseMove" MouseLeftButtonDown="onEllipseMouseLeftPressed" MouseLeftButtonUp="onEllipseMouseLeftReleased" MouseLeave="onEllipseMouseLeave"/>
<Canvas Margin="257,145,10,10" MouseLeftButtonUp="Canvas_MouseLeftButtonUp_1">
<Thumb Name="myThumb" Canvas.Left="73" Canvas.Top="85" RenderTransformOrigin="0,0">
<Thumb.RenderTransform>
<RotateTransform/>
</Thumb.RenderTransform>
<Thumb.Template>
<ControlTemplate>
<Line Name="needle_line" X1="0" Y1="0" X2="50" Y2="0" StrokeThickness="2" Stroke="Black" MouseLeave="needle_line_MouseLeave"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<TextBox Name="txtbxAngle" Height="23" Canvas.Left="168" TextWrapping="Wrap" Text="TextBox" Canvas.Top="25" Width="72"/>
</Canvas>
</Grid>
</Window>
然后c# "code":
public partial class MainWindow : Window
{
/* Angle control stuff */
public bool bNeedlePressed = false;
public double refreshedAngle = 0;
public MainWindow()
{
InitializeComponent();
}
private void onEllipseMouseMove(object sender, MouseEventArgs e)
{
if (bNeedlePressed)
{
// This will get the mouse cursor relative to the upper left corner of your ellipse.
// Note that nothing will happen until you are actually inside of your ellipse.
Point curPoint = e.GetPosition(ellipse1);
// Assuming that your ellipse is actually a circle.
Point center = new Point(ellipse1.Width / 2, ellipse1.Height / 2);
// A bit of math to relate your mouse to the center...
Point relPoint = new Point(curPoint.X - center.X, curPoint.Y - center.Y);
/* Process new angle */
refreshedAngle = Math.Atan2(relPoint.Y, relPoint.X) * 180 / Math.PI;
txtbxAngle.Text = ((int)(refreshedAngle)).ToString();
/* Apply new angle */
var transform = myThumb.RenderTransform as RotateTransform;
transform.Angle = refreshedAngle;
}
}
private void onEllipseMouseLeftPressed(object sender, MouseButtonEventArgs e)
{
bNeedlePressed = true;
}
private void Canvas_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
bNeedlePressed = false;
}
private void onEllipseMouseLeftReleased(object sender, MouseButtonEventArgs e)
{
bNeedlePressed = false;
}
private void onEllipseMouseLeave(object sender, MouseEventArgs e)
{
//bNeedlePressed = false;
}
private void needle_line_MouseLeave(object sender, MouseEventArgs e)
{
//bNeedlePressed = false;
}
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
bNeedlePressed = false;
}
}
如您所见,它可以使用不同的事件,但没有一个能达到目的。好吧,我相信至少有一个,但我不知道是哪一个。
如果你有什么想法,请随时回答:)
可以管理鼠标离开事件
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
bNeedlePressed = false;
}
在XAML
中声明的<Grid MouseLeftButtonUp="Grid_MouseLeftButtonUp" MouseLeave="Grid_MouseLeave" MouseEnter="Grid_MouseEnter">
和鼠标输入
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
bNeedlePressed = e.LeftButton == MouseButtonState.Pressed;
}