Advertisement
Yevrag35

ExampleGUI

Apr 6th, 2020
1,518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName PresentationFramework
  2. Add-Type -AssemblyName System.Windows.Forms
  3. Add-Type -AssemblyName WindowsBase
  4. Add-Type -AssemblyName PresentationCore
  5.  
  6. # Build the XAML hashtable for everything
  7. $uiHash = [hashtable]::Synchronized(@{})
  8.  
  9. #region The XAML Code
  10. [xml]$xaml = @'
  11. <Window x:Class="ExampleGUI"
  12.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  13.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  14.        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  15.        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  16.        xmlns:local="clr-namespace:ExampleGUI"
  17.        DataContext="{Binding RelativeSource={RelativeSource Self}}"
  18.        mc:Ignorable="d"
  19.        Title="Example GUI" Height="450" Width="800">
  20.    <Window.Background>
  21.        <LinearGradientBrush StartPoint='0,0' EndPoint='0,1'>
  22.            <LinearGradientBrush.GradientStops>
  23.                <GradientStop Color='#C4CBD8' Offset='0' />
  24.                 <GradientStop Color='#E6EAF5' Offset='0.2' />
  25.                 <GradientStop Color='#CFD7E2' Offset='0.9' />
  26.                 <GradientStop Color='#C4CBD8' Offset='1' />
  27.             </LinearGradientBrush.GradientStops>
  28.         </LinearGradientBrush>
  29.     </Window.Background>
  30.    <Window.Resources>
  31.         <!--Control colors.-->
  32.         <Color x:Key="ControlNormalColor">#000000</Color>
  33.         <Color x:Key="ControlMouseOverColor">#5E5E5E</Color>
  34.         <Color x:Key="DisabledControlColor">#FFF2F2F2</Color>
  35.         <Color x:Key="DisabledForegroundColor">#909098</Color>
  36.         <Color x:Key="ControlPressedColor">#878787</Color>
  37.  
  38.         <Style x:Key="ButtonFocusVisual">
  39.             <Setter Property="Control.Template">
  40.                 <Setter.Value>
  41.                     <ControlTemplate>
  42.                         <Border>
  43.                             <Rectangle Margin="2" StrokeThickness="1" Stroke="#60000000" StrokeDashArray="1 2" />
  44.                         </Border>
  45.                     </ControlTemplate>
  46.                 </Setter.Value>
  47.             </Setter>
  48.         </Style>
  49.  
  50.         <!-- Button -->
  51.         <Style TargetType="Button">
  52.             <Setter Property="SnapsToDevicePixels" Value="true" />
  53.             <Setter Property="OverridesDefaultStyle" Value="False" />
  54.             <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}" />
  55.             <!--<Setter Property="MinHeight" Value="29px" />
  56.             <Setter Property="MinWidth"  Value="103px" />-->
  57.             <Setter Property="Foreground" Value="#FFFFFFFF" />
  58.             <Setter Property="Template">
  59.                 <Setter.Value>
  60.                     <ControlTemplate TargetType="Button">
  61.                         <Border TextBlock.Foreground="{TemplateBinding Foreground}" x:Name="Border">
  62.                             <Border.Background>
  63.                                 <SolidColorBrush  Color="{DynamicResource ControlNormalColor}" />
  64.                             </Border.Background>
  65.                             <VisualStateManager.VisualStateGroups>
  66.                                 <VisualStateGroup x:Name="CommonStates">
  67.                                     <VisualStateGroup.Transitions>
  68.                                         <VisualTransition GeneratedDuration="0:0:0.5" />
  69.                                         <VisualTransition GeneratedDuration="0" To="Pressed" />
  70.                                     </VisualStateGroup.Transitions>
  71.                                     <VisualState x:Name="Normal" />
  72.                                     <VisualState x:Name="MouseOver">
  73.                                         <Storyboard>
  74.                                             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
  75.                                                 Storyboard.TargetName="Border">
  76.                                                 <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
  77.                                             </ColorAnimationUsingKeyFrames>
  78.                                         </Storyboard>
  79.                                     </VisualState>
  80.                                     <VisualState x:Name="Pressed">
  81.                                         <Storyboard>
  82.                                             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
  83.                                                 Storyboard.TargetName="Border">
  84.                                                 <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColor}" />
  85.                                             </ColorAnimationUsingKeyFrames>
  86.                                         </Storyboard>
  87.                                     </VisualState>
  88.                                     <VisualState x:Name="Disabled">
  89.                                         <Storyboard>
  90.                                             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
  91.                                                 Storyboard.TargetName="Border">
  92.                                                 <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlColor}" />
  93.                                             </ColorAnimationUsingKeyFrames>
  94.                                             <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
  95.                                                 Storyboard.TargetName="Border">
  96.                                                 <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" />
  97.                                             </ColorAnimationUsingKeyFrames>
  98.                                         </Storyboard>
  99.                                     </VisualState>
  100.                                 </VisualStateGroup>
  101.                             </VisualStateManager.VisualStateGroups>
  102.                             <ContentPresenter Margin="2"
  103.                             HorizontalAlignment="Center"
  104.                             VerticalAlignment="Center"
  105.                             RecognizesAccessKey="True" />
  106.                         </Border>
  107.                     </ControlTemplate>
  108.                 </Setter.Value>
  109.             </Setter>
  110.         </Style>
  111.     </Window.Resources>
  112.     <Grid>
  113.         <Grid.ColumnDefinitions>
  114.             <ColumnDefinition Width="100" />
  115.             <ColumnDefinition />
  116.         </Grid.ColumnDefinitions>
  117.         <TextBox x:Name="CodeBlock" Grid.Column="1"
  118.                  VerticalAlignment="Top" HorizontalAlignment="Left"
  119.                  TextWrapping="NoWrap" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"/>
  120.         <Button Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100"
  121.                 x:Name="ResetBtn" Height="40" IsEnabled="False"/>
  122.  
  123.     </Grid>
  124. </Window>
  125. '@ -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'
  126. $reader = New-Object System.Xml.XmlNodeReader -ArgumentList $xaml
  127. $uiHash.Window = [System.Windows.Markup.XamlReader]::Load($reader)
  128.  
  129. $xaml.SelectNodes("//*[@Name]") | ?{$_.Name -notlike "*lbl" -and $_.Name -ne "label" -and $_.Name -notlike "*Color*"} | %{
  130.    $uiHash.Add($_.Name,$uiHash.Window.FindName($_.Name))
  131. }
  132. #endregion
  133.  
  134. $uiHash.NotSet = $true
  135.  
  136. # Event for clicking the Reset Button
  137. $uiHash.ResetBtn.Add_Click({
  138.    $uiHash.NotSet = $true
  139.    $uiHash.CodeBlock.Text = [string]::Empty
  140.    $uiHash.ResetBtn.IsEnabled = $false
  141. })
  142.  
  143. # Event for when the mouse enters the text box
  144. $uiHash.CodeBlock.Add_MouseEnter({
  145.    if ($uiHash.NotSet)
  146.    {
  147.        # Reads the Clipboard only if it's a file that's copied.
  148.        $file = Get-Clipboard -Format FileDropList -ErrorAction SilentlyContinue
  149.        if ($null -ne $file)
  150.        {
  151.            # If it's a file, then we try to the read the text contents
  152.             $text = Get-Content -Path $file.FullName -Raw -ErrorAction SilentlyContinue
  153.             if (-not [string]::IsNullOrEmpty($text))
  154.             {
  155.                 # If there's content read, then we populate the textbox with it.
  156.                 $uiHash.CodeBlock.Text = $text
  157.                 $uiHash.NotSet = $false
  158.                 $uiHash.ResetBtn.IsEnabled = $true  # Enable the Reset Button so the user do this again.
  159.             }
  160.         }
  161.     }
  162. })
  163.  
  164. $uiHash.Window.ShowDialog() > $null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement