软件自动还原已经打开的窗口
程序远行后,用户点了窗口右上角最小化后.过了很长时间,用户有时已经忘了打开过程序了,当再次点EXE时可以判断原来的程序是否在远行,如果有在运行的话,可以提示不能重复运行.
If App.PrevInstance = True Then
MsgBox "软件已经运行!"
End
End If
现在的问题是能否在提示完不能重复运行后,软件自动还原已经打开的窗口.是打开过的窗口恢复原来的大小,而不是在最小化的状态.
[解决办法]
Option ExplicitPrivate Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Const SW_SHOWNORMAL As Long = 1Dim AhWnd As Long '前台活动窗口句柄Private Sub Command1_Click() Unload MeEnd SubPrivate Sub Form_Initialize() Dim FileNumber As Long, Rtn As Long FileNumber = FreeFile If App.PrevInstance Then Open "c:\AhWnd.hwnd" For Binary As #FileNumber Get #FileNumber, 4, Rtn Close #FileNumber MsgBox "软件已经运行!" & "进程一句柄:" & Rtn & " 进程二句柄:" & Me.hwnd Call ShowWindow(Rtn, SW_SHOWNORMAL) Unload Me: End End IfEnd SubPrivate Sub Form_Load() Me.Caption = Me.hwnd Timer1.Interval = 100 Timer1.Enabled = TrueEnd SubPrivate Sub Timer1_Timer() Dim FileNumber As Long AhWnd = GetForegroundWindow FileNumber = FreeFile If AhWnd = Me.hwnd Then Open "c:\AhWnd.hwnd" For Binary As #FileNumber Put #FileNumber, 4, AhWnd Close #FileNumber Timer1.Enabled = False End IfEnd Sub