利用多线程解决对话框自动消失的办法,虽然有点乱,但是完全正确,自己使用的时候把command1_click的执行动作封装成自己的函数就行了,运行过程,点command1,出现对话框,10秒后对话框消失。(因为我这个程序多次用于测试,所以没用的API声明太多了,去掉没用的就行了) '窗体代码 Option Explicit Private Sub Command1_Click() Command1.Enabled = False id = CreateThread(ByVal 0&, ByVal 2000&, AddressOf closemessagebox, 0, ByVal 0&, id1) MsgBox "a" TerminateThread id, 0 Command1.Enabled = True End Sub Private Sub Form_Load() ProcOld = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc) End Sub Private Sub Form_Unload(Cancel As Integer) SetWindowLong Me.hwnd, GWL_WNDPROC, ProcOld End Sub '模块代码 Option Explicit Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Public Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As RECT) As Long Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Public Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public Type POINTAPI x As Long y As Long End Type Public ProcOld As Long Public Const TPM_LEFTALIGN = &H0& Public Const WM_SYSCOMMAND = &H112 Public Const MF_SEPARATOR = &H800& Public Const MF_STRING = &H0& Public Const GWL_WNDPROC = (-4) Public Const IDM_ABOUT As Long = 1010 Public Const WM_COMMAND = &H111 Public Const WM_ACTIVATE = &H6 Public Const WA_INACTIVE = 0 Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source Public Const WM_CLOSE = &H10 Public Const INFINITE = &HFFFF Public g__thread As Long Public id As Long Public id1 As Long Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case iMsg Case WM_ACTIVATE If wParam = WA_INACTIVE Then Dim mywnd As Long Dim buf As String * 64 Dim oldrect As RECT GetWindowRect hwnd, oldrect mywnd = lParam GetClassName mywnd, buf, 64 If Mid(buf, 1, 6) = "#32770" Then Dim processid As Long GetWindowThreadProcessId mywnd, processid If processid = GetCurrentProcessId Then g__thread = mywnd End If End If End If End Select WindowProc = CallWindowProc(ProcOld, hwnd, iMsg, wParam, lParam) End Function Public Sub closemessagebox() Sleep 10000 If g__thread <> 0 Then SendMessage g__thread, WM_CLOSE, 0, 0 g__thread = 0 End If End Sub |