对啊,其实只要对原来的代码稍作一点改动,问题就解决了,不需要苦苦追寻很难实现的方法的。
用class吧 添加一个bool属性ShowMsgbox 不管是本form调用还是外部调用 都预设一下值.和全局变量的作用相同 好处在于每个class都有自己的属性 不会互相冲突.全局变量你要是同时打开的form过多 会被改来改去的.
既然每个窗口都大同小异 干嘛不定义一个基类 用继承来做呢
'以下放到Moudle中
'**************************************
' Name: Auto close messagebox
' Description:This function replaces VB'
' s msgbox function and closes itself afte
' r the parameter provided number of secon
' ds. The syntax and return values are exa
' ctly the same as msgbox except the first
' parameter is the number of seconds to di
' splay. Just add this code to a module (n
' ot a cls or frm) in your project and cal
' l ACmsgbox. Thanks to Sparq's submission
' here for help in writing this.
'With the added parameter of
' By: Daniel Biener
'
'This code is copyrighted and has' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=27940&lngWId=1'for details.'**************************************
'**************************************
'Windows API/Global Declarations for :Au
' to close messagebox
'**************************************
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Const NV_CLOSEMSGBOX As Long = &H5000&
Private sLastTitle As String
Public Function ACmsgbox(AutoCloseSeconds As Long, prompt As String, Optional buttons As Long, _
Optional title As String, Optional helpfile As String, _
Optional context As Long) As Long
sLastTitle = title
SetTimer Screen.ActiveForm.hWnd, NV_CLOSEMSGBOX, AutoCloseSeconds * 1000, AddressOf TimerProc
ACmsgbox = MsgBox(prompt, buttons, title, helpfile, context)
End Function
Private Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Dim hMessageBox As Long
KillTimer hWnd, idEvent
Select Case idEvent
Case NV_CLOSEMSGBOX
hMessageBox = FindWindow("#32770", sLastTitle)
If hMessageBox Then
Call SetForegroundWindow(hMessageBox)
SendKeys "{enter}"
End If
sLastTitle = vbNullString
End Select
End Sub
'调用代码
Private Sub Command1_Click()
ACmsgbox 3, "test", vbYes, "test"
End Sub