tags:

views:

296

answers:

1

Hello,

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!

(I'm using WPF and C#)

EDIT:

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

Code for the sample:

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>

Window class:

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)";
    }
}
+1  A: 

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.

For example:

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 Roche
Hello Dennis, thank you very much for the Mouse.OverrideCursor hint! It doesn't work for the sample above (after clicking a button, no mousemove event is being fired) however it works for my real application (where I need this during drag sample above was a reduced sample where the problem is another one: mousemove isn't fired when the left mouse button is down). So while this isn't the answer to my sample, it still solved my problem :-)
stefan.at.wpf
@Stefan: I tested it with the sample, however thought the MouseMove event was your attempt at changing the cursor so I omitted it.
Dennis Roche
@Stefan: By the way, this is *not* how you change the cursor to give user feedback during a drag/drop operation. Take a look at DragDrop.GiveFeedback and DragDrop.QueryContinueDrag events (http://bit.ly/bbP14Y).
Dennis Roche
I'm using the GiveFeedback Event, but there I still have to use Mouse.OverrideCursor or did I miss something?
stefan.at.wpf