Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
Enum Equates Singular
IDC_Button
End Enum
Global hDlg, hWin As Dword, PID As Dword, EXEName As WStringZ * %Max_Path, fName$
Function PBMain() As Long
Dialog New Pixels, 0, "PowerBASIC",300,300,300,100, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
SetWindowPos(hDlg, %HWND_TOPMOST, 0, 0, 0, 0, %SWP_NOMOVE Or %SWP_NOSIZE) 'on Top
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button
fName$ = "gbsnippets_temp.bas"
If IsFalse IsFile(fName$) Then WinBeep(300,200) : Exit Function
FindExecutable((fName$), Exe.Path$, ExeName)
If IsFalse IsFile(ExeName) Then WinBeep(300,200) : Exit Function
PID = Shell(EXEName + " " + fName$,1)
While hWin=0
hWin = GetHandleFromPID(PID)
Wend
Dialog Set Text hDlg, Str$(hWin)
SetWindowPos(hWin, %HWND_TOPMOST, 0, 0, 0, 0, %SWP_NOMOVE Or %SWP_NOSIZE) 'on Top of calling app
End Select
End Select
End Function
Function GetHandleFromPid(ByVal Pid As Dword)As Dword
Local hTry As Dword
Local pidTry As Dword
Local idThread As Dword
hTry = GetForegroundWindow()
hTry = GetTopWindow(%HWND_Desktop)
Do While hTry 'Scan all handles via GetWindow() to find the pid
If GetParent(hTry) = 0 Then
idThread = GetWindowThreadProcessId(hTry, pidTry)
If pidTry = Pid Then
Function = hTry
Exit Do
End If
End If
hTry = GetWindow(hTry, %GW_HWNDNEXT)
Loop
End Function
http://www.garybeene.com/sw/gbsnippets.htm