0

I am creating a GUI in powershell with winform. The interface is simple: enter image description here

But i have a bug when i click on my button, the font and the form change : enter image description here

I don't understand why, i comment the font code but i have the same problem ... I want to fix the size of all fonts

I attach the code that I use to create the GUI

### MAIN ###

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName PresentationCore,PresentationFramework

$Icon = New-Object system.drawing.icon (".\icone.ico")

$User = $env:username
#$Pwshll_version = $PsVersionTable.PSVersion.Major

$Form = New-Object System.Windows.Forms.Form

$Form.ClientSize = '400, 470'
#$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$Form.Text = "TEST-$User"
$Form.Icon = $Icon
#$Form.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Segoe UI',[System.Single]10,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$Form.AutoScaleMode = None

#TITRE
$Titre = New-Object System.Windows.Forms.Label
$Titre.Location = New-Object System.Drawing.Point(90,10)
$Titre.AutoSize     = $true
$Titre.Text = "COMMUNICATION"
#$Titre.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Segoe UI',[System.Single]12,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))

#EMPLACEMENT D'ECRITURE DU MESSAGE
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Point(10,230)
$TextBox.Multiline = $true
$TextBox.Width = 380
$TextBox.height = 150
#$TextBox.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Segoe UI',[System.Single]11,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))

$Label_error = New-Object System.Windows.Forms.Label
$Label_error.Location = New-Object System.Drawing.Point(30,390)
$Label_error.AutoSize     = $true
#$Label_error.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Segoe UI',[System.Single]9,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$Label_error.Text = ""

$Label = New-Object System.Windows.Forms.Label

#Toutes les checkboxs
$AllCheckBox                      = New-Object system.Windows.Forms.CheckBox
$AllCheckBox.text                 = "Test"
$AllCheckBox.AutoSize             = $true
$AllCheckBox.width                = 120
$AllCheckBox.height               = 25
$AllCheckBox.location             = New-Object System.Drawing.Point(115,50)
$AllCheckBox.Add_Click({
    If ($AllCheckBox.Checked -eq $true){
        $ROCheckBox.Checked = $false
        $LRCheckBox.Checked = $false
        $OLCheckBox.checked = $false
        $MACheckbox.Checked = $false
        $Label.Text = "ALL"
    }
})

$LRCheckBox                      = New-Object system.Windows.Forms.CheckBox
$LRCheckBox.text                 = "LR"
$LRCheckBox.AutoSize             = $true
$LRCheckBox.width                = 120
$LRCheckBox.height               = 25
$LRCheckBox.location             = New-Object System.Drawing.Point(115,80)
$LRCheckBox.Add_Click({
    If ($LRCheckBox.Checked -eq $true){
        $ROCheckBox.Checked = $false
        $AllCheckbox.Checked = $false
        $OLCheckBox.checked = $false
        $MACheckbox.Checked = $false
        $Label.Text = "LR"
    }
})

$ROCheckBox                      = New-Object system.Windows.Forms.CheckBox
$ROCheckBox.text                 = "Ro"
$ROCheckBox.AutoSize             = $true
$ROCheckBox.width                = 120
$ROCheckBox.height               = 25
$ROCheckBox.location             = New-Object System.Drawing.Point(115,110)
$ROCheckBox.Add_Click({
    If ($ROCheckBox.Checked -eq $true){
        $LRCheckBox.checked = $false
        $AllCheckbox.Checked = $false
        $OLCheckBox.checked = $false
        $MACheckbox.Checked = $false
        $Label.Text = "RO"
    }
})

$OLCheckBox                      = New-Object system.Windows.Forms.CheckBox
$OLCheckBox.text                 = "Ol"
$OLCheckBox.AutoSize             = $true
$OLCheckBox.width                = 120
$OLCheckBox.height               = 25
$OLCheckBox.location             = New-Object System.Drawing.Point(115,140)
$OLCheckBox.Add_Click({
    If ($OLCheckBox.Checked -eq $true){
        $LRCheckBox.checked = $false
        $AllCheckbox.Checked = $false
        $ROCheckbox.Checked = $false
        $MACheckbox.Checked = $false
        $Label.Text = "OL"
    }
})

