the most simplest approach
Code: Select all
#SingleInstance Off
#NoEnv
#NoTrayIcon
#MaxMem 128
SetWorkingDir, %A_ScriptDir%
AutoTrim,Off
SetBatchLines,-1
SetControlDelay,-1
SetWinDelay,-1
ListLines, Off
DetectHiddenWindows, On
SetTitleMatchMode,2
SendMode, Input
Process,Priority,,A
CoordMode, Mouse, Relative
iniRead,winid,windowid.ini,winid,winid
if winexist("testtitle ahk_id " . winid)
{
msgbox, already exist
WinActivate,% "testtitle ahk_id " . winid
Exitapp
}
gui, add, button ,, ok
gui, show , w400 h400 ,testtitle
WinGet,winid, ID, A ; getting window id immidiatly after showing window
iniwrite,%winid%,windowid.ini,winid,winid
return
GuiClose:
ExitApp
little more advanced approach to send message to 1st instance to show the gui of first instance if hidden or do other stuff based on the message you send
Code: Select all
#SingleInstance Off
#NoEnv
#NoTrayIcon
#MaxMem 128
SetWorkingDir, %A_ScriptDir%
AutoTrim,Off
SetBatchLines,-1
SetControlDelay,-1
SetWinDelay,-1
ListLines, Off
DetectHiddenWindows, On
SetTitleMatchMode,2
SendMode, Input
Process,Priority,,A
CoordMode, Mouse, Relative
iniRead,winid,windowid.ini,winid,winid
TargetScriptTitle := "testtitle ahk_id" . winid
StringToSend:="alreadyExist" ; just in case you want to send any data to 1st instance and do something based on that
result := Send_WM_COPYDATA(StringToSend, TargetScriptTitle)
if (result=1){
msgbox, already exist `n it will show the already existing instance
exitapp
}
OnMessage(0x004A, "Receive_WM_COPYDATA")
gui, add, button ,, ok
gui, show , w400 h400 ,testtitle
WinGet,winid, ID, A
iniwrite,%winid%,windowid.ini,winid,winid
return
showguitimer:
gui, show, x0 y0
return
GuiClose:
ExitApp
Receive_WM_COPYDATA(wParam, lParam)
{
StringAddress := NumGet(lParam + 2*A_PtrSize) ; Retrieves the CopyDataStruct's lpData member.
Global firstinstance_message := StrGet(StringAddress) ; Copy the string out of the
settimer,showguitimer, -2
return true ; Returning 1 (true) is the traditional way to acknowledge this message.
}
Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle) ; ByRef saves a little memory in this case.
; This function sends the specified string to the specified window and returns the reply.
; The reply is 1 if the target window processed the message, or 0 if it ignored it.
{
VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0) ; Set up the structure's memory area.
; First set the structure's cbData member to the size of the string, including its zero terminator:
SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
NumPut(SizeInBytes, CopyDataStruct, A_PtrSize) ; OS requires that this be done.
NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize) ; Set lpData to point to the string itself.
Prev_DetectHiddenWindows := A_DetectHiddenWindows
Prev_TitleMatchMode := A_TitleMatchMode
DetectHiddenWindows On
SetTitleMatchMode 2
TimeOutTime := 4000 ; Optional. Milliseconds to wait for response from receiver.ahk. Default is 5000
; Must use SendMessage not PostMessage.
SendMessage, 0x004A, 0, &CopyDataStruct,, %TargetScriptTitle%,,,, %TimeOutTime% ; 0x004A is WM_COPYDATA.
DetectHiddenWindows %Prev_DetectHiddenWindows% ; Restore original setting for the caller.
SetTitleMatchMode %Prev_TitleMatchMode% ; Same.
return ErrorLevel ; Return SendMessage's reply back to our caller.
}