7,763
社区成员
发帖
与我相关
我的任务
分享
If AhWnd = Form1.hwnd OR AhWnd = Form2.hwnd Then'注意AhWnd必须是自己程序的窗口句柄。
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOWNORMAL As Long = 1
Dim AhWnd As Long '前台活动窗口句柄
Private Sub Command1_Click()
Unload Me
End Sub
Private 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 If
End Sub
Private Sub Form_Load()
Me.Caption = Me.hwnd
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Private 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 If
End Sub