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

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