Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, February 10, 2016

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



No comments: