PoshCode Archive  Artifact [989f6246e1]

Artifact 989f6246e1ef565b786448c18ea34754b2137ac1d34ce0d5d8f6a4873af1acf9:

  • File TeamViewer-Panel.ps1 — part of check-in [d0b8dd1621] at 2018-06-10 13:45:12 on branch trunk — Set applications/windows on hide of TeamViewer windows (user: Shay Levy size: 2910)

# encoding: ascii
# api: powershell
# title: TeamViewer Panel
# description: Set applications/windows on hide of TeamViewer windows
# version: 0.1
# type: class
# author: Shay Levy
# license: CC0
# function: Set-TopMost
# x-poshcode-id: 4738
# x-derived-from-id: 4755
# x-archived: 2016-07-01T08:16:09
# x-published: 2016-12-24T10:23:00
#
#
#Requires -Version 2.0 

$signature = @"
	
	[DllImport("user32.dll")]  
	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  

	public static IntPtr FindWindow(string windowName){
		return FindWindow(null,windowName);
	}

	[DllImport("user32.dll")]
	public static extern bool SetWindowPos(IntPtr hWnd, 
	IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);

	[DllImport("user32.dll")]  
	public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

	static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
	static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);

	const UInt32 SWP_NOSIZE = 0x0001;
	const UInt32 SWP_NOMOVE = 0x0002;

	const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

	public static void MakeTopMost (IntPtr fHandle)
	{
		SetWindowPos(fHandle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
	}

	public static void MakeNormal (IntPtr fHandle)
	{
		SetWindowPos(fHandle, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
	}
"@


$app = Add-Type -MemberDefinition $signature -Name Win32Window -Namespace ScriptFanatic.WinAPI -ReferencedAssemblies System.Windows.Forms -Using System.Windows.Forms -PassThru

function Set-TopMost
{
	param(		
		[Parameter(
			Position=0,ValueFromPipelineByPropertyName=$true
		)][Alias('MainWindowHandle')]$hWnd=0,

		[Parameter()][switch]$Disable

	)

	
	if($hWnd -ne 0)
	{
		if($Disable)
		{
			Write-Verbose "Set process handle :$hWnd to NORMAL state"
			$null = $app::MakeNormal($hWnd)
			return
		}
		
		Write-Verbose "Set process handle :$hWnd to TOPMOST state"
		$null = $app::MakeTopMost($hWnd)
	}
	else
	{
		Write-Verbose "$hWnd is 0"
	}
}



function Get-WindowByTitle($WindowTitle="*")
{
	Write-Verbose "WindowTitle is: $WindowTitle"
	
	if($WindowTitle -eq "*")
	{
		Write-Verbose "WindowTitle is *, print all windows title"
		Get-Process | Where-Object {$_.MainWindowTitle} | Select-Object Id,Name,MainWindowHandle,MainWindowTitle
	}
	else
	{
		Write-Verbose "WindowTitle is $WindowTitle"
		Get-Process | Where-Object {$_.MainWindowTitle -like "*$WindowTitle*"} | Select-Object Id,Name,MainWindowHandle,MainWindowTitle
	}
}

Exaples:

# set powershell console on top of other windows 
gps powershell | Set-TopMost 

# unset
gps powershell | Set-TopMost -disable


# set an application on top of other windows by its windows title (wildcard is supported)
Get-WindowByTitle *pad* | Set-TopMost 

# unset
Get-WindowByTitle textpad | Set-TopMost -Disable