1、在程序启动是写入用户数据 2、判断程序是否已经启动 3、根据用户数据来查找窗口 4、最后最大化窗口 ======================================== 1、为程序写入自己的数据 Public Function fun_WSetUserData(ByVal lng_hwnd As Long, ByVal lng_UserData As Long) As Long fun_WSetUserData = SetWindowLong(lng_hwnd, GWL_USERDATA, lng_UserData) End Function 2、If App.PrevInstance = True then 3、恢复已经启动的程序,0表示失败 该例子是通过两个用户数据来查找窗口 Public Function fun_WindowRestore(ByVal lng_UserData1 As Long, ByVal lng_UserData2 As Long) As Long '0表示失败 Dim lng_Ret As Long Dim lng_hwnd As Long Dim lng_User As Long lng_hwnd = 0 Do lng_Ret = FindWindowEx(0, ByVal lng_hwnd, vbNullString, vbNullString) If lng_Ret <> ERROR_SUCCESS Then lng_hwnd = lng_Ret lng_User = GetWindowLong(lng_hwnd, GWL_USERDATA) If lng_User = lng_UserData1 Or lng_User = lng_UserData2 Then lng_Ret = ShowWindow(lng_hwnd, SW_RESTORE) '把SW_RESTORE该为最大化即可。 lng_Ret = SetForegroundWindow(lng_hwnd) Exit Do End If End If Loop Until lng_Ret = ERROR_SUCCESS If lng_Ret = 0 Then MsgBox "该进程的一个副本已经在运行!", vbInformation, "提示" End If fun_WindowRestore = lng_Ret End Function
Private Sub Form_Load() If App.PrevInstance Then Dim sTitle As String ' Save my title sTitle = Me.Caption ' Change my title bar so I won’t activate myself Me.Caption = Hex$(Me.hWnd) ' Activate other instance AppActivate sTitle ' Terminate myself SendKeys "% R", True End End If End Sub
WINNT+VB6:測試通過! 在Form1中放置一個Timer1Form1中的代碼如下: Private Sub Timer1_Timer() Form1.WindowState = 1 Timer1.Enabled = False End Sub新建Module1,放置如下代碼: Public Flag As Long Private Const SC_MAXIMIZE = &HF030& Private Const WM_SYSCOMMAND = &H112 Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim slength As Long, buffer As String ' title bar text length and buffer Dim retval As Long ' return value
Static winnum As Integer ' counter keeps track of how many windows have been enumerated winnum = winnum + 1 ' one more window enumerated.... slength = GetWindowTextLength(hwnd) + 1 ' get length of title bar text If slength > 1 Then ' if return value refers to non-empty string buffer = Space(slength) ' make room in the buffer retval = GetWindowText(hwnd, buffer, slength) ' get title bar text If Left(buffer, slength - 1) = "Form1" Then
SendMessage hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0 SetForegroundWindow hwnd Flag = Flag + 1 End If End If EnumWindowsProc = 1 ' return value of 1 means continue enumeration End Function Public Sub Main() EnumWindows AddressOf EnumWindowsProc, 0 If Flag <> 0 Then End Form1.Show End Sub注意:1.Timer1的Interval設為100 2.工程的啟動設為sub main enjoy it please!
補充一點: If Left(buffer, slength - 1) = "Form1" 中的"Form1"一定要設為程序的標題名
然后向窗口发送消息,这得用到api了
然后向窗口发送消息,这得用api了
api自己应该能查到的
2、判断程序是否已经启动
3、根据用户数据来查找窗口
4、最后最大化窗口
========================================
1、为程序写入自己的数据
Public Function fun_WSetUserData(ByVal lng_hwnd As Long, ByVal lng_UserData As Long) As Long
fun_WSetUserData = SetWindowLong(lng_hwnd, GWL_USERDATA, lng_UserData)
End Function
2、If App.PrevInstance = True then
3、恢复已经启动的程序,0表示失败 该例子是通过两个用户数据来查找窗口
Public Function fun_WindowRestore(ByVal lng_UserData1 As Long, ByVal lng_UserData2 As Long) As Long '0表示失败
Dim lng_Ret As Long
Dim lng_hwnd As Long
Dim lng_User As Long
lng_hwnd = 0
Do
lng_Ret = FindWindowEx(0, ByVal lng_hwnd, vbNullString, vbNullString)
If lng_Ret <> ERROR_SUCCESS Then
lng_hwnd = lng_Ret
lng_User = GetWindowLong(lng_hwnd, GWL_USERDATA)
If lng_User = lng_UserData1 Or lng_User = lng_UserData2 Then
lng_Ret = ShowWindow(lng_hwnd, SW_RESTORE) '把SW_RESTORE该为最大化即可。
lng_Ret = SetForegroundWindow(lng_hwnd)
Exit Do
End If
End If
Loop Until lng_Ret = ERROR_SUCCESS
If lng_Ret = 0 Then
MsgBox "该进程的一个副本已经在运行!", vbInformation, "提示"
End If
fun_WindowRestore = lng_Ret
End Function
If App.PrevInstance Then
Dim sTitle As String
' Save my title
sTitle = Me.Caption
' Change my title bar so I won’t activate myself
Me.Caption = Hex$(Me.hWnd)
' Activate other instance
AppActivate sTitle
' Terminate myself
SendKeys "% R", True
End
End If
End Sub
在Form1中放置一個Timer1Form1中的代碼如下:
Private Sub Timer1_Timer()
Form1.WindowState = 1
Timer1.Enabled = False
End Sub新建Module1,放置如下代碼:
Public Flag As Long
Private Const SC_MAXIMIZE = &HF030&
Private Const WM_SYSCOMMAND = &H112
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim slength As Long, buffer As String ' title bar text length and buffer
Dim retval As Long ' return value
Static winnum As Integer ' counter keeps track of how many windows have been enumerated winnum = winnum + 1 ' one more window enumerated....
slength = GetWindowTextLength(hwnd) + 1 ' get length of title bar text
If slength > 1 Then ' if return value refers to non-empty string
buffer = Space(slength) ' make room in the buffer
retval = GetWindowText(hwnd, buffer, slength) ' get title bar text
If Left(buffer, slength - 1) = "Form1" Then
SendMessage hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0
SetForegroundWindow hwnd
Flag = Flag + 1
End If End If EnumWindowsProc = 1 ' return value of 1 means continue enumeration
End Function
Public Sub Main()
EnumWindows AddressOf EnumWindowsProc, 0
If Flag <> 0 Then End
Form1.Show
End Sub注意:1.Timer1的Interval設為100
2.工程的啟動設為sub main
enjoy it please!
If Left(buffer, slength - 1) = "Form1"
中的"Form1"一定要設為程序的標題名