dispatcher
Goto Top

Powershell Anzeige: Systray immer im Vordergrund

Moin Moin Zusammen,

ich habe ein kleines Systray Programm, welches ein paar allgemeine Pc Informationen etc. anzeigt. Das funktioniert auch. Ist die Systray Anzeige aber gerade aktiv und etwas ploppt in der Taskleiste etwas hoch, bspw. eine Toast Notification oder eine Nachricht eines anderen Icons, verschwindet die Anzeige in den Hintergrund und es nicht mehr alles lesbar. Ich bekomme es aber nicht hin, das die Anzeige immer im Vordergrund ist. Ich würde daher gerne einen kleinen Button oder ähnliches einbauen das die Anzeige minimiert, sonst aber immer im Vordergrund ist.

Folgendes habe ich bisher

$window = Add-Type -memberDefinition @"  
[DllImport("user32.dll")]  
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
"@ -name "Win32ShowWindowAsync" -namespace Win32Functions -passThru  
$window::ShowWindow((Get-Process –id $pid).MainWindowHandle, 0) 


Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')  
[xml]$XAML = @'  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:WpfApp3"  
        Name="window" WindowStyle="None" Width="513" Height="250" Opacity="1.0" AllowsTransparency="False">  
        <Window.Resources>
        <Style TargetType="GridViewColumnHeader">  
            <Setter Property="Background" Value="Black" />  
            <Setter Property="Foreground" Value="White"/>  
            <Setter Property="BorderBrush" Value="Black"/>  
            <Setter Property="FontWeight" Value="Bold"/>  
            <Setter Property="Opacity" Value="1.0"/>  
            <Setter Property="Template">  
                <Setter.Value>
                    <ControlTemplate TargetType="GridViewColumnHeader">  
                    <Border Background="White">  
                    <ContentPresenter></ContentPresenter>
                    </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        </Window.Resources>
    <Grid Name="grid" Height="270" HorizontalAlignment="Left" VerticalAlignment="Top">  
	
	
        <Label Name="Title" Content="COMPUTER INFORMATION" HorizontalAlignment="Left" VerticalAlignment="Top" Width="513" Background="Red" Foreground="White" FontWeight="Bold" FontSize="16" Height="27"/>  
		
        <Label Content="Computername" HorizontalAlignment="Left" Margin="0,27,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="Ethernet IP Addresse" HorizontalAlignment="Left" Margin="0,57,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="WLAN IP Addresse" HorizontalAlignment="Left" Margin="0,87,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="VPN IP Addresse" HorizontalAlignment="Left" Margin="0,117,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="Domain" HorizontalAlignment="Left" Margin="0,147,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="Benutzername" HorizontalAlignment="Left" Margin="0,177,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>  
		<Label Content="Betriebssystem" HorizontalAlignment="Left" Margin="0,207,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="Red" Foreground="White" FontSize="14"/>		  
		
        <TextBox Name="txtHostName" Height="30" Margin="170,27,50,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" IsEnabled="True" AllowDrop="True" BorderThickness="0.5" HorizontalAlignment="Left" Width="343" FontSize="14"/>  
		<TextBox Name="txtEthernetIP" HorizontalAlignment="Left" Height="30" Margin="170,57,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
		<TextBox Name="txtWLANIP" HorizontalAlignment="Left" Height="30" Margin="170,87,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
		<TextBox Name="txtVPNIP" HorizontalAlignment="Left" Height="30" Margin="170,117,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
		<TextBox Name="txtDomain" HorizontalAlignment="Left" Height="30" Margin="170,147,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
		<TextBox Name="txtUsername" HorizontalAlignment="Left" Height="30" Margin="170,177,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
		<TextBox Name="txtOSName" HorizontalAlignment="Left" Height="30" Margin="170,207,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5" FontSize="14"/>  
	
            <ListView Name="listview" SelectionMode="Single" Foreground="White" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False">   
                <ListView.ItemContainerStyle>
                    <Style>
                        <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>  
                        <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>  
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
    </Grid>
</Window>
'@  


$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}  

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}  

Function RefreshData{

$oWMIOS = @()
$oWMIHD = @()
$oWMINIC = @()
$oWMICPU = @()
$oWMIOS = Get-WmiObject win32_OperatingSystem
$oWMIHD = Get-WmiObject Win32_Volume -Filter "DriveType='3'"  
$oWMINIC = Get-WmiObject Win32_NetworkAdapterConfiguration | where Description -Like '*Ethernet*' | Select -Expand IPAddress | Where { $_ -like '1*' }  
$oVPN = Get-WmiObject Win32_NetworkAdapterConfiguration | where Description -Like '*Cisco*' | Select -Expand IPAddress | Where { $_ -like '1*' }  
$oWLAN = Get-WmiObject Win32_NetworkAdapterConfiguration | where Description -Like '*Wireless*' | Select -Expand IPAddress | Where { $_ -like '1*' }  
$oWMICPU = Get-WmiObject win32_processor | select Name, LoadPercentage


$txtUsername.Text = $env:UserName
$txtDomain.Text = $env:USERDNSDOMAIN
$txtHostName.Text = $oWMIOS.PSComputerName


$aOSName = $oWMIOS.name.Split("|")  
$txtOSName.Text = $aOSName


$txtEthernetIP.Text = $oWMINIC 
$txtWLANIP.Text = $oWLAN
$txtVPNIP.Text = $oVPN

}


$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\SystemSettingsBroker.exe")  


$listview.ItemsSource = $disks
$listview.Width = $grid.width*.9 


$gridview = New-Object System.Windows.Controls.GridView 
 

foreach ($column in $columnorder) { 
    $gridcolumn = New-Object System.Windows.Controls.GridViewColumn 
    $gridcolumn.Header = $column 
    $gridcolumn.Width = $grid.width*.20 
    $gridbinding = New-Object System.Windows.Data.Binding $column 
    $gridcolumn.DisplayMemberBinding = $gridbinding 
    $gridview.AddChild($gridcolumn) 
} 
 

$listview.View = $gridview 
 

$notifyicon = New-Object System.Windows.Forms.NotifyIcon 
$notifyicon.Text = "Systeminformation"   
$notifyicon.Icon = $icon 
$notifyicon.Visible = $true 
 
$menuitem = New-Object System.Windows.Forms.MenuItem 
$menuitem.Text = "Beenden"   

$contextmenu = New-Object System.Windows.Forms.ContextMenu 
$notifyicon.ContextMenu = $contextmenu 
$notifyicon.contextMenu.MenuItems.AddRange($menuitem) 
 

$notifyicon.add_Click({ 
    if ($_.Button -eq [Windows.Forms.MouseButtons]::Left) { 
            # reposition each time, in case the resolution or monitor changes 
	    $window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width) 
            $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height) 
            $window.Show() 
            $window.Activate()
    	    RefreshData
    } 
}) 
 
 
$window.Add_MouseDoubleClick({ 
    RefreshData
}) 
 

$window.Add_Deactivated({ 
    $window.Hide()
}) 
 

$menuitem.add_Click({ 
   $notifyicon.Visible = $false 
   $window.close() 
   Stop-Process $pid 
}) 
 

$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'   
$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0) 
 
Force garbage collection just to start slightly lower RAM usage. 
[System.GC]::Collect() 
 
Create an application context for it to all run within. 
This helps with responsiveness, especially when clicking Exit. 
$appContext = New-Object System.Windows.Forms.ApplicationContext 
[void][System.Windows.Forms.Application]::Run($appContext)

Content-Key: 610394

Url: https://administrator.de/contentid/610394

Printed on: April 26, 2024 at 02:04 o'clock