Kae Travis

Custom Tooltips in PowerShell Forms using OwnerDraw

Other Posts in this Series:

This post provides an example of how we can override the default behaviour to show custom tooltips in PowerShell Forms using OwnerDraw.

Add-Type -AssemblyName System.Drawing
#define brushes for black and white
$whiteBrush = new-object Drawing.SolidBrush White
$blackBrush = new-object Drawing.SolidBrush Black
#create form
$form = New-Object System.Windows.Forms.Form
$alkaneButton = New-Object System.Windows.Forms.Button
$alkaneButton.Size = New-Object System.Drawing.Size(200, 40)
$alkaneButton.Location = New-Object System.Drawing.Size(20, 20)
$alkaneButton.Text = "Alkane Button"
$form.Controls.Add($alkaneButton)
$tooltip1 = New-Object System.Windows.Forms.ToolTip
$tooltip1.SetToolTip($alkaneButton, "This is a tooltip.")
$tooltip1.OwnerDraw = $true 
$tooltip1_Draw=[System.Windows.Forms.DrawToolTipEventHandler]{
#define custom font
$fontstyle = new-object System.Drawing.Font('Segoe UI', 9, [System.Drawing.FontStyle]::Regular)
#configure alignment
$format = [System.Drawing.StringFormat]::GenericTypographic
$format.LineAlignment = [System.Drawing.StringAlignment]::Center
$format.Alignment = [System.Drawing.StringAlignment]::Center    
#draw tooltip    
$_.Graphics.FillRectangle($blackBrush, $_.Bounds)
$_.Graphics.DrawString($_.ToolTipText, $fontstyle, $whiteBrush, ($_.Bounds.X + ($_.Bounds.Width/2)), ($_.Bounds.Y + ($_.Bounds.Height/2)), $format)         
}
$tooltip1.Add_Draw($tooltip1_Draw)
$Add_FormClosed=
{
try
{
$tooltip1.Remove_Draw($tooltip1_Draw)
}
catch { Out-Null }
}
[void]$form.ShowDialog()
Custom Tooltips in PowerShell Forms using OwnerDraw
Custom Tooltips in PowerShell Forms using OwnerDraw

Leave a Reply