Skip to main content

Draw Using Transparent Colors with PowerShell

Have you ever had to walk an end user through some PowerShell commands?  It is not pretty.  We all have different skill sets. As IT Professionals we need to remember that our skill set does not easily translate well to our users.  They want pretty pictures to click on and simplistic processes.  One area of PowerShell actually involves creating GUIs.  I do not recommend GUIs for everything.  Only if you need to put your code in front of someone else.

The most important part of developing a GUI is to make sure your PowerShell code runs first.  Without solid code doing the intended task, your GUI will just be a waste of time.  Developing GUIs can be time consuming without the right tools.

This post is about how to draw with transparent colors.  To set this up, I used SAPIEN PowerShell Studio 2015 as my tool to help accelerate my GUI development. 

To set this up, I created an empty form.  Next I added a PictureBox control which I renamed PB1 and also set PB1’s size property to 256, 256.

This is what our final product will look like:



Here is the code:
<#
===============================================================================
Transparent Color Demonstration
Jason A. Yoder
Twitter: @JasonYoder_MCT

Created with SAPIEN PowerShell Studio
===============================================================================
#>

# Create your brushes with their colors
$RBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 255, 0, 0))
$GBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 0, 255, 0))
$BBrush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromArgb(100, 0, 0, 255))

# Create the rectangles that the circles will fit inside of.
$RRec = [System.Drawing.Rectangle]::New(78, 50, 100, 100)
$GRec = [System.Drawing.Rectangle]::New(50, 95, 100, 100)
$BRec = [System.Drawing.Rectangle]::New(106, 95, 100, 100)

# Events ______________________________________________________________________

$form1_Load={
# No activities need to take place in the load event.
      
}#end form1_Load

$PB1_Paint=[System.Windows.Forms.PaintEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
      
       # Paint the circles
       $_.Graphics.FillEllipse($RBrush, $RRec)
       $_.Graphics.FillEllipse($GBrush, $GRec)
       $_.Graphics.FillEllipse($BBrush, $BRec)
      
      
}#end PB1_Paint


Let’s talk about what is going on in this code.

We use System.Drawing.SolidBrush objects to create the brushes to use to paint our ellipses.  Ellipses are used to draw circles.  We also use System.Drawing.Color objects to define our colors.  When we define our colors, we called the FromArgb method. ARGB stands for Alpha (Transparency), Red, Green, and Blue.  For the Alpha channel, 255 is opaque while 0 is transparent.  For the three color values, 255 is that channel all the way on while 0 is off.

The System.Drawing.Rectangle objects define where to draw our ellipses.  The first 2 numbers are the upper left corner position.  The next two values are the width and height.

We then call the Paint event for our PictureBox.  This event creates a temporary variable $_.  Using  $_ to render our drawings allow for the PictureBox controls double buffering to be used.  This prevents flickering if you are doing any animation.  Using $_ we call the graphics object and then the FillEllipse method.  We provide the brush to use and the location to draw. 

The rest is just running the code.

If you picked up on the hint about animation, that is tomorrow.






Comments

Popular posts from this blog

With the AD Recycle Bin Turned on, What Happens when you Create a User Account with a Password that does not meet the Password Policy?

This was an interesting observation from one of my Windows Server 2012 classes.  While working with the AD Recycle bin in a lab, one of my students discovered some interesting accounts that were created. When he created user accounts that did not meet password complexity requirements, an account is temporarily made and then deleted.  When a new password is provided that meets the password requirements, then a new account is made. We discovered this in two places.  First off in the Active Directory Administrative Center.  This is what caused the initial confusion.  Take a look.  This is in the Deleted Objects OU. You can see multiple deleted accounts for Test2 and one for Test3.  Test3 is a valid, functioning user account.  Using the PowerShell command Get-ADObject –IncludeDeletedObjects –Filter * –Properties ObjectSID we can see that indeed, two accounts were created, with one of them deleted. Notice the RID portion of the SID is different. ...

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

Backup and Restore AD LDS with DSDBUTIL.exe

Active Directory Lightweight Directory Services allow you to create a directory service that allows applications to have access to user accounts, groups, and authentication similar to Active Directory Domain Services.  The big advantage here is that the schema of the directory service will not be bound by the rules of an Active Directory database.  Exchange 2007/2010, for example, use an instance of AD LDS on the Edge Transport Server to provide for user authentication from the internet.  Because your Active Directory database is not exposed to the internet, this is more secure. Applications will handle most of the dirty work should they require AD LDS.  You may want to make sure the database is being backed up and also have a restore plan in place.  Should the database become corrupt, the application that uses that database will fail.  This document will walk you through backing up and restoring an instance of AD LDS using the dsdbutil.exe command. Fi...