需要时未触发鼠标左键向上
Posted
技术标签:
【中文标题】需要时未触发鼠标左键向上【英文标题】:Mouse left button up not fired when wanted 【发布时间】:2017-01-10 11:10:51 【问题描述】:任务: 我有一个简单的任务要执行:围绕椭圆的中心在椭圆内移动一根针。它将成为角度计指示器/控件。
状态:
当左键在椭圆内时,我想根据鼠标位置移动这个针。
没关系。
如果松开鼠标左键,我想阻止它移动。
没关系。
我的问题:
但问题是,如果我用鼠标左键退出椭圆区域(即移动针时不小心),那么它是“保持”。 如果你用鼠标左键向上返回椭圆,针会像被点击一样移动。 所以我需要的是在鼠标离开椭圆时禁用“拖动”,无论是否单击鼠标左键。 此时,如果针被“锁定”,则需要离开椭圆,单击鼠标左键,并通过进入椭圆区域保持按住。如果您现在松开鼠标左键,它将恢复正常/想要的行为。
这是我的 XAML“代码”:
<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#“代码”:
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;
如您所见,它使用不同的事件,但没有一个可以解决问题。 好吧,我相信至少有一个,但我不知道是哪一个。
如果你有什么想法,请随时回答:)
【问题讨论】:
***.com/questions/7417739/… msdn.microsoft.com/en-us/library/… 谢谢,答案可能在这些链接中,但目前对我来说理解起来相当苛刻。 【参考方案1】:您可以管理鼠标离开事件
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;
【讨论】:
很高兴它有帮助。哦...如果我们使用MouseButtonState.Pressed
(鼠标进入时)会更优雅以上是关于需要时未触发鼠标左键向上的主要内容,如果未能解决你的问题,请参考以下文章