Start a program like google chrome always on top without external software

6,497

You may try this powershell script. Adjust $exe path if required.

# Your executable here
# $exe = "notepad"
$exe = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

function Set-TopMost($handle) {
  $FnDef = '
  [DllImport("user32.dll")]
  public static extern bool SetWindowPos(int hWnd, int hAfter, int x, int y, int cx, int cy, uint Flags);
  ';
  $user32 = Add-Type -MemberDefinition $FnDef -Name 'User32' -Namespace 'Win32' -PassThru
  $user32::SetWindowPos($handle, -1, 0,0,0,0, 3)
}

$pname = [System.IO.Path]::GetFileNameWithoutExtension($exe)

$p = (Get-Process $pname -ErrorAction "SilentlyContinue") | ? { $_.MainWindowHandle -ne 0 }
if ($p -eq $null) {
  Write-Host "Process '$pname' not found, starting: $exe"
  & $exe
  Write-Host "Waiting for process '$pname'"
  while ($p -eq $null) {
    sleep -Milliseconds 100
    $p = (Get-Process $pname -ErrorAction "SilentlyContinue") | ? { $_.MainWindowHandle -ne 0 }
  }
}

$p.MainWindowHandle
Set-TopMost $p.MainWindowHandle
Share:
6,497

Related videos on Youtube

TobiasW
Author by

TobiasW

Indie AR-Developer

Updated on September 18, 2022

Comments

  • TobiasW
    TobiasW over 1 year

    So as the topic says, I want to start my chrome and keep the window always on top, would that be possible with a script or something? I googled a bit about that topic and if the question is answered, they mostly refer to autohotkey, but that's not what I want. Isn't it possible to write a little script, which could keep the program on top for me? And if this works, how can I start it with a specific url?

    OS: Windows 10

    Background: If games don't support add-ons I saw often "always on top" programs, which could be dragged above the game so you could get an add-on on top of the game. In my case it is "rust", the official website runs a livemap, but the game has no minimap so I thought, it would be nice to have chrome always on top and drag it over the game so I would get a minimap :)

    • Hastur
      Hastur over 8 years
      Please specify the version of the operating system. It will help to find an answer. BTW Chrome, as firefox ..., can start with any default page you want. it's enough to set it the preferences. If instead you are searching something that from remote act as a virus keeping maximized the window with the page you want... I suppose you will not find the answer here ;)
    • TobiasW
      TobiasW over 8 years
      I updated the question for you, and yes you can specify it, but I think this will be more like a private session so it would be interesting to know if you can send a parameter with the start request which sets the url for the browser.
    • DavidPostill
      DavidPostill over 8 years
    • Hastur
      Hastur over 8 years
      E.g. with firefox you can start with firefox -no-remote -P profilename myurl.com and it will start directly with a new session (-no-remote) a desired profile (-P profilename) and the desired URL (myurl.com). This move your research to find a way to start always on top an application. With firefox there is an add-on Always on top that allow you to keep the windows "Always on top" with Ctrl+Alt+T. Try to apply to chrome.
    • TobiasW
      TobiasW over 8 years
      Okay thanks for your answer :) I think this can do the trick, but I hoped for a script where I could click and the window would open always on top, so I could provide this script to other players easily.