Search code examples
powershellxaml

How do I add a move function my my powershell form. The default windows style is disabled so I can't drag with the top bar


I'm trying to add a way to move around my powershell window by clicking on the top bar. Everything is linked to a xaml gui so I'm having issues getting the move function to link and actually work.

So far I've mainly tried chatGPT solutions but nothing so far has worked. Have not found many resources online for the linking of powershell to a xaml gui but everything else has worked so far.

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore

# Load the XAML definition
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="487"
        x:Name="MainWindow"
        WindowStyle="None"
        ResizeMode="NoResize"
        Background="#181735">
    <Grid>

    <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
       
        <Grid x:Name="Top_Bar" Background="#0f0f2d"
              Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

          <Button x:Name="BuTClose" FontFamily="arial" Content="X" Margin="204,0,0,0" VerticalAlignment="Center" Height="25" Width="25" FontSize="16" Grid.Column="1">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="TextElement.FontFamily" Value="Arial"/>
                    <Setter Property="Background" Value="#fe6584"/>
                    <Setter Property="Cursor" Value="Hand"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FF4c70"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border Width="20" Height="20"
                        CornerRadius="12"
                        Background="{TemplateBinding Background}">
                            <ContentPresenter VerticalAlignment="Center"
                                      HorizontalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>
        
        <ListBox x:Name="AccountListBox" Background="#0f0f2d" Margin="152,64,151,0" Height="323" VerticalAlignment="Top" Grid.Row="1"/>
        
        <TextBlock Text="Clean AD Unlock"
                    Foreground="White"
                    Grid.Column="0"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    FontFamily="Arial"
                   FontWeight="SemiBold"/>
   
        <Button x:Name="PopulateButton" Content="Check"
                    Foreground="White"
                    FontSize="14"
                    FontFamily="arial"
                    FontWeight="Medium" Margin="-97,64,239,336" Grid.Row="1">
            <Button.Style>

                <Style TargetType="Button">
                    <Setter Property="TextElement.FontFamily" Value="Arial"/>
                    <Setter Property="Background" Value="#fe6584"/>
                    <Setter Property="Cursor" Value="Hand"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FF4c70"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border Width="100" Height="25"
                        CornerRadius="12"
                        Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center"
                                      HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
        <Button x:Name="UnlockButton" Content="Unlock"
                    Foreground="White"
                    FontSize="14"
                    FontFamily="arial"
                    FontWeight="Medium" Margin="-97,118,239,282" Grid.Row="1">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="TextElement.FontFamily" Value="Arial"/>
                    <Setter Property="Background" Value="#fe6584"/>
                    <Setter Property="Cursor" Value="Hand"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FF4c70"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border Width="100" Height="25"
                        CornerRadius="12"
                        Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center"
                                      HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>

         <Rectangle HorizontalAlignment="Left" Height="190" Margin="-2,234,0,0" Grid.Row="1" VerticalAlignment="Top" Width="30" RenderTransformOrigin="0.5,0.5" RadiusX="15" RadiusY="15" Fill="#FF39FF14" Opacity="0.75">
            <Rectangle.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="46.572"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Rectangle.RenderTransform>
        </Rectangle>

        <Rectangle HorizontalAlignment="Left" Height="207" Margin="456,23,0,0" VerticalAlignment="Top" Width="30" RenderTransformOrigin="0.5,0.5" RadiusX="15" RadiusY="15" Fill="#FF39FF14" Grid.Row="1" Opacity="0.75">
            <Rectangle.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="46.572"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Rectangle.RenderTransform>
        </Rectangle>

        <Rectangle HorizontalAlignment="Left" Height="182" Margin="452,-12,0,0" VerticalAlignment="Top" Width="30" RenderTransformOrigin="0.5,0.5" RadiusX="15" RadiusY="15" Fill="#FFFF10F0" Grid.RowSpan="2" Opacity="0.75">
            <Rectangle.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="46.572"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Rectangle.RenderTransform>
        </Rectangle>

        <Rectangle HorizontalAlignment="Left" Height="300" Margin="-47,303,0,0" VerticalAlignment="Top" Width="30" RenderTransformOrigin="0.5,0.5" RadiusX="15" RadiusY="15" Fill="#FFFF10F0" Grid.Row="1" Opacity="0.75">
            <Rectangle.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="46.572"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Rectangle.RenderTransform>
        </Rectangle>
    </Grid>
</Window>
"@

# Create a PowerShell XML reader and a XAML form object
$reader = [System.Xml.XmlReader]::Create([System.IO.StringReader] $xaml)
$form = [Windows.Markup.XamlReader]::Load($reader)

# Retrieve the controls from the form
$accountListBox = $form.FindName("AccountListBox")
$populateButton = $form.FindName("PopulateButton")
$unlockButton = $form.FindName("UnlockButton")
$BuTClose = $form.FindName("BuTClose")

# Add an event handler for the Populate Button
$populateButton.Add_Click({
    #$lockedAccounts = Get-ADUser -Filter {Enabled -eq $false} -SearchBase "OU=Stenger Users,DC=stengerlaw,DC=com" -SearchScope Subtree
    $lockedAccounts = search-adaccount -lockedout -server dc | select SamAccountName, Name
    $accountListBox.Items.Clear()
    foreach ($account in $lockedAccounts) {
        $accountListBox.Items.Add($account.SamAccountName)
    }
})

# Add an event handler for the Unlock Button
$unlockButton.Add_Click({
    $selectedAccount = $accountListBox.SelectedItem
    if ($selectedAccount) {
        $adminCreds = Get-Credential -Message "Enter your admin credentials"
        try {
            $adAccount = Get-ADUser -Identity $selectedAccount
            if ($adAccount) {
                Unlock-ADAccount -Identity $adAccount -Credential $adminCreds
                [System.Windows.MessageBox]::Show("The account '$selectedAccount' has been unlocked.", "Account Unlocked", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Information)
            } else {
                [System.Windows.MessageBox]::Show("The account '$selectedAccount' was not found.", "Account Not Found", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
            }
        } catch {
            [System.Windows.MessageBox]::Show("An error occurred while unlocking the account. Error: $($_.Exception.Message)", "Error", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
        }
    } else {
        [System.Windows.MessageBox]::Show("No account selected.", "No Account Selected", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
    }
})
$BuTClose.Add_Click( {
        $form.Close()
    }
)

# Retrieve the controls from the form
$Form = $form.FindName("MainWindow")









$form.ShowDialog() | Out-Null

Solution

  • Call $Form.DragMove() from a MouseDown handler on the desired control:

    $topbar = $form.FindName("Top_Bar")
    $topbar.add_MouseDown({
        param($s,$e)
    
        if ($e.ChangedButton -eq [System.Windows.Input.MouseButton]::Left){
            $Form.DragMove()
        }
    })