VBA6 - > VBA7 - 将代码转换为64位的问题 - 将窗口置于前景(AutoCAD 2014)

时间:2014-09-07 23:36:17

标签: vb.net vba excel-vba autocad-plugin excel

我正在努力将一些非常旧的VBA代码转换为在AutoCAD 2014上运行。到目前为止,我已经转换了所有内容,但表单存在问题(它们是无模式的,需要激活回调才能修改窗口属性)。以下是VBA6源代码:

在表格中:

Private Sub UserForm_Activate()
#If ACAD2000 = 0 Then
    If Not bPopup Then
        Call EnumWindows(AddressOf EnumWindowsProc, vbNull)
        Call SubClass
        bPopup = True
    End If
#End If
End Sub

MODULE(名为modModeLessFormFocus):

Option Explicit

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private ThisHwnd As Long
Public Const GWL_STYLE = -16
Public Const WS_POPUP = &H80000000

Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Integer

    Dim title As String * 32
    Call GetWindowText(hwnd, ByVal title, 32)
    If InStr(title, "About") Then
        ThisHwnd = hwnd
        EnumWindowsProc = False
    ElseIf InStr(title, "Preferences") Then
        ThisHwnd = hwnd
        EnumWindowsProc = False
    ElseIf InStr(title, "Display Block Attributes") Then
        ThisHwnd = hwnd
        EnumWindowsProc = False
    Else
        EnumWindowsProc = True
    End If
End Function

Public Function SubClass() As Long
    Dim Flags As Long
    Flags = GetWindowLong(ThisHwnd, GWL_STYLE)
    Flags = Flags Xor WS_POPUP
    SetWindowLong ThisHwnd, GWL_STYLE, Flags
End Function

我在运行时遇到的错误是"类型不匹配"在UserForm_Activate on' AddressOf EnumWindowsProc。'我曾尝试使用PtrSafe和PtrLong将其转换为64位,但不可避免地会失败并且程序崩溃。

如果有人足够聪明地将其转换为或指向正确的方向,我将非常感激。

由于

1 个答案:

答案 0 :(得分:2)

我在http://www.jkp-ads.com/articles/apideclarations.asp

中找到了64位VBA7的API
#If VBA7 Then
    #If Win64 Then
        Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
    #Else
        Private Declare PtrSafe Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
    #End If
    Private Declare PtrSafe Function GetWindowText Lib "USER32" Alias "GetWindowTextA" _
                                      (ByVal hWnd As LongPtr, ByVal lpString As String, _
                                       ByVal cch As LongPtr) As Long

#Else
    Private Declare Function GetWindowLongPtr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 
    Private Declare Function GetWindowText Lib "USER32" Alias "GetWindowTextA" _
                                      (ByVal hWnd As Long, ByVal lpString As String, _
                                       ByVal cch As Long) As Long
#End If

您还可以查看http://msdn.microsoft.com/en-us/library/aa383663(VS.85).aspx了解更新后的API