像Microsoft Access(VBA)的通知一样非阻塞"吐司"

kri*_* KM 12 ms-access notifications vba nonblocking access-vba

我要去ASK并回答我认为对MS Access中一些很酷的UI功能感兴趣的人有用的问题. 回答自己的问题

问题: 如何在Microsoft Access中显示非阻塞"吐司"之类的通知?确实有一些动画,不应该阻止主机应用程序!

kri*_* KM 15

我的朋友问我关于ms访问的非阻塞吐司通知.我的第一个想法是,检查谷歌你会发现大量的样本.他对他得到的样品不满意.

他想要(JQuery)非阻塞通知.用户需要知道但不一定需要互动的东西.

由于在VBA中无法进行线程化,我想,如果你能编写自己的.dll怎么办?所以我最终编写了一个.NET DLL,可以通过(windows)VBA代码访问并显示Toast通知. (实际的dll创建和从vba访问.NET dll是我将在稍后介绍的另一个主题)(您可以在我的博客中阅读更多内容,根据您的意愿留下评论或建议.)

现在,您可以从这里下载我创建的DLL: HERE

编辑:以上下载链接和GitHub链接已更新为我认为属于作者的工作链接.

如果您担心下载未知的DLL:VirusTotal Scan报告

将DLL添加到应用程序的根文件夹,并将以下代码添加到您的应用程序.

'Module level public variable

Public gTOASTER As Object

' to save window metrics
Public Type RECT
    Left        As Long  ' x1
    Top         As Long  ' y1
    Right       As Long  ' x2
    Bottom      As Long  ' y2
End Type

#If VBA7 Then 
    Public Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
    Public Declare PtrSafe Function KRISH_VBA_TOOLS Lib "VBA_TOOLS.dll" () As Object
    Public Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, ByRef lpRect As RECT) As LongPtr 
#Else
    Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal strFilePath As String) As Long
    Public Declare Function KRISH_VBA_TOOLS Lib "VBA_TOOLS.dll" () As Object
    Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, ByRef lpRect As RECT) As LongPtr
#End If

Public Function FN_TOAST_DLL(iMessage As String, Optional iCLOSE_DURATION As Long = 3000, Optional iType As String = "success", Optional iANIME_DURATION As Long = 1000, Optional iFONT_COLOR As String = "#FFFFFF", Optional iX As Long = 0, Optional iY As Long = 0, Optional iANIME_DIRECTION As Integer = 1, Optional iPARENT_HWND As Long = 0)

On Error GoTo LABEL_EXIT_ROUTINE:

    If gTOASTER Is Nothing Then
        LoadLibrary (FN_APP_GET_BASE_PATH & "VBA_TOOLS.dll")
        Set gTOASTER = KRISH_VBA_TOOLS()
        GoTo LABEL_TOAST
    Else
        GoTo LABEL_TOAST
    End If

    On Error GoTo 0
    Exit Function

LABEL_EXIT_ROUTINE:
    msgbox iMessage & vbnewline & err.description
    Exit Function

LABEL_TOAST:
    'set background color. (pass any html color code)
    Select Case iType
        Case "error"
            iType = "#F76160"
        Case "success"
            iType = "#26ad82"
        Case Else
            iType = "#26ad82"
    End Select

    'if parent object is provided show the toast on top of the parent. if custom x, y is provided use x,y coordinated. if none provided use access app's locaiton.
    Dim mRect As RECT
    If iPARENT_HWND <= 0 Then
        If iX = 0 And iY = 0 Then
            GetWindowRect Application.hWndAccessApp, mRect

            iANIME_DIRECTION = 0 'anim direction 0 to down and 1 to up
        End If
    Else ' iPARENT_HWND > 0 Then 'parent_hwnd is null
        GetWindowRect iPARENT_HWND, mRect
    End If

    'set up some offsets
    iX = mRect.Left + 360
    iY = mRect.Top + 1


    On Error Resume Next
    gTOASTER.FN_SHOW_TOAST iMessage, iCLOSE_DURATION, iType, iANIME_DURATION, iFONT_COLOR, iX, iY, iANIME_DIRECTION

End Function

Public Function FN_APP_GET_BASE_PATH()
    Dim FN As String
    FN = Application.CurrentProject.path
    If VBA.Right(Application.CurrentProject.path, 1) <> "\" Then FN = FN & "\"
    FN_APP_GET_BASE_PATH = FN
End Function
Run Code Online (Sandbox Code Playgroud)

如果要自定义fn_toast_dll函数,则从DLL中获取参数列表:

'    /// <summary>
'    ///
'    /// </summary>
'    /// <param name="iMessage">Message to display</param>
'    /// <param name="iDuration">Duration in Milliseconds to keep the toast before fading out..</param>
'    /// <param name="iBG_COLOR">HTML color code for your toast background...</param>
'    /// <param name="iANIME_DURATION">Millisecond value used to for fading in and out the Toast.. 1/4 is used to fade in rest to fade out..</param>
'    /// <param name="iFONT_COLOR">HTML Color code for the font..</param>
'    /// <param name="iX">x position on the screen. where the toast should appear</param>
'    /// <param name="iY">y position on the screen where the toast should appear</param>
'    /// <param name="iANIM_DIRECTION">{0,1} 0 will show/add further notifications downwards and 1 upwards.</param>
'    /// <returns></returns>
Run Code Online (Sandbox Code Playgroud)

显示通知调用此方法:

FN_TOAST_DLL "hello this is a green test" ' By default a success message with 3 seconds will be "toasted"
FN_TOAST_DLL "hello this is an error", 15000, "error"
Run Code Online (Sandbox Code Playgroud)

用法:

您可以将此用于任何非交互式警报,例如登录成功,操作取消警报或任何用户无需按OK确认您的消息.

目标 我将在GitHub上传Dll项目,并要求其他VBA C#专家提供帮助,使其更加出色,适用于所有VBA开发人员.

这是我的GitHub链接:GitHub 请尽可能多地贡献并让每个人都可以使用:)如果您可以保留主要的类名,我会很高兴.

一个样品: Dll吐司vba

  • 太好了!如果您可以发布DLL源代码,那将是有帮助的.也许在你的答案中添加一个描述参数的函数头(例如,我不确定`iANIME_DIRECTION`是做什么的).谢谢. (3认同)