Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Custom icons in Powershell winforms

Since Ed Wilson has been on a Primal Forms kick (view one of the articles here)…. and since I happened to use Primal Forms community edition for my Scriptomania entry I thought I would share my method for creating a custom icon within a winform using Powershell.  When talking about icons we’re talking about the graphic that sits in the top left corner of a winform:

If you’re using primal forms you can easily add an icon to your winform by creating an ico file, and then browsing to the file within the Window Style->Icon property of the main form:

Doing this will use the ExtractAssociatedIcon() method of System.Drawing.Icon to create the icon from the file.  Here is the code that Primal Forms generates for you:

$form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Users\ttanasovski\scripts\toenuff.ico')

To make your icon there are a ton of tools out there.  Favicon generator is a great site if you want to quickly draw and export a 16×16 ico file.  This is the perfect size for a simple one-letter winform .ico.  The interface is very clean and simple:

Fine and dandy, but when you’re talking about a script I always feel one of the goals should be extreme portability.

I want to be able to hand some text to my colleague so he/she can use it immediately without having to worry about needing supporting files.  Because of this I had to find a way to generate my icon within Powershell itself.  In the end I found that I could use the System.Drawing.Graphics of a new 16×16 System.Drawing.Bitmap:

$bmp = New-Object System.Drawing.Bitmap(16,16)
$g = [System.Drawing.Graphics]::FromImage($bmp)

Once I had this I could use the numerous graphics methods available to draw my icon.  Take the following example that draws an X:

$g.drawline([System.Drawing.Pens]::Black,0,0,15,15)
$g.drawline([System.Drawing.Pens]::Black,0,15,15,0)

Once my doodling was completed I could pass the GetHicon() method of Bitmap to the FromHandle static method of System.Drawing.Icon to create the icon:

$ico = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())

Finally, I only needed to set the icon property of the winform to the icon I created:

$form1.Icon = $ico

Here is the final product that creates my winform, draws out my lovely T for Toe/Toenuff/Tome/Terrific, turns it into an icon, and then sets the icon to the form:

function GenerateForm {
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    $form1 = New-Object System.Windows.Forms.Form
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    ###
    # Icon generation and loading into form
    ###
    $bmp = New-Object System.Drawing.Bitmap(16,16)
    $g = [System.Drawing.Graphics]::FromImage($bmp)
    $g.drawline([System.Drawing.Pens]::Black,0,0,15,0)
    $g.drawline([System.Drawing.Pens]::Black,15,0,15,4)
    $g.drawline([System.Drawing.Pens]::Black,15,4,10,4)
    $g.drawline([System.Drawing.Pens]::Black,10,4,10,15)
    $g.drawline([System.Drawing.Pens]::Black,10,15,6,15)
    $g.drawline([System.Drawing.Pens]::Black,6,15,6,4)
    $g.drawline([System.Drawing.Pens]::Black,6,4,0,4)
    $g.drawline([System.Drawing.Pens]::Black,0,4,0,0)
    $g.drawline([System.Drawing.Pens]::Blue,1,1,14,1)
    $g.drawline([System.Drawing.Pens]::Blue,1,2,14,2)
    $g.drawline([System.Drawing.Pens]::Blue,1,3,14,3)
    $g.drawline([System.Drawing.Pens]::Blue,7,4,7,14)
    $g.drawline([System.Drawing.Pens]::Blue,8,4,8,14)
    $g.drawline([System.Drawing.Pens]::Blue,9,4,9,14)
    $ico = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
    $form1.Icon = $ico
    ###
    # End of the icon generation and loading
    ###
    $form1.Text = "Icon Form"
    $form1.Name = "form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 284
    $System_Drawing_Size.Height = 262
    $form1.ClientSize = $System_Drawing_Size
    $InitialFormWindowState = $form1.WindowState
    $form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null
}
GenerateForm
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: