IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

Programme avec plusieurs fenêtres, comment masquer une fenêtre ? [PowerShell]


Sujet :

Scripts/Batch

  1. #1
    Membre averti Avatar de florian7
    Homme Profil pro
    Apprenti
    Inscrit en
    Août 2015
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Apprenti
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Août 2015
    Messages : 35
    Par défaut Programme avec plusieurs fenêtres, comment masquer une fenêtre ?
    Bonjour à tous,

    C'est un programme pour créer des comptes AD, et il y a trois fenêtres à faire apparaître.
    Comment faire pour masquer la première et faire apparaître la seconde par exemple ?

    Sur le web j'ai trouvé plusieurs fonctions show-powershell (pour afficher) et Hide-Powershell (pour masquer) mais sans succès.

    Code de la fonction et aussi en lancement il y a des erreurs :

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    Function Invoke-Win32 ( [string] $dllName, 
                            [Type] $returnType,
                            [string] $methodName, 
                            [Type[]] $parameterTypes, 
                            [Object[]] $parameters) {
     
        #Fonction récupéré sur http://powershell-scripting.com
        ## Begin to build the dynamic assembly
        $domain = [AppDomain]::CurrentDomain
        $name = New-Object Reflection.AssemblyName 'PInvokeAssembly'
        $assembly = $domain.DefineDynamicAssembly($name, 'Run')
        $module = $assembly.DefineDynamicModule('PInvokeModule')
        $type = $module.DefineType('PInvokeType', "Public,BeforeFieldInit")
     
        # Go through all of the parameters passed to us.  As we do this,
        # we clone the user's inputs into another array that we will use for
        # the P/Invoke call.
        $inputParameters = @()
        $refParameters = @()
     
        for($counter = 1 ; $counter -le $parameterTypes.Length ; $counter++) {
            # If an item is a PSReference, then the user
            # wants an [out] parameter.
            if ($parameterTypes[$counter - 1] -eq [Ref]) {
                # Remember which parameters are used for [Out] parameters
                $refParameters += $counter
     
                # On the cloned array, we replace the PSReference type with the
                # .Net reference type that represents the value of the PSReference,
                # and the value with the value held by the PSReference.
                $parameterTypes[$counter - 1] = $parameters[$counter - 1].Value.GetType().MakeByRefType()
                $inputParameters += $parameters[$counter - 1].Value
            } else {
                # Otherwise, just add their actual parameter to the input array.
                $inputParameters += $parameters[$counter - 1]
            }
        }
     
        # Define the actual P/Invoke method, adding the [Out] attribute for any parameters that were 
        # originally [Ref] parameters.
        $method = $type.DefineMethod($methodName, 'Public,HideBySig,Static,PinvokeImpl', $returnType, $parameterTypes)
        foreach ($refParameter in $refParameters) {
            $method.DefineParameter($refParameter, "Out", $null)
        }
     
        # Apply the P/Invoke constructor
        $ctor = [Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
        $attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, $dllName
        $method.SetCustomAttribute($attr)
     
        #Create the temporary type, and invoke the method.
        $realType = $type.CreateType()
        $realType.InvokeMember($methodName, 'Public,Static,InvokeMethod', $null, $null, $inputParameters)
     
        # Finally, go through all of the reference parameters, and update the values of the PSReference 
    objects that the user passed in.
        foreach ($refParameter in $refParameters) {
            $parameters[$refParameter - 1].Value = $inputParameters[$refParameter - 1]
        }
    }
     
    Function Hide-PowerShell() { 
        $global:myWindowHandle = (Get-Process -Id $pid).MainWindowHandle
        $null = ShowWindowAsync $((Get-Process -Id $pid).MainWindowHandle)  0
    }
     
     
     Function Show-PowerShell() { 
         $null = ShowWindowAsync $($global:myWindowHandle)  1
    }
     
    Function ShowWindowAsync([IntPtr] $hWnd, [Int32] $nCmdShow)
    {
        #0 Cache la fenetre
        #1 Montre la fenetre
        #2 Minimize la fenetre
        $parameterTypes = [IntPtr], [Int32] 
        $parameters = $hWnd, $nCmdShow
     
        Invoke-Win32 "user32.dll" ([Boolean]) "ShowWindowAsync" $parameterTypes $parameters
        }

    J'ai trouvé un moyen d'afficher les deux fenêtres et de les fermer en même temps quand je clique sur le bouton suivant de l'interface.

    Avec ça :

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $Suivant.Add_Click({
    if ($Ext.IsChecked){
     }
     
    if ($In.IsChecked){
     
    $Form.ShowDialog()
    $Intro.visibility=1
     }             
    })

    Voila si quelqu'un a une idée merci d'avance.

  2. #2
    Membre éprouvé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juillet 2015
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juillet 2015
    Messages : 66
    Par défaut
    Comment sont créées les fenêtres, s'agit-il de formes?

    Imaginons deux formes: $form et $form2

    elles ont toutes les deux un bouton: OK

    Si on clic sur OK, la première forme disparait, et la deuxième forme apparait. Si on clic sur OK de la deuxième forme, elle disparait, et la première réapparait.

    Il y a plusieurs façons de faire disparaitre une forme:

    - Méthode $form.Dispose(), libère l'objet et la forme ferme, c'est définitif, il faudra recréer la forme. C'est pas ce qu'on veut.
    - Methode $form.Close(), ferme la forme de façon définitive et comme le programme généralement se base sur la $form, en fait ça ferme tout. Pas non plus ce qu'on veut.

    - Propriété $form.Visible = $false, ne ferme pas la forme mais la rend invisible. Pour ensuite faire réapparaitre une forme, il suffit de faire: $form.visible = $true

    Voici un exemple:

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
     
    $ButtonOK_Click = {
    	$form.Visible = $false
     
    	$form2.ShowDialog()
    }
    $ButtonOK2_Click = {
    	$form2.Visible = $false
     
    	$form.Visible = $true
    }
    $form = New-Object System.Windows.Forms.Form
    $form2 = New-Object System.Windows.Forms.Form
    $Form.ClientSize = '342, 502'
    $Form2.ClientSize = '342, 502'
     
    $ButtonOK = New-Object System.Windows.Forms.Button
    $ButtonOK.Location = '245,467'
    $ButtonOK.Size = '75,23'
    $ButtonOK.Text = 'OK'
    $ButtonOK.add_Click($ButtonOK_Click)
    $form.Controls.Add($ButtonOK)
     
    $ButtonOK2 = New-Object System.Windows.Forms.Button
    $ButtonOK2.Location = '245,467'
    $ButtonOK2.Size = '75,23'
    $ButtonOK2.Text = 'OK'
    $ButtonOK2.add_Click($ButtonOK2_Click)
    $form2.Controls.Add($ButtonOK2)
     
    # Rock & roll
    $form.ShowDialog()

    PS. Utiliser le bouton Close en haut à droite de la forme créée pour sortir du programme, car en cliquant sur OK on ne sort pas

  3. #3
    Membre averti Avatar de florian7
    Homme Profil pro
    Apprenti
    Inscrit en
    Août 2015
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Apprenti
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Août 2015
    Messages : 35
    Par défaut
    C'est des form's xaml, merci beaucoup pour ton aide c'est vraiment top

    Passe une bonne journée,

  4. #4
    Membre averti Avatar de florian7
    Homme Profil pro
    Apprenti
    Inscrit en
    Août 2015
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Apprenti
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Août 2015
    Messages : 35
    Par défaut
    Comme c'est des form's xaml ça marche pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $Suivant.Add_Click({
    if ($Ext.IsChecked){
     }
    
    if ($In.IsChecked){
    
    $Intro.Visible = $false
    $Form.ShowDialog()
    
     }             
    })
    Message d'erreur :

    La propriété «*Visible*» est introuvable dans cet objet. Vérifiez qu’elle existe et qu’elle peut être définie.
    Au caractère C:\Users\mtb247428\Downloads\Set-WindowStyle.ps1:55 : 1
    + $Intro.Visible = $false
    + ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation : ( [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    Si tu as une idée

  5. #5
    Membre éprouvé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juillet 2015
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juillet 2015
    Messages : 66
    Par défaut
    Tu peux poster un exemple de création de tes formes XAML?

    Merci

  6. #6
    Membre averti Avatar de florian7
    Homme Profil pro
    Apprenti
    Inscrit en
    Août 2015
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Apprenti
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Août 2015
    Messages : 35
    Par défaut
    Voila un exemple

    Merci de ton aide.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    [xml] $xaml = @'
    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            Title="Types de contrat :" Icon="C:\Users\mtb247428\Desktop\Powershell_Projet_Fabrice\IMAGES_PROJET\active-directory.png" Height="426" Width="504">
        <Grid Background="#FF1D3245" HorizontalAlignment="Left" Width="499">
            <Image Margin="725,377,73,10" Source="C:\bde92b4f9b3b8b869e8095e6af1d6724.png" Stretch="Fill"/>
            <CheckBox x:Name="In" Content="Interne" HorizontalAlignment="Left" Margin="146,94,0,0" VerticalAlignment="Top" Foreground="White"/>
            <TextBlock HorizontalAlignment="Left" Margin="105,53,0,0" TextWrapping="Wrap" Text="Sélectionner le types de contrat du compte à créer :" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="CDI" Content="CDI" HorizontalAlignment="Left" Margin="146,125,0,0" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="CDD" Content="CDD" HorizontalAlignment="Left" Margin="146,146,0,0" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="Appr" Content="Apprenti" HorizontalAlignment="Left" Margin="146,167,0,0" VerticalAlignment="Top" Foreground="White"/>
            <CheckBox x:Name="Ext" Content="Externe" HorizontalAlignment="Left" Margin="268,94,0,0" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="Stag" Content="Stagiaire" HorizontalAlignment="Left" Margin="146,188,0,0" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="Int" Content="Intérimaire" HorizontalAlignment="Left" Margin="268,125,0,0" VerticalAlignment="Top" Foreground="White"/>
            <RadioButton x:Name="Pres" Content="Prestataire" HorizontalAlignment="Left" Margin="268,149,0,0" VerticalAlignment="Top" Foreground="White"/>
            <Separator HorizontalAlignment="Left" Height="34" Margin="105,55,0,0" VerticalAlignment="Top" Width="273"/>
            <Button x:Name="Suivant" Content="Suivant" HorizontalAlignment="Left" Margin="205,338,0,0" VerticalAlignment="Top" Width="75"/>
            <TextBlock HorizontalAlignment="Left" Margin="146,258,0,0" TextWrapping="Wrap" Text="Adresse Email :" VerticalAlignment="Top" Foreground="White"/>
            <GroupBox Header="Messier-Buttagi-Dowty" HorizontalAlignment="Left" Margin="96,30,0,0" VerticalAlignment="Top" Height="195" Width="294" RenderTransformOrigin="0.5,0.5" Foreground="White"/>
            <GroupBox Header="Demandeur" HorizontalAlignment="Left" Margin="135,238,0,0" VerticalAlignment="Top" Height="75" Width="225" Foreground="White">
            <TextBox HorizontalAlignment="Left" Height="23" Margin="0,22,0,0" TextWrapping="Wrap" Text="@safranmbd.com" VerticalAlignment="Top" Width="210"/>
            </GroupBox>
        </Grid>
    </Window>
    '@
    
    $reader = New-Object system.xml.xmlnodereader $xaml
    $Intro = [windows.markup.xamlreader]::Load($reader)
    
    #########################################################################
    #            # Déclaration des variables de l'interface graphique #     #
    #########################################################################
    
    $In = $Intro.Findname('In')
    $CDI = $Intro.Findname('CDI')
    $CDD = $Intro.Findname('CDD')
    $Appr = $Intro.Findname('Appr')
    $Ext = $Intro.Findname('Ext')
    $Stag = $Intro.Findname('Stag')
    $Int = $Intro.Findname('Int')
    $Pres = $Intro.Findname('Pres')
    $Suivant = $Intro.Findname('Suivant')
    $Demandeur = $Intro.FindName('demandeur')

  7. #7
    Membre éprouvé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juillet 2015
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juillet 2015
    Messages : 66
    Par défaut
    OK, il s'agit de Windows, des vrais avec des handles et pas de formes.

    Donc la propriété Visible n'existe pas dans ces objets. Et ca devient un peu plus compliqué.

    Il y a un excellent exemple ici : http://stackoverflow.com/questions/1...powershell-ise

    En gros l'auteur définit deux fonctions qui se trouvent dans user32.dll: FindWindow et ShowWindow

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    $definition = @"    
          [DllImport("user32.dll")]
          static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
          public static void Show(string wClass, string wName)
          {
             IntPtr hwnd = FindWindow(wClass, wName);
             if ((int)hwnd > 0)
                ShowWindow(hwnd, 1);
          }
    
          public static void Hide(string wClass, string wName)
          {
             IntPtr hwnd = FindWindow(wClass, wName);
             if ((int)hwnd > 0)
                ShowWindow(hwnd, 0);
          }
    "@
    
    add-type -MemberDefinition $definition -Namespace my -Name WinApi
    Ensuite il appelle FindWindow pour récuperer l'identifiant (le handle) de la fenetre qu'il faut cacher. Et finalement la cache avec ShowWindow.

    Pour simplifier tout ça l'auteur a créé une fonction Hide qu'on peut appeler tout simplement, en passant soit la classe de fenetre (par exemple Notepad), soit son titre.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [my.WinApi]::Hide('Notepad', 'Untitled - Notepad')

    Dans ton cas il faut l'adapter avec peut-être:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [my.WinApi]::Hide($null, 'Types de contrat :')
    Dis nous si ça aide

  8. #8
    Membre averti Avatar de florian7
    Homme Profil pro
    Apprenti
    Inscrit en
    Août 2015
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Apprenti
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Août 2015
    Messages : 35
    Par défaut
    C'est bon, merci beaucoup pour ton aide !!!

    je reviendrais vers toi surement si ça te dérange pas

    Bon week-end a toi,

  9. #9
    Membre éprouvé
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Juillet 2015
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Juillet 2015
    Messages : 66
    Par défaut
    Ben de rien, et bien sur que ça ne dérange personne.

    Bon WE.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 6
    Dernier message: 03/01/2011, 23h00
  2. Comment fermer une fenêtre père via sa fenêtre fille ?
    Par kamikazrider dans le forum Windows Forms
    Réponses: 6
    Dernier message: 26/11/2010, 14h05
  3. Réponses: 1
    Dernier message: 21/12/2007, 21h27
  4. Comment fermer une fenêtre Child dans une application MDI
    Par bahiatoon dans le forum C++Builder
    Réponses: 9
    Dernier message: 05/10/2006, 08h57
  5. Réponses: 20
    Dernier message: 10/05/2006, 11h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo