英文:
Equivalent to "User32" in 64-bit MSAccess
问题
抱歉,由于代码部分无需翻译,我将跳过代码的部分,以下是关于代码的讨论:
不幸的是,一些用户已经升级到64位版本的Office,而 "user32" 不被识别。是否有一种方法可以重新编码以适用于64位和32位版本?
如果要使此代码兼容64位和32位版本,您可以使用条件编译来处理不同的情况。下面是一个示例,可以帮助您实现这一目标:
#If Win64 Then
Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hWnd As LongPtr, _
ByVal bRevert As Long) As LongPtr
Private Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As _
LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As LongPtr
#Else
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#End If
这个修改后的代码块将根据是否为64位编译环境来选择使用正确的声明。这样,您的代码将在32位和64位版本的Office中都能正常工作。
英文:
I am maintaining several old Microsoft Access Databases and I use the following to stop users from exiting the application except from the menus.
For whatever reason the unload event has never done the job properly so this is what we use:
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&
Public Function SetEnabledState(blnState As Boolean)
Call CloseButtonState(blnState)
End Function
'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
Dim hWnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long
hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub
Unfortunately some users have have been updated to 64-bit versions of office and "user32" is not recognised. Is there a way I could recode this for both 64-bit and 32-bit?
答案1
得分: 1
你应该使用条件编译来实现这个,例如在这里查看:https://stackoverflow.com/questions/5506912/how-should-i-make-my-vba-code-compatible-with-64-bit-windows。
要获取正确的API调用声明,请阅读我的答案https://stackoverflow.com/a/76362787/7599798 - 我使用了一个工具“Windows API Viewer”,但现在有点难找到它。
以下代码应该在32位和64位上都可以工作,不过我只在64位上进行了测试(没有32位可用):
#If VBA7 Then
Private Declare PtrSafe Function GetSystemMenu Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal bRevert As Long) As LongPtr
Private Declare PtrSafe Function EnableMenuItem Lib "user32" ( _
ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#Else
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#End If
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&
Public Function SetEnabledState(blnState As Boolean)
Call CloseButtonState(blnState)
End Function
'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
#If VBA7 Then
Dim hMenu As LongPtr
#Else
Dim hMenu As Long
#End If
Dim hWnd As Long
Dim wFlags As Long
Dim result As Long
hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub
英文:
You should do this with conditional compilation, see for example here: https://stackoverflow.com/questions/5506912/how-should-i-make-my-vba-code-compatible-with-64-bit-windows.
To get the correct declarations for you API calls, read my answer https://stackoverflow.com/a/76362787/7599798 - I use a tool "Windows API Viewer" but it is a little bit tricky nowadays to find it.
The following code should work on 32bit and 64bit, however, I have tested it only on 64bit (have no more 32bit available)
#If VBA7 Then
Private Declare PtrSafe Function GetSystemMenu Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal bRevert As Long) As LongPtr
Private Declare PtrSafe Function EnableMenuItem Lib "user32" ( _
ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#Else
private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#End If
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&
Public Function SetEnabledState(blnState As Boolean)
Call CloseButtonState(blnState)
End Function
'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
#If VBA7 Then
Dim hMenu As LongPtr
#Else
Dim hMenu As Long
#End If
Dim hWnd As Long
Dim wFlags As Long
Dim result As Long
hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub
答案2
得分: 0
尝试使用64位声明:
Private PtrSafe Declare Function GetSystemMenu Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal bRevert As Long) As LongPtr
Private PtrSafe Declare Function EnableMenuItem Lib "user32" ( _
ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
并相应地调整其余的代码。
英文:
Try with the 64-bit declarations:
Private PtrSafe Declare Function GetSystemMenu Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal bRevert As Long) As LongPtr
Private PtrSafe Declare Function EnableMenuItem Lib "user32" ( _
ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
and adjust the rest of the code accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论