TopMost on TopMost

Category: Application Features

Date: 02-16-2022

Return to Index


 
'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
 
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
               PID = Shell("NotePad",1)
               Sleep 100
               hWin = GetHandleFromPID(PID)
               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


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm