在Excel VBA 2013中获取光标状态

3

我创建了一个宏来进行一系列的鼠标点击和鼠标移动(按键宏),以输入重复数据到Oracle(程序/数据库)。

之前我使用Dataload Classic或Dataloader Classic(按键程序)来将数据输入到Oracle,但是它缺乏“智能”,所以我创建了自己带有一些“智能”的按键程序。

我使用SLEEP命令/函数在每次鼠标移动和鼠标点击后等待几秒钟/毫秒。有时Oracle会变慢并“暂停”/“加载”或“冻结”,而且冻结时间可能超过SLEEP命令的初始等待时间,然后继续执行程序,从而弄乱一切。

示例:

if something_happens then
sleep 2000
end if

在DataLoad Classic/Dataloader Classic中,有选项可以更改您可以等待/暂停每次鼠标点击或鼠标移动等的时间。有一个“HOURGLASS CHECK”。这表示您可以设置程序等待的时间,如果鼠标处于沙漏状态,则用户可以输入毫秒或秒。

是否有Excel VBA代码来检查鼠标的HOURGLASS状态?


光标状态是什么意思?它在表格中的哪里?如果加载轮正在转动,那么检测到某些事情正在发生的更好方法可能不仅仅是光标动画所显示的。 - BruceWayne
我正在模块中编写它,我已经在网上搜索了很久,但是没有找到任何有用的信息。也许我没有使用正确的术语进行搜索。@BruceWayne - Proggie
2
我们的主要问题是为什么要检查光标是否有某些操作?这听起来像是 XY 问题。我认为你的“真正”问题在于如何确定循环何时完成(当鼠标停止成为沙漏)? - BruceWayne
1
有一个“Application.Cursor”属性。你试过了吗?作为测试,可以尝试以下代码:如果 Application.Cursor = xlWait Then MsgBox "foo"。你可能需要在“Do While”循环中包含一个检查。这是MSDN文档链接:https://msdn.microsoft.com/zh-cn/library/office/ff198335.aspx?f=255&MSPPError=-2147217396 - sous2817
显示剩余5条评论
2个回答

3
您可以尝试以下函数,该函数使用win api函数LoadCursorGetCursorInfo来确定当前光标是否等于等待光标。
该函数首先加载win预定义的等待光标,然后获取当前光标并检查它们是否相同。希望对您有所帮助。
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

0
尝试检查 screen.MousePointer 属性是否为忙碌状态(沙漏形状)。
While screen.MousePointer = 11
      Sleep(500)
Wend

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接