ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 611|回复: 9

[已解决] msgbox 自定义消息,那位大佬来挑战一下

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-2-23 09:04 | 显示全部楼层 |阅读模式
本帖最后由 oonly 于 2023-2-23 17:26 编辑

近期想升级excel 64 bit 但是之前的msgbox自定义函数不能使用了,修改了多次无效果,特意让大佬们试试


Option Explicit

Private Const MB_YESNOCANCEL = &H3&
Private Const MB_YESNO = &H4&
Private Const MB_RETRYCANCEL = &H5&
Private Const MB_OKCANCEL = &H1&
Private Const MB_OK = &H0&
Private Const MB_ABORTRETRYIGNORE = &H2&
Private Const MB_ICONEXCLAMATION = &H30&
Private Const MB_ICONQUESTION = &H20&
Private Const MB_ICONASTERISK = &H40&
Private Const MB_ICONINFORMATION = MB_ICONASTERISK
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const IDABORT = 3
Private Const IDRETRY = 4
Private Const IDIGNORE = 5
Private Const IDYES = 6
Private Const IDNO = 7
Private Const IDPROMPT = &HFFFF&
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
Private Type MSGBOX_HOOK_PARAMS
    hwndOwner As Long
    hHook As Long
End Type
Private MSGHOOK As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
    "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias _
    "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, _
    ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function SetDlgItemText Lib "user32" Alias _
    "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, _
    ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias _
    "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
    ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias _
    "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
    (ByVal hHook As Long) As Long
Dim mbFlags As VbMsgBoxStyle
Dim mbFlags2 As VbMsgBoxStyle
Dim mTitle As String
Dim mPrompt As String
Dim But1 As String
Dim But2 As String
Dim But3 As String
Public Function MessageBoxH(hwndThreadOwner As Long, _
        hwndOwner As Long, mbFlags As VbMsgBoxStyle) As Long
    'This function calls the hook
    Dim hInstance As Long
    Dim hThreadId As Long
    hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
    hThreadId = GetCurrentThreadId()
    With MSGHOOK
        .hwndOwner = hwndOwner
        .hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, _
            hInstance, hThreadId)
    End With
    MessageBoxH = MessageBox(hwndOwner, Space$(120), Space$(120), mbFlags)
End Function
Public Function MsgBoxHookProc(ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
    'This function catches the messagebox before it opens
    'and changes the text of the buttons – then removes the hook
    If uMsg = HCBT_ACTIVATE Then
        SetWindowText wParam, mTitle
        SetDlgItemText wParam, IDPROMPT, mPrompt
        Select Case mbFlags
            Case vbAbortRetryIgnore
                SetDlgItemText wParam, IDABORT, But1
                SetDlgItemText wParam, IDRETRY, But2
                SetDlgItemText wParam, IDIGNORE, But3
            Case vbYesNoCancel
                SetDlgItemText wParam, IDYES, But1
                SetDlgItemText wParam, IDNO, But2
                SetDlgItemText wParam, IDCANCEL, But3
            Case vbOKOnly
                SetDlgItemText wParam, IDOK, But1
            Case vbRetryCancel
                SetDlgItemText wParam, IDRETRY, But1
                SetDlgItemText wParam, IDCANCEL, But2
            Case vbYesNo
                SetDlgItemText wParam, IDYES, But1
                SetDlgItemText wParam, IDNO, But2
            Case vbOKCancel
                SetDlgItemText wParam, IDOK, But1
                SetDlgItemText wParam, IDCANCEL, But2
        End Select
        UnhookWindowsHookEx MSGHOOK.hHook
    End If
    MsgBoxHookProc = False
End Function

Public Function BBmsgbox(mhwnd As Long, _
        mMsgbox As VbMsgBoxStyle, Title As String, _
        Prompt As String, Optional mMsgIcon As VbMsgBoxStyle, _
        Optional ButA As String, Optional ButB As String, _
        Optional ButC As String) As String
    'This function sets your custom parameters and returns
    'which button was pressed as a string
    Dim mReturn As Long
    mbFlags = mMsgbox
    mbFlags2 = mMsgIcon
    mTitle = Title
    mPrompt = Prompt
    But1 = ButA
    But2 = ButB
    But3 = ButC
    mReturn = MessageBoxH(mhwnd, GetDesktopWindow(), _
        mbFlags Or mbFlags2)
    Select Case mReturn
        Case IDABORT
            BBmsgbox = But1
        Case IDRETRY
            BBmsgbox = But2
        Case IDIGNORE
            BBmsgbox = But3
        Case IDYES
            BBmsgbox = But1
        Case IDNO
            BBmsgbox = But2
        Case IDCANCEL
            BBmsgbox = But3
        Case IDOK
            BBmsgbox = But1
    End Select
End Function

Sub test()
    Dim mReturn As String
    mReturn = BBmsgbox(1, vbYesNoCancel, "Hi There", "Cool ?", , "Go for it", "No way", "Let me think")
        If mReturn = "Go for it" Then
               MsgBox "1"
          ElseIf mReturn = "No way" Then
                MsgBox "2"
            ElseIf mReturn = "Let me think" Then
        MsgBox "3"
                    End If
    MsgBox "You pressed " + mReturn
End Sub




TA的精华主题

TA的得分主题

发表于 2023-2-23 12:15 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
64位好像要在Declare后面加上Ptrsafe,楼主试一下。

TA的精华主题

TA的得分主题

发表于 2023-2-23 12:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
1.gif

64bit-MessageboxFunc.7z

21.37 KB, 下载次数: 13

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-23 13:12 | 显示全部楼层

感谢 我在虚拟机64excel 运行好像有点卡,能运行
话说64位的excel 真比32位好吗?
我现在32位excel老是卡死,想换64位的

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-23 13:12 | 显示全部楼层
杨建营 发表于 2023-2-23 12:15
64位好像要在Declare后面加上Ptrsafe,楼主试一下。

我试过 不行的 楼下的大佬改好了,感谢

TA的精华主题

TA的得分主题

发表于 2023-2-23 14:55 | 显示全部楼层
oonly 发表于 2023-2-23 13:12
感谢 我在虚拟机64excel 运行好像有点卡,能运行
话说64位的excel 真比32位好吗?
我现在32位excel老是卡 ...

虚拟机下不知道,Excel2016(64位)运行你那个程序正常,无卡顿。
至于Excel到底是32位快还是64位快,别人说了都没用,自己试试才知道,哈哈。

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-23 17:28 | 显示全部楼层
wodewan 发表于 2023-2-23 14:55
虚拟机下不知道,Excel2016(64位)运行你那个程序正常,无卡顿。
至于Excel到底是32位快还是64位快,别 ...

这个自定义msgbox 不能设定4个按钮的吧 我看其他人也有相关的函数,修改过不行

TA的精华主题

TA的得分主题

发表于 2023-2-24 16:20 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
无标题.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-24 18:40 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

可惜不能改成四个 最多3个

TA的精华主题

TA的得分主题

发表于 2023-2-25 00:44 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-19 00:45 , Processed in 0.035196 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表