如何在 Visio 中以编程方式更改光标?

发布于 2024-11-11 14:49:14 字数 87 浏览 3 评论 0原文

您好,有什么方法可以以编程方式更改 Visio 中的鼠标光标吗? 我浏览了 Visio SDK 中的所有自动化类,但找不到任何相关的属性、方法、事件......

Hi is there any way I can change the mouse cursor in Visio programmatically?
I went through all the Automation classes in Visio SDK and could not find any related property, method, event....

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

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

发布评论

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

评论(1

甜警司 2024-11-18 14:49:14

-- 编辑:即使您可以通过编程方式更改光标,但 Visio(我的计算机中为 2003)似乎会不断恢复原始光标。我已经尝试过,如果我不移动鼠标,我可以获得不同的光标(如手),直到我移动鼠标,然后它返回到箭头。

所以,现在我的答案是:你不能改变光标。

也许其他 Visio 版本也可以。


您可以使用 VBA 代码中的 Windows API 调用来更改光标。

这里有一个例子: http://www.vbaexpress.com/kb/getarticle .php?kb_id=929

一个更好的例子,我必须在 Visio 中工作:http://www.tek-tips.com/viewthread.cfm?qid=1700789

下面是我用于测试环境的代码:

首先,创建一个“modCursor”模块:

Option Explicit

'Declare Windows API Constants for Windows System cursors.
Public Const IDC_APPSTARTING = 32650&    'Standard arrow and small hourglass.
Public Const IDC_ARROW = 32512&          'Standard arrow.
Public Const IDC_CROSS = 32515           'Crosshair.
Public Const IDC_HAND = 32649            'Hand.
Public Const IDC_HELP = 32651            'Arrow and question mark.
Public Const IDC_IBEAM = 32513&          'Text I-beam.
Public Const IDC_ICON = 32641&           'Windows NT only: Empty icon.
Public Const IDC_NO = 32648&             'Slashed circle.
Public Const IDC_SIZE = 32640&           'Windows NT only: Four-pointed arrow.
Public Const IDC_SIZEALL = 32646&        'Four-pointed arrow pointing north, south, east, and west.
Public Const IDC_SIZENESW = 32643&       'Double-pointed arrow pointing northeast and southwest.
Public Const IDC_SIZENS = 32645&         'Double-pointed arrow pointing north and south.
Public Const IDC_SIZENWSE = 32642&       'Double-pointed arrow pointing northwest and southeast.
Public Const IDC_SIZEWE = 32644&         'Double-pointed arrow pointing west and east.
Public Const IDC_UPARROW = 32516&        'Vertical arrow.
Public Const IDC_WAIT = 32514&           'Hourglass.

'Declarations for API Functions.
Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Declare Function DestroyCursor Lib "user32" (ByVal hCursor As Long) As Long

'Declare handles for cursor.
Private hOldCursor As Long
Private hNewCursor As Long

'The UseCursor function will load and set a system cursor or a cursor from file to a
'controls event property.
Public Function UseCursor(ByVal NewCursor As Variant)

    'Load new cursor.
    Select Case TypeName(NewCursor)
        Case "String" 'Custom cursor from file.
            hNewCursor = LoadCursorFromFile(NewCursor)
        Case "Long", "Integer" 'System cursor.
            hNewCursor = LoadCursor(ByVal 0&, NewCursor)
        Case Else 'Do nothing
    End Select
    'If successful set new cursor.
    If (hNewCursor > 0) Then
        hOldCursor = SetCursor(hNewCursor)
    End If
    'Clean up.
    hOldCursor = DestroyCursor(hNewCursor)
    hNewCursor = DestroyCursor(hOldCursor)

End Function

其次,创建一个类模块“MouseListener”:

Option Explicit

Dim WithEvents vsoWindow As Window

Private Sub Class_Initialize()

    Set vsoWindow = ActiveWindow

End Sub

Private Sub Class_Terminate()

    Set vsoWindow = Nothing

End Sub

Private Sub vsoWindow_MouseDown(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    If Button = 1 Then

        Debug.Print "Left mouse button clicked"

    ElseIf Button = 2 Then

        Debug.Print "Right mouse button clicked"

    ElseIf Button = 16 Then

        Debug.Print "Center mouse button clicked"

    End If

End Sub

Private Sub vsoWindow_MouseMove(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    Debug.Print "x-position is "; x
    Debug.Print "y-position is "; y

    modCursor.UseCursor modCursor.IDC_HAND

End Sub

Private Sub vsoWindow_MouseUp(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    If Button = 1 Then

        Debug.Print "Left mouse button released"
        modCursor.UseCursor modCursor.IDC_HAND

    ElseIf Button = 2 Then

        Debug.Print "Right mouse button released"
        modCursor.UseCursor modCursor.IDC_ARROW

    ElseIf Button = 16 Then

        Debug.Print "Center mouse button released"

    End If

End Sub

第三,将以下代码插入到“ThisDocument”模块中:

Private myMouseListener As MouseListener

Private Sub Document_DocumentSaved(ByVal doc As IVDocument)

Set myMouseListener = New MouseListener

End Sub

Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)

Set myMouseListener = Nothing

End Sub

现在,通过移动鼠标并单击按钮,您可以在立即窗口中获得一些信息。

如果单击左键,光标会变成手形,但是当您再次移动鼠标时,光标会变回来。我能想到的唯一解释是 Visio 的事件正在根据(视觉)上下文更改光标图标。

问候,

-- Edit: Even while you can programmatically change the cursor, it seems that Visio (2003 in my computer) continuously restores the original cursor. I've tried it and, if I don't move the mouse, I can get a different cursor (like the hand) until I move the mouse, then it goes back to the arrow.

So, for now, my answer is: you can't change the cursor.

Maybe it is possible for other Visio versions.


You can use Windows API calls from your VBA code to change the cursor.

There is an example here: http://www.vbaexpress.com/kb/getarticle.php?kb_id=929

A better example, which I have got to work in Visio: http://www.tek-tips.com/viewthread.cfm?qid=1700789

And below, the code I have used for the testing environment:

First, create a "modCursor" module:

Option Explicit

'Declare Windows API Constants for Windows System cursors.
Public Const IDC_APPSTARTING = 32650&    'Standard arrow and small hourglass.
Public Const IDC_ARROW = 32512&          'Standard arrow.
Public Const IDC_CROSS = 32515           'Crosshair.
Public Const IDC_HAND = 32649            'Hand.
Public Const IDC_HELP = 32651            'Arrow and question mark.
Public Const IDC_IBEAM = 32513&          'Text I-beam.
Public Const IDC_ICON = 32641&           'Windows NT only: Empty icon.
Public Const IDC_NO = 32648&             'Slashed circle.
Public Const IDC_SIZE = 32640&           'Windows NT only: Four-pointed arrow.
Public Const IDC_SIZEALL = 32646&        'Four-pointed arrow pointing north, south, east, and west.
Public Const IDC_SIZENESW = 32643&       'Double-pointed arrow pointing northeast and southwest.
Public Const IDC_SIZENS = 32645&         'Double-pointed arrow pointing north and south.
Public Const IDC_SIZENWSE = 32642&       'Double-pointed arrow pointing northwest and southeast.
Public Const IDC_SIZEWE = 32644&         'Double-pointed arrow pointing west and east.
Public Const IDC_UPARROW = 32516&        'Vertical arrow.
Public Const IDC_WAIT = 32514&           'Hourglass.

'Declarations for API Functions.
Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Declare Function DestroyCursor Lib "user32" (ByVal hCursor As Long) As Long

'Declare handles for cursor.
Private hOldCursor As Long
Private hNewCursor As Long

'The UseCursor function will load and set a system cursor or a cursor from file to a
'controls event property.
Public Function UseCursor(ByVal NewCursor As Variant)

    'Load new cursor.
    Select Case TypeName(NewCursor)
        Case "String" 'Custom cursor from file.
            hNewCursor = LoadCursorFromFile(NewCursor)
        Case "Long", "Integer" 'System cursor.
            hNewCursor = LoadCursor(ByVal 0&, NewCursor)
        Case Else 'Do nothing
    End Select
    'If successful set new cursor.
    If (hNewCursor > 0) Then
        hOldCursor = SetCursor(hNewCursor)
    End If
    'Clean up.
    hOldCursor = DestroyCursor(hNewCursor)
    hNewCursor = DestroyCursor(hOldCursor)

End Function

Second, create a Class Module, "MouseListener":

Option Explicit

Dim WithEvents vsoWindow As Window

Private Sub Class_Initialize()

    Set vsoWindow = ActiveWindow

End Sub

Private Sub Class_Terminate()

    Set vsoWindow = Nothing

End Sub

Private Sub vsoWindow_MouseDown(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    If Button = 1 Then

        Debug.Print "Left mouse button clicked"

    ElseIf Button = 2 Then

        Debug.Print "Right mouse button clicked"

    ElseIf Button = 16 Then

        Debug.Print "Center mouse button clicked"

    End If

End Sub

Private Sub vsoWindow_MouseMove(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    Debug.Print "x-position is "; x
    Debug.Print "y-position is "; y

    modCursor.UseCursor modCursor.IDC_HAND

End Sub

Private Sub vsoWindow_MouseUp(ByVal Button As Long, ByVal KeyButtonState As Long, ByVal x As Double, ByVal y As Double, CancelDefault As Boolean)

    If Button = 1 Then

        Debug.Print "Left mouse button released"
        modCursor.UseCursor modCursor.IDC_HAND

    ElseIf Button = 2 Then

        Debug.Print "Right mouse button released"
        modCursor.UseCursor modCursor.IDC_ARROW

    ElseIf Button = 16 Then

        Debug.Print "Center mouse button released"

    End If

End Sub

Third, insert the following code into the "ThisDocument" module:

Private myMouseListener As MouseListener

Private Sub Document_DocumentSaved(ByVal doc As IVDocument)

Set myMouseListener = New MouseListener

End Sub

Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)

Set myMouseListener = Nothing

End Sub

Now, by moving the mouse and clicking the buttons you get some information in the immediate window.

If you click the left button, the cursor changes to the hand, but when you move the mouse again, the cursor changes back. The only explanation I can think of is that Visio's events are changing the cursor icon depending on the (visual) context.

Regards,

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