按下鼠标左键时更改鼠标光标? [英] Change mouse cursor while left mouse button is pressed?

查看:250
本文介绍了按下鼠标左键时更改鼠标光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在按下鼠标左键的同时更改鼠标光标.不幸的是,直到释放鼠标左键,鼠标光标的更改才会被忽略.有什么解决方法吗?谢谢您的提示!

I need to change the mouse cursor while the left mouse button is pressed. Unfortunately changes to the mouse cursor are ignored until the left mouse button is released. Is there any workaround to this? Thanks for any hint!

(我正在使用WPF和C#)

(I'm using WPF and C#)

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

Sample Project: http://cid-0432ee4cfe9c26a0.skydrive.live.com/self.aspx/%c3%96ffentlich/WpfApplication5.zip (just run it, instructions are shown in the application)

示例代码:

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)";
    }
}

推荐答案

我建议在可能的情况下使用Preview *事件进行视觉更改,因为这将使您的逻辑保持良好的分离.另外,最好(IMHO)使用Mouse.OverrideCursor属性临时更改光标.

I would recommend using Preview* events where possible for visual changes, as it will keep your logic nicely separated. Also, it is best (IMHO) to use the Mouse.OverrideCursor property to change the cursor temporarily.

例如:

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;
}

这篇关于按下鼠标左键时更改鼠标光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