Home > other >  How can I create a blinking button with Powershell / XAML?
How can I create a blinking button with Powershell / XAML?

Time:02-26

The following code changes the color of the button on mouseover.
But I would like to change the color every 500ms without mouseover.
What do I need to do for that?

Thanks very much for help!
Tigerix

# Load the window from XAML
$Window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))

# Custom function to add a button
Function Add-Button {
    Param($Content)
    $Button = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonXaml))
    $ButtonText = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonTextXaml))
    $ButtonText.Text = "$Content"
    $Button.Content = $ButtonText
    $Button.Content.FontSize = "16"
    $Button.Content.FontWeight = "Bold"
    $Button.Content.Background = "#E6E6E6" # LightGray
    
    $Button.Add_MouseEnter({
        $This.Content.Background = "#00A387"            
        $This.Content.Foreground = "white"          
    })
    $Button.Add_MouseLeave({
        $This.Content.Background = "#D8D8D8"
        $This.Content.Foreground = "black"          
    })
    $Button.Add_Click({
        New-Variable -Name WPFMessageBoxOutput -Value $($This.Content.Text) -Option ReadOnly -Scope Script -Force
        $Window.Close()
    })
    $Window.FindName('ButtonHost').AddChild($Button)
}

CodePudding user response:

You should be able to do that with a Timer control:

Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase

Give your button a Name upon creation, so you can use $Window.FindName('FlashingButton')

$Button.Name = 'FlashingButton'

Use a Timer to have the background color change every 500 milliseconds

$Timer = [System.Windows.Threading.DispatcherTimer]::new()
$Timer.Interval = [timespan]::FromMilliseconds(500)
# in the Tick event, toggle the colors
$Timer.Add_Tick({ 
    # find the button control
    $button = $Window.FindName('FlashingButton')
    if ($button.Content.Background -eq '#00A387') {
        $button.Content.Background = '#D8D8D8'
        $button.Content.Foreground = 'black'
    }
    else {
        $button.Content.Background = '#00A387'
        $button.Content.Foreground = 'white'
    }
})
$Timer.Start()

When all done, stop the timer

$Timer.Stop()

To not have to find the button every time the Tick event fires, store a reference to it earlier and inside the Tick({..}) you use it with the $script: scope:

# store a reference to the button in a variable
$FlashButton = $Window.FindName('FlashingButton')
# in the Tick event, toggle the colors
$Timer.Add_Tick({ 
    if ($script:FlashButton.Content.Background -eq '#00A387') {
        $script:FlashButton.Content.Background = '#D8D8D8'
        $script:FlashButton.Content.Foreground = 'black'
    }
    else {
        $script:FlashButton.Content.Background = '#00A387'
        $script:FlashButton.Content.Foreground = 'white'
    }
})

CodePudding user response:

If you want change without user interaction, you can try to use thread like Start-Job

  • Related