Here is one of the ways not to use any thread.
Using global variables instead of using the file map object will make it simpler.
Code: Select all
#EVENT_SYSTEM_FOREGROUND = $0003
#WINEVENT_INCONTEXT = 4
Procedure WinEventProc(hWinEventHook, event.l, hwnd, idObject.l, idChild.l, idEventThread.l, dwmsEventTime.l)
Protected Buffer.s{64}, *NewPosition.RECT, hMap, rt.RECT, hShell, hTree, hItem, x, y, w, h
If hwnd
If event = #EVENT_SYSTEM_FOREGROUND
If GetClassName_(hwnd, @Buffer, 63)
If Buffer = "#32770"
UnhookWinEvent_(hWinEventHook)
;Get the data of the global memory.
hMap = OpenFileMapping_(#FILE_MAP_ALL_ACCESS, 0, "AppPosData")
If hMap
*NewPosition = MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(RECT))
If *NewPosition
If GetWindowRect_(hwnd, rt)
With rt
If *NewPosition\left = #PB_Ignore : x = \left : Else : x = *NewPosition\left : EndIf
If *NewPosition\top = #PB_Ignore : y = \top : Else : y = *NewPosition\top : EndIf
If *NewPosition\right = #PB_Ignore : w = \right - \left : Else : w = *NewPosition\right : EndIf
If *NewPosition\bottom = #PB_Ignore : h = \bottom - \top : Else : h = *NewPosition\bottom : EndIf
EndWith
SetWindowPos_(hwnd, 0, x, y, w, h, #SWP_NOZORDER | #SWP_NOOWNERZORDER | #SWP_FRAMECHANGED)
;Fix the problem that the selected item is sometimes not visible after resizing.
hShell = GetDlgItem_(hwnd, 0)
If hShell
hTree = GetDlgItem_(hShell, 100)
If hTree
If GetClassName_(hTree, @Buffer, 63)
If FindString(Buffer, "treeview", 1, #PB_String_NoCase)
hItem = SendMessage_(hTree, #TVM_GETNEXTITEM, #TVGN_CARET, 0)
If hItem
SendMessage_(hTree, #TVM_ENSUREVISIBLE, 0, hItem)
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
UnmapViewOfFile_(*NewPosition)
EndIf
CloseHandle_(hMap)
EndIf
EndIf
EndIf
EndIf
EndIf
EndProcedure
Procedure.s New_PathRequester(Title$, InitialPath$, x = #PB_Ignore, y = #PB_Ignore, w = #PB_Ignore, h = #PB_Ignore)
Protected sPath.s, hMap, *NewPosition.RECT
Protected hHook = SetWinEventHook_(#EVENT_SYSTEM_FOREGROUND, #EVENT_SYSTEM_FOREGROUND, GetModuleHandle_(0), @WinEventProc(), GetCurrentProcessId_(), GetCurrentThreadId_(), #WINEVENT_INCONTEXT)
hMap = CreateFileMapping_(#INVALID_HANDLE_VALUE, 0, #PAGE_READWRITE, 0, SizeOf(RECT), "AppPosData")
If hMap
*NewPosition = MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(RECT))
If *NewPosition
*NewPosition\left = x
*NewPosition\top = y
*NewPosition\right = w
*NewPosition\bottom = h
EndIf
EndIf
sPath = PathRequester(Title$, InitialPath$)
If hMap
If *NewPosition
UnmapViewOfFile_(*NewPosition)
EndIf
CloseHandle_(hMap)
EndIf
If hHook
UnhookWinEvent_(hHook)
EndIf
ProcedureReturn sPath
EndProcedure
Debug New_PathRequester("test", "c:\", #PB_Ignore, #PB_Ignore, 300, 300)
Debug New_PathRequester("test", "c:\", 300, #PB_Ignore, 500, 500)
Debug New_PathRequester("test", "c:\", 100, 100)