PoshCode Archive  Artifact [12cad42cf4]

Artifact 12cad42cf49b60fd803e728fd2e539a2bc625ff4edd50ca184528e2e1702fc1d:

  • File Out-Balloon.ps1 — part of check-in [e9fe01dc53] at 2018-06-10 12:56:15 on branch trunk — With just a few arguments, it is easy to make some text appear in a little balloon. (user: cz9qvh size: 4629)

# encoding: utf-8
# api: powershell
# title: Out-Balloon.ps1
# description: With just a few arguments, it is easy to make some text appear in a little balloon.
# version: 0.1
# type: script
# author: cz9qvh
# license: CC0
# x-poshcode-id: 1006
# x-derived-from-id: 1051
# x-archived: 2016-06-09T22:53:19
# x-published: 2009-04-08T12:13:00
#
# You can specify an icon file (*.ico) with the -icon argument, if you don’t then 
# the first icon of the host is used.
# out-balloon accepts pipeline input, strings only please.
# It blocks for the duration of the balloon display, 3 seconds by default.  Simple
# fixes for this are welcome.
# timeout should be an integer value.
# INSTALLATION:
# um, save this text in a file named out-balloon.ps1 in your path
#
<#
.Synopsis
    Makes a baloon tip in the notification area
.Description
    With just a few arguments, it is easy to make some text appear in a little balloon.
    
    You can specify an icon file (*.ico) with the -icon argument, if you don't then 
    the first icon of the host is used.
    
    out-balloon accepts pipeline input, strings only please.
    
    It blocks for the duration of the balloon display, 3 seconds by default.  Simple
    fixes for this are welcome.
    
    timeout should be an integer value.
    
    INSTALLATION:
        um, save this text in a file named out-balloon.ps1 in your path
        
.Example
    out-balloon "hey, your job is done." -icon "C:\Program Files\Microsoft Office\Office12\MYSL.ICO"
    
    "job done." | out-balloon
#>    
param(
    [Parameter(ValueFromPipeline=$true,Position=0,Mandatory=$true)]
    [Alias('output')]
    [string]$text,
    [string]$icon,
    [int32]$timeout = 3
    )
begin
{
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

    $not = new-object System.Windows.Forms.NotifyIcon
    if ($icon -eq $null)
    {
        if ($win32extraciconex -eq $null)
        {
            $script:ExtractIconEx = Add-Type –memberDefinition @"
                [DllImport("Shell32", CharSet=CharSet.Auto)]
                private static extern int ExtractIconEx (string lpszFile, int nIconIndex,
                    IntPtr[] phIconLarge,IntPtr[] phIconSmall,int nIcons);

                [DllImport("user32.dll", EntryPoint="DestroyIcon", SetLastError=true)]
                private static extern int DestroyIcon(IntPtr hIcon);

                public static System.Drawing.Icon ExtractIconFromExe(string file, bool large)
                {
                    int readIconCount = 0;
                    IntPtr[] hDummy  = new IntPtr[1] {IntPtr.Zero};
                    IntPtr[] hIconEx = new IntPtr[1] {IntPtr.Zero};

                    try
                    {
                        if(large)
                            readIconCount = ExtractIconEx(file,0, hIconEx, hDummy, 1);
                        else
                            readIconCount = ExtractIconEx(file,0, hDummy, hIconEx, 1);

                        if(readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
                        {
                            System.Drawing.Icon extractedIcon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(hIconEx[0]).Clone();
                            return extractedIcon;
                        }
                        else // NO ICONS READ
                            return null;
                    }
                    catch(Exception ex)
                    {
                        throw new ApplicationException("Failed extracting icon", ex);
                    }
                    finally
                    {
                        foreach(IntPtr ptr in hIconEx)
                        if(ptr != IntPtr.Zero)
                            DestroyIcon(ptr);

                        foreach(IntPtr ptr in hDummy)
                        if(ptr != IntPtr.Zero)
                            DestroyIcon(ptr);
                    }
                
                }
"@  -name “Win32ExtractIconEx” -namespace win32api –passThru -ReferencedAssemblies "System.Drawing"
        }

        $not.Icon = $extractIconEx::ExtractIconFromExe((get-process -Id $pid).mainmodule.filename, $true)
    }
    else
    {
        $not.Icon = new-object System.Drawing.Icon($icon)
    }
    $not.visible = $true
}
process
{

    $not.BalloonTipText = $text
    $not.ShowBalloonTip($timeout)
    sleep $timeout
}
end
{
    $not.dispose()
}