【问题标题】:Make my code wait until cursor is loading MS Excel 2016让我的代码等到光标加载 MS Excel 2016
【发布时间】:2018-10-16 11:29:24
【问题描述】:

我正在做一些自动化工作来重复复制粘贴工作。有时服务器会很慢。那时我会使用下面的代码等到光标等待恢复正常

Option Explicit

Private Const IDC_WAIT As Long = 32514

Private Type POINT
x As Long
y As Long
End Type

Private Type CURSORINFO
cbSize As Long
flags As Long
hCursor As Long
ptScreenPos As POINT
End Type

Private Declare Function GetCursorInfo _
Lib "user32" (ByRef pci As CURSORINFO) As Boolean
Private Declare Function LoadCursor _
Lib "user32" Alias "LoadCursorA" _
(ByVal hInstance As Long, ByVal lpCursorName As Long) As Long

Public Function IsWaitCursor() As Boolean

' Get handle to wait cursor
Dim handleWaitCursor As Long
handleWaitCursor = LoadCursor(ByVal 0&, IDC_WAIT)

Dim pci As CURSORINFO
pci.cbSize = Len(pci)

' Retrieve information about the current cursor
Dim ret As Boolean
ret = GetCursorInfo(pci)

If ret = False Then
    MsgBox "GetCursorInfo failed", vbCritical
    Exit Function
End If

' Returns true when current cursor equals to wait cursor
IsWaitCursor = (pci.hCursor = handleWaitCursor)

End Function

以上代码在 MS Excel 2013 32 位中对我来说运行良好。但现在我使用的是 MS Excel 64 位,上面的代码不起作用。有人请告诉我需要做什么

【问题讨论】:

  • 当你说:code is not working,是不是抛出了错误?还是不等待?
  • 也可能有帮助 - 引用:“LongPtr 是您的 Declare 语句中任何指针或句柄的完美类型。...不要忘记一个非常重要的细节:不仅是您的 @987654324 @ 语句应该使用LongPtr 数据类型编写,您的程序调用外部API 函数实际上也必须使用LongPtr 类型“ - 参见Windows API declaration VBA 64 bit

标签: excel vba


【解决方案1】:
Private Const IDC_WAIT As Long = 32514
Private Type POINT
X As Long
Y As Long
End Type

Private Type CURSORINFO
cbSize As Long
flags As Long
hCursor As LongPtr
ptScreenPos As POINT
End Type

Private Declare PtrSafe Function GetCursorInfo _
Lib "User32" (ByRef pci As CURSORINFO) As Boolean
Private Declare PtrSafe Function LoadCursor Lib "User32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As LongPtr

Public Function IsWaitCursor() As Boolean

' Get handle to wait cursor
Dim handleWaitCursor As LongPtr
handleWaitCursor = LoadCursor(ByVal 0&, IDC_WAIT)

Dim pci As CURSORINFO
pci.cbSize = Len(pci)

' Retrieve information about the current cursor
Dim ret As Boolean
ret = GetCursorInfo(pci)

If ret = False Then
    MsgBox "GetCursorInfo failed", vbCritical
    Exit Function
End If

' Returns true when current cursor equals to wait cursor
IsWaitCursor = (pci.hCursor = handleWaitCursor)

End Function

上面的代码对我有用。我将 Long 数据类型更改为 LongPtr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2019-04-09
    • 2010-10-08
    • 2017-09-17
    相关资源
    最近更新 更多