【问题标题】:Change mouse cursor while left mouse button is pressed?按下鼠标左键时更改鼠标光标?
【发布时间】:2011-02-28 08:59:53
【问题描述】:

我需要在按下鼠标左键时更改鼠标光标。不幸的是,在释放鼠标左键之前,鼠标光标的更改将被忽略。有什么解决方法吗?感谢您的任何提示!

(我正在使用 WPF 和 C#)

编辑:

示例项目: http://cid-0432ee4cfe9c26a0.skydrive.live.com/self.aspx/%c3%96ffentlich/WpfApplication5.zip (只需运行它,说明在应用程序中显示)

示例代码:

XAML:

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="695" Loaded="Window_Loaded">
<Grid>
    <Button Content="Button1" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="235" />
    <Button Content="Button2" Height="287" HorizontalAlignment="Left" Margin="284,12,0,0" Name="button2" VerticalAlignment="Top" Width="278" MouseMove="button2_MouseMove" />
</Grid>

窗口类:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button2_MouseMove(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Cross;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        button1.Content="Step1: Left click on this button, \nhold down the left mouse button";
        button2.Content = "(Make sure you don't hover this\n button before hovering Button1.\n Default application cursor\n is the normal arrow cursor)\n\n Step 2: Keep on holding the left mouse \nbutton, hover this button\n\nThe cursor won't change. (It will change after the \nleft mouse button was released)";
    }
}

【问题讨论】:

  • 您使用的是 MouseLeftButtonDown 还是 MouseLeftButtonClick 事件?
  • 您好 Ozan,我正在使用 MouseLeftButtonDown。我在上面添加了一个示例。

标签: c# wpf mouse-cursor


【解决方案1】:

我建议尽可能使用 Preview* 事件来进行视觉更改,因为它可以很好地分离您的逻辑。此外,最好(恕我直言)使用Mouse.OverrideCursor 属性临时更改光标。

例如:

void Window_Loaded(object sender, RoutedEventArgs e)
{
    // ...
    button1.PreviewMouseLeftButtonDown += Button1_PreviewMouseLeftButtonDown;
    button1.PreviewMouseLeftButtonUp += Button1_PreviewMouseLeftButtonUp;
}

void Button1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Mouse.OverrideCursor = Cursors.Cross;
    Mouse.Capture(button1);
}

void Button1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Mouse.Capture(null);
    Mouse.OverrideCursor = null;
}

【讨论】:

  • 您好 Dennis,非常感谢您提供 Mouse.OverrideCursor 提示!它不适用于上面的示例(单击按钮后,没有触发 mousemove 事件)但是它适用于我的实际应用程序(在拖放 givefeedback 事件期间我需要这个;上面的示例是一个简化的示例,问题是另一个问题一:鼠标左键按下时不会触发mousemove)。因此,虽然这不是我的示例的答案,但它仍然解决了我的问题 :-)
  • @Stefan:我用示例对其进行了测试,但认为 MouseMove 事件是您尝试更改光标,所以我省略了它。
  • @Stefan:顺便说一下,这不是在拖放操作期间更改光标以向用户提供反馈的方式。查看 DragDrop.GiveFeedback 和 DragDrop.QueryContinueDrag 事件 (bit.ly/bbP14Y)。
  • 我正在使用 GiveFeedback 事件,但我仍然必须使用 Mouse.OverrideCursor 还是我错过了什么?
【解决方案2】:

在鼠标左键处理程序中,您可以使用以下代码。

try
{
   Cursor = Cursors.WaitCursor;
}
catch(Exception ex)
{
}
finally
{
   Cursor = Cursors.Default;
}

您可以根据需要重置为默认光标。

【讨论】:

    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多