如何在 Microsoft Access 中显示非阻塞“toast”之类的通知?那确实有某种动画,不应该阻止主机应用程序!
最佳答案
我的 friend 问我关于非阻塞 toast 的问题,比如 ms Access 的通知。我的第一个想法是,检查谷歌你会发现很多样本。他对他得到的 sample 并不满意。
他想要类似(JQuery)非阻塞通知的东西。用户需要知道但不一定需要交互的东西。
由于在 VBA 中无法进行线程处理,我想,如果您可以编写自己的 .dll 会怎样?所以我最终编写了一个 .NET DLL,它可以被(windows)VBA 代码 Access 并显示一个 toast 通知。
(实际 dll 创建和从 vba Access .NET dll 是我稍后将介绍的另一个主题)( You can read more in my blog 根据您的意愿留下评论或建议。)
现在,您可以从这里下载我创建的 DLL:
HERE
编辑:以上下载链接和 GitHub 链接已更新为我认为属于作者的工作链接。
如果您担心下载未知的 DLL:VirusTotal Scan report
将 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
如果要自定义 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>
显示通知调用此方法:
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"
用法 :
您可以将其用于任何非交互警报......例如登录成功,或操作取消警报或用户不需要按 OK 确认您的消息的任何内容。
目标
我将在 GitHub 上上传 Dll 项目,并请求其他 VBA C# 专家的贡献,以使其更加美观并可供所有 VBA 开发人员使用。
这是我的 GitHub 链接:GitHub
请尽可能多地贡献,并让每个人都可以使用它:)
如果您可以保留主类名称,我会很高兴。
一个 sample :
关于ms-access - 非阻塞 "toast"类似 Microsoft Access (VBA) 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224308/