$MACheckBox                      = New-Object system.Windows.Forms.CheckBox
$MACheckBox.text                 = "Ma"
$MACheckBox.AutoSize             = $true
$MACheckBox.width                = 120
$MACheckBox.height               = 25
$MACheckBox.location             = New-Object System.Drawing.Point(115,170)
$MACheckBox.Add_Click({
    If ($MACheckBox.Checked -eq $true){
        $LRCheckBox.checked = $false
        $AllCheckbox.Checked = $false
        $ROCheckbox.Checked = $false
        $OLCheckbox.Checked = $false
        $Label.Text = "MA"
    }
})

$Bouton = New-Object System.Windows.Forms.Button
$Bouton.Location = New-Object System.Drawing.Point(310,430)
$Bouton.Width = 80
$Bouton.Height = 30
#$Bouton.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Bouton.Text = "Envoyer"

$Label_en_cours = New-Object System.Windows.Forms.Label
$Label_en_cours.Location = New-Object System.Drawing.Point(175,420)
$Label_en_cours.AutoSize     = $true

$count = 0

$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Visible = $false
$ProgressBar.Minimum = 0
$ProgressBar.Location = new-object System.Drawing.Size(30,395)
$ProgressBar.size = new-object System.Drawing.Size(340,25)

$Form.controls.AddRange(@($Titre,$Textbox,$AllCheckbox,$LRCheckBox,$OLCheckBox,$MACheckBox,$ROCheckBox,$Bouton,$Label_en_cours,$Label_error,$ProgressBar))

if ($User -notlike "sup*"){
    [System.Windows.MessageBox]::Show("Veuillez lancer l'application avec un autre utilisateur !", "Mauvais utilisateur", "OK", "Error")
    $Bouton.Enabled = $false
    $TextBox.Enabled = $false
    $Label_error.Text = "Lancer l'application avec un utilisateur SUP !"
}



$Bouton.Add_Click({
    if ($AllCheckBox.Checked -eq $false -And $LRCheckBox.Checked -eq $false -And $ROCheckBox.Checked -eq $false -And $OLCheckBox.Checked -eq $false -And $MACheckBox.Checked -eq $false ){
        [System.Windows.MessageBox]::Show("Aucune case cochée", "Erreur", "OK", "Error")
        return
    }

    if ($TextBox.Text -eq "")
    {
        [System.Windows.MessageBox]::Show("Vous n'avez pas renseignez de message !", "Champ vide", "OK", "Error")
        return
    }

    Get-job | Remove-Job -Force
    $msgBoxInput =  [System.Windows.MessageBox]::Show("Vérifier le texte avant l'envoie. Voulez-vous envoyer la communication?",'Envoyer la communication','YesNoCancel','Error')

    switch  ($msgBoxInput)
    {

        'Yes'
        {
            $ProgressBar.Visible = $true
            $form.Cursor = 'WaitCursor'
            Send_MSG
            $ProgressBar.Value = 0
            $Label_en_cours.Text = "TERMINE"
            $form.Cursor = 'Default'
            Start-Sleep 2
            $Label_en_cours.Text = ""
            $ProgressBar.Visible = $false
        }
    }
})

$Form.ShowDialog()
Tazounet
  • 41
  • 6
  • 3
    You're referencing and using WPF assemblies mixed with WinForms'. Totally *legal*, but keep in mind that WinForms windows become DpiAware (if they're not already) as soon as *WPF stuff* appears. Not clear why you're showing a WPF MessageBox instead of the WinForm's -- See the notes here: [DPI Awareness - Unaware in one Release, System Aware in the Other](https://stackoverflow.com/a/50276714/7444103), in case you're interested – Jimi Aug 14 '23 at 10:14
  • You can see it's not *a bug*, it's standard, foreseen, behavior – Jimi Aug 14 '23 at 10:24
  • I didn't realize I was using a message Box WPF. This is a misunderstanding, if I understand correctly as soon as I display a messagebox all the fonts change. I am therefore not using the correct syntax to display the message, So I have to use message box winform ? Is that right? – Tazounet Aug 14 '23 at 11:50
  • You have three (main) choices: Make the WinForms part DpiAware, make the Process forcibly non-DpiAware or eliminate `Add-Type -AssemblyName PresentationCore,PresentationFramework` entirely and use the `System.Windows.Forms.MessageBox` only (in case the WPF assemblies are not used for anything else, as it appears) – Jimi Aug 14 '23 at 11:56
  • Perfect, I removed "Add-Type -AssemblyName PresentationCore,PresentationFramework" to use only Winforms and it Work. Thank you guy – Tazounet Aug 14 '23 at 12:06

0 Answers0