Skip to main content

Adding Some Animation into Your GUI

Yesterday, I posted some code on how to draw on a GUI using transparent colors.  To further demonstrate this transparency, we are going to add some animation.


You can use your code from yesterday and just add to it.  The only object that you need to add is a timer control. Simply drag the timer control from the Toolbox in SAPIEN PowerShell Studio onto your form.


The timer control will appear under he actual form in the Designer tab.


Here are some differences from yesterday’s code.  (The entire code will be posted below)
First we need to set the bounds for the circles to move.  They will be bouncing off the walls.  Remember that the ellipse origin is the upper left corner, not the center.  That is why our right and bottom boundaries are 156 and not 256.  The ellipses have a size of 100 pixels.
# Set the X and Y max and min values.
$XMin = 0
$YMin = 0
$XMax = 156
$YMax = 156

We need to declare the starting positions of all three ellipses.  Since these values will be modified in events, we assign them to be script level variables. We need to provide both X and Y positions for all three ellipses.
# Set the starting values for the circles.
$Script:RX = 78 ; $Script:RY = 50
$Script:GX = 50; $Script:GY = 95
$Script:BX = 106; $Script:BY = 95

We need to provide a rate of movement each time our timer’s Tick event executes. Here we will send all three ellipses into different directions.
# Set the directions
$Script:RXMove = 1 ; $Script:RYMove = 1
$Script:GXMove = 1; $Script:GYMove = -1
$Script:BXMove = -1; $Script:BYMove = 1

We need to set an interval value for the timer.  The interval determines how long windows will wait before executing the Tick event of the timer.  It is in milliseconds.  Once set, we execute the Start method.
# Set the speed of the motion by controlling how fast the Timer ticks.
$Timer1.Interval = 10 # Milliseconds.

# Start the timer.
$timer1.Start()

In the paint event, we need to change the X and Y values to reflect or position variables as opposed to hard coding them.
# Create the rectangles that the circles will fit inside of.
$RRec = [System.Drawing.Rectangle]::New($Script:RX, $Script:RY, 100, 100)
$GRec = [System.Drawing.Rectangle]::New($Script:GX, $Script:GY, 100, 100)
$BRec = [System.Drawing.Rectangle]::New($Script:BX, $Script:BY, 100, 100)
      
# Paint the circles
$_.Graphics.FillEllipse($RBrush, $RRec)
$_.Graphics.FillEllipse($GBrush, $GRec)
$_.Graphics.FillEllipse($BBrush, $BRec)

The $timer1_Tick event is next.  We need to monitor the values of our position variables.  Since we are only changing them by a value of 1, we can look to see when they are equal to our boundaries.  If they do equal the boundary, then we multiple the movement direction by -1.  This reverses the movement’s directions.
# Change directions if a value hits a boundary
If ($Script:RX -eq $XMin -or $Script:RX -eq $XMax) { $Script:RXMove *= (-1) }
If ($Script:GX -eq $XMin -or $Script:GX -eq $XMax) { $Script:GXMove *= (-1) }
If ($Script:BX -eq $XMin -or $Script:BX -eq $XMax) { $Script:BXMove *= (-1) }

If ($Script:RY -eq $YMin -or $Script:RY -eq $YMax) { $Script:RYMove *= (-1) }
If ($Script:GY -eq $YMin -or $Script:GY -eq $YMax) { $Script:GYMove *= (-1) }
If ($Script:BY -eq $YMin -or $Script:BY -eq $YMax) { $Script:BYMove *= (-1) }

Next we commit the movement values to the current position values.

# Change the position values
$Script:RX += $Script:RXMove
$Script:GX += $Script:GXMove
$Script:BX += $Script:BXMove

$Script:RY += $Script:RYMove
$Script:GY += $Script:GYMove
$Script:BY += $Script:BYMove

Finally, we instruct the picture box, $PB1 to invoke its Paint event.  This will use the PictureBox Control’s double buffer to redraw the ellipses without any flickering.
# Tell the Paint box to redraw itself.
$PB1.Refresh()


Here is the code in its entirety.  Remember to set up the form in SAPIEN PowerShell Studio as described in yesterday’s post with the modifications from todays.

<#
===============================================================================
Transparent Color Demonstration with Animation
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))

# Set the X and Y max and min values.
$XMin = 0
$YMin = 0
$XMax = 156
$YMax = 156

# Set the starting values for the circles.
$Script:RX = 78 ; $Script:RY = 50
$Script:GX = 50; $Script:GY = 95
$Script:BX = 106; $Script:BY = 95

# Set the directions
$Script:RXMove = 1 ; $Script:RYMove = 1
$Script:GXMove = 1; $Script:GYMove = -1
$Script:BXMove = -1; $Script:BYMove = 1

# Set the speed of the motion by controlling how fast the Timer ticks.
$Timer1.Interval = 10 # Milliseconds.

# Start the timer.
$timer1.Start()

# Events ______________________________________________________________________

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

$PB1_Paint=[System.Windows.Forms.PaintEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
      
       # Create the rectangles that the circles will fit inside of.
       $RRec = [System.Drawing.Rectangle]::New($Script:RX, $Script:RY, 100, 100)
       $GRec = [System.Drawing.Rectangle]::New($Script:GX, $Script:GY, 100, 100)
       $BRec = [System.Drawing.Rectangle]::New($Script:BX, $Script:BY, 100, 100)
      
       # Paint the circles
       $_.Graphics.FillEllipse($RBrush, $RRec)
       $_.Graphics.FillEllipse($GBrush, $GRec)
       $_.Graphics.FillEllipse($BBrush, $BRec)
      
}#end PB1_Paint

$timer1_Tick={
      
       # Change directions if a value hits a boundary
       If ($Script:RX -eq $XMin -or $Script:RX -eq $XMax) { $Script:RXMove *= (-1) }
       If ($Script:GX -eq $XMin -or $Script:GX -eq $XMax) { $Script:GXMove *= (-1) }
       If ($Script:BX -eq $XMin -or $Script:BX -eq $XMax) { $Script:BXMove *= (-1) }
      
       If ($Script:RY -eq $YMin -or $Script:RY -eq $YMax) { $Script:RYMove *= (-1) }
       If ($Script:GY -eq $YMin -or $Script:GY -eq $YMax) { $Script:GYMove *= (-1) }
       If ($Script:BY -eq $YMin -or $Script:BY -eq $YMax) { $Script:BYMove *= (-1) }
      
       # Change the position values
       $Script:RX += $Script:RXMove
       $Script:GX += $Script:GXMove
       $Script:BX += $Script:BXMove
      
       $Script:RY += $Script:RYMove
       $Script:GY += $Script:GYMove
       $Script:BY += $Script:BYMove
      
       # Tell the Paint box to redraw itself.
       $PB1.Refresh()
} #end timer1_Tick



Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

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.