任务: 我有一个简单的任务:在椭圆的中心周围的椭圆内移动针。它将成为角度指示器/控制器。
状态:
当左侧按钮在椭圆内部向下时,我想根据鼠标位置移动此针。
这没关系。
如果释放鼠标左键,我想停止移动。
没关系。
我的问题:
但问题是如果我用鼠标左键退出椭圆区域(即偶然地移动针头),那么它就是“保持”。 如果您在鼠标左键向上的情况下返回椭圆,则针将像点击一样移动。 所以我需要的是当我的鼠标离开椭圆时禁用“拖动”,无论鼠标左键是否被点击。 此时,如果针被“锁定”,则需要进入椭圆外部,单击鼠标左键,并通过输入椭圆区域按住它。如果您现在释放鼠标左键,它将恢复正常/想要的行为。
这是我的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;
}
}
正如您所看到的,它使用不同的事件,但没有一个可以做到。 嗯,我相信至少有一个,但我不知道哪一个。
如果您有任何想法,请随时回答:)
答案 0 :(得分:0)
您可以管理鼠标离开事件
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;
}