我有一个应用程序,希望能够移动滑块。但是,该程序每30秒会自动更新滑块的位置。当我试图用鼠标改变滑块位置时,程序仍在更新滑块位置,因此我无法手动移动它。
我想让应用程序在用户正在改变滑块位置时不更新滑块。如何判断鼠标左键是否按下,而不使用左键按下事件?我只想访问鼠标,并检查左键的状态,如何在没有鼠标事件的情况下访问它?
我有一个应用程序,希望能够移动滑块。但是,该程序每30秒会自动更新滑块的位置。当我试图用鼠标改变滑块位置时,程序仍在更新滑块位置,因此我无法手动移动它。
我想让应用程序在用户正在改变滑块位置时不更新滑块。如何判断鼠标左键是否按下,而不使用左键按下事件?我只想访问鼠标,并检查左键的状态,如何在没有鼠标事件的情况下访问它?
bool mouseIsDown = System.Windows.Input.Mouse.LeftButton == MouseButtonState.Pressed;
有些罕见情况下,System.Windows.Input.Mouse.LeftButton
无法正常工作。例如在调整大小期间。
那么你可以使用GetKeyState:
public enum VirtualKeyCodes : short
{
LeftButton = 0x01
}
[DllImport("user32.dll")]
private static extern short GetKeyState(VirtualKeyCodes code);
public static bool CheckKey(VirtualKeyCodes code) => (GetKeyState(code) & 0xFF00) == 0xFF00;
好的,我明白了。WPF中的Slider控件不会触发MouseDown、LeftMouseDown或LeftMouseUp事件。这是因为当用户操作滑块时,它们在内部使用这些事件来调整值。但是,WPF中的Slider控件确实会触发PreviewLeftMouseDown和PreviewLeftMouseUp事件。此外,当您单击左键时,Slider控件会自动捕获鼠标,并一直保持到您释放左键。
我通过以下3个事件解决了我的问题:
private void _sliderVideoPosition_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_adjustingVideoPositionSlider = true;
_mediaElement.Pause();
}
private void _sliderVideoPosition_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_adjustingVideoPositionSlider = false;
_mediaElement.Play();
}
private void _sliderVideoPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (_adjustingVideoPositionSlider)
{
_mediaElement.Position = TimeSpan.FromMilliseconds((int)e.NewValue);
}
}
TimeSpan.FromMilliseconds((int)e.NewValue)
更明显一些。 - Nick Freeman据我了解,您希望保持MVVM模式以完成此任务。
因此,我首先会设计我在ViewModel中想要的内容。
public class ViewModel
{
public ICommand EditingSliderCommand { get; set; } // actually set the command
public ICommand DoneEditingCommand { get; set; } // actually set the command
public bool IsEditing { get; set; }
...
private void AutomaticUpdate
{
if (IsEditing)
return;
Update();
}
}
然后您可以使用 Blend Interactivity
库在 XAML 中处理 UI 方面的内容。
以下是一个示例。我不知道事件名称是否正确。您也可以使用鼠标抬起和完成编辑来完成相同的操作。
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<cmd:EventToCommand Command="{Binding Mode=OneWay,
Path=EditingSliderCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
被接受的答案对我没有用,但这个有效:
if ((System.Windows.Forms.Control.MouseButtons & System.Windows.Forms.MouseButtons.Left) == System.Windows.Forms.MouseButtons.Left)
{
System.Diagnostics.Debug.WriteLine("Left mouse down");
}