Private Sub Form_Load()
Dim OldWindowText As String
Dim Hwnd As Long
OldWindowProc = SetWindowLong(Form1.Hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
If App.PrevInstance Then
OldWindowText = Me.Caption
Me.Caption = "appCopy"
Hwnd = FindWindow(vbNullString, OldWindowText)
SetForegroundWindow Hwnd
PostMessage Hwnd, Runtwice, 0, 0
Unload Me
End If
End Sub
'''''''code in moudle''''''''
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal Hwnd As Long) 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 FlashWindow Lib "user32" (ByVal Hwnd As Long, ByVal bInvert 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 Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
Public Const Runtwice = (WM_USER + 1001)
Public Const WM_SYSCOMMAND = &H112
Public Const SC_RESTORE = &HF120&
Public OldWindowProc As Long
Public OldWindowState As Integer
Public LastWindowMsg As Long
Public Function NewWindowProc(ByVal Hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = Runtwice Then
If OldWindowState = 2 Then
PostMessage Hwnd, WM_SYSCOMMAND, SC_RESTORE, 0&
End If
Form1.Timer1.Enabled = True
ElseIf Msg = 71 Then
OldWindowState = Form1.WindowState + 1
End If
NewWindowProc = CallWindowProc(OldWindowProc, Hwnd, Msg, wParam, lParam)
LastWindowMsg = Msg
End Function