按下鼠标左键时更改鼠标光标?

发布于 2024-09-04 05:54:30 字数 1806 浏览 2 评论 0原文

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

(我正在使用 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)";
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-09-11 05:54:30

我建议尽可能使用 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;
}

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;
}
故乡的云 2024-09-11 05:54:30

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

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

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

In left mouse down handler you can have following code.

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

You can reset to Default cursor as per your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文