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

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the