65.9K
CodeProject 正在变化。 阅读更多。
Home

在 VB.NET 中更改系统光标

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.15/5 (8投票s)

2008年7月15日

CPOL
viewsIcon

61965

downloadIcon

1208

替换系统光标并在需要时将其恢复到原始光标。

引言

我一直从这个网站获取帮助,所以现在该回报了。这是我在这里的第一篇帖子,请多多包涵 :P

本文的目的是向您展示如何系统范围内替换光标,以及**具体地**,如何在准备好后将其恢复到原始光标。

背景

更改系统光标对我来说很简单,但当我想要将系统光标恢复到原始光标时出现了一个问题。许多人建议使用 LoadCursorLoadImage API 来恢复光标,但似乎(至少对我来说)这两个 API 在 VB.NET 中都无法工作。因此,我独自想出了以下解决方案。

使用代码

我不确定这是否是最好的方法,但它似乎运行得很好。示例项目的主要代码如下

'Variable to save current cursor
Dim SavedCursor As Icon

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    'Save cursor
    SavedCursor = Icon.FromHandle(Cursors.Arrow.CopyHandle)

    'Change arrow cursor to mine
    Dim NewCursor As IntPtr = _
        LoadCursorFromFile(Application.StartupPath & "\MyCross.cur")

    'Check
    If NewCursor = IntPtr.Zero Then
        'Error loading cursor from file
        Debug.WriteLine("Error loading cursor from file.")
        Return
    End If

    'Set the system cursor
    If SetSystemCursor(NewCursor, IDC_ARROW) = 0 Then
        'Error setting system cursor
        Debug.WriteLine("Error setting system cursor.")
        Return
    End If

    'Disable/enable buttons
    Button1.Enabled = False
    Button2.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    'Get old cursor
    Dim OldCursor As IntPtr = SavedCursor.Handle

    'Set the system cursor
    SetSystemCursor(OldCursor, IDC_ARROW)

    'Disable/enable buttons
    Button1.Enabled = True
    Button2.Enabled = False
End Sub

关注点

此示例是在 Visual Studio 2008 中使用 .NET Framework 3.5 编写和测试的。任何反馈都将不胜感激(尤其是如果有人有任何改进)。

历史

  • 发布版本 1(2008 年 7 月 15)。
© . All rights reserved.