Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

XL 2010 Userform Plein Ecran VBA7

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

cathodique

XLDnaute Barbatruc
Bonjour la communauté,

J'ai besoin de votre aide. Merci à celles et ceux qui ont Windows 10 64 bits et Excel 2019 64 bits de tester l'affichage en plein écran d'un userform.

La procédure ci-dessous avait été proposée, si mes souvenirs sont bons par @patricktoulon ,

je n'ai modifié que la partie haute mais ne fonctionne pas sous: Windows 10 64 bits et Excel 2019 64 bits.

Auriez-vous une solution à me proposer?
VB:
Option Explicit

#If VBA7 Then
    #If Win64 Then
        Public Declare PtrSafe Function SetWindowLongA Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        Public Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
        Public Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
        Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
    #Else
        Public Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        Public Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
        Public Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
        Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
    #End If
#Else
    ' Pour Excel 2007 ou antérieur (VBA6)
    Public Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#End If

Public Sub SameSizeApplication(Usf As Object)   'utilisé
   Dim ctl As Control, ratioW#, ratioH#, tbCw, i&

   With Application: ratioW = .UsableWidth / Usf.Width: ratioH = .Height / Usf.Height: End With
   Usf.Move 0, 0, Usf.Width * ratioW, Usf.Height * ratioH

   For Each ctl In Usf.Controls
      ctl.Move ctl.Left * ratioW, ctl.Top * ratioH, ctl.Width * ratioW, ctl.Height * ratioH
      On Error Resume Next
      ctl.Font.Size = Round(ctl.Font.Size * Application.Min(ratioH, ratioW))
      On Error GoTo 0

      If TypeName(ctl) = "ListBox" Or TypeOf ctl Is ListBox Then
         If ctl.ColumnWidths <> "" Then
            tbCw = Split(Replace(ctl.ColumnWidths, " pt", ""), ";")
            For i = LBound(tbCw) To UBound(tbCw): tbCw(i) = val(tbCw(i)) * ratioW: Next
            ctl.ColumnWidths = Join(tbCw, ";")
         End If
      End If
   Next
End Sub

Public Sub ShowFullScreenUserForm(Usf As Object)  ''ok'utilisé
   Dim hWnd As Long
   ' Ajuster les contrôles en fonction du nouveau format
   SameSizeApplication Usf

   hWnd = FindWindowA(vbNullString, Usf.Caption) 'plante ici
   ' Mettre le UserForm en plein écran sans barre de titre
   SetWindowLongA hWnd, -16, &H94080080
   '   ShowWindow hwnd, 3
End Sub
 

Pièces jointes

Solution
sinon
si tu veux un fullscreen fixe avec resize des controls tout totomatic avec showwindow sans déclaration d'api
ben tu le fait avec les macro4(activer les macro4 à partir de 2021 dans les options excel)
un module resize standard
VB:
'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //...
Bonjour.
À tout hasard essayez en laissant tout As Long au lieu de As LongPtr. En principe seules des infos destinées à être effectivement envoyées en intégralité sur les bus à 64bits du CPU doivent être impérativement As LongPtr. Or je crois que ce n'est peut être pas le cas des handles de fenêtres et sûrement pas celui des masques d'options ni des codes retour de bonne exécution renvoyés par les Function, qui n'ont à en renseigner que les 32bits de poids faibles.
De plus il y a une incohérence en ce sens que dans votre Sub ShowFullScreenUserForm vous avez déclaré HWnd As Long dans tous les cas alors qu'elle est obtenue par une Function pouvant avoir été déclarée (peut être à tort) comme retournant un LongPtr, c'est à dire un pseudo type équivalent à LonLong pour Win64.
 
Dernière édition:
Salut,
c'est normal que cela ne fonctionne pas. Le Hwnd est mal défini pour du VBA7 dans ta routine ShowFullScreenUserForm
Mettre ceci :
VB:
Public Sub ShowFullScreenUserForm(Usf As Object)  ''ok'utilisé
#If VBA7 Then
   Dim hWnd As LongPtr
#Else
   Dim hWnd As Long
#End If

Nullosse
 
Bonjour
@catodique
tu mélange les deux version à savoir:
  1. redimentionnement par raport à l'application
  2. redimentionnement en fullscreen a lors que tu a le showWindow
à moins que ton app excel ne soit pas fullscreen et que tu veux simplement la couvrir
 
sinon
si tu veux un fullscreen fixe avec resize des controls tout totomatic avec showwindow sans déclaration d'api
ben tu le fait avec les macro4(activer les macro4 à partir de 2021 dans les options excel)
un module resize standard
VB:
'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'                   Module fullscreen (fixe )pour userform et resize des controls au prorata  minimal
'Version 1.7
'auteur: patricktoulon
Option Explicit

Public oldW#
Public oldH#
Sub FullScreenU()
    Dim hwnd&
    hwnd = ExecuteExcel4Macro("CALL(""user32"",""GetActiveWindow"",""JCC"")") 'api GetActiveWindow
    ExecuteExcel4Macro ("CALL(""user32"",""ShowWindow"",""JJJ"",""" & hwnd & """,""" & 3 & """)") ' application du mode
End Sub

Sub Dimensions(usf As Object, Mode&)
    Dim Ctrl, Fz&, cw$, NW#, NH#, dimo, TW, Z&, Ecart#, I&
    Ecart = 0 '((USF.Width - USF.InsideWidth) * 2)
    If Mode = 1 Then
        oldW = usf.InsideWidth + Ecart: oldH = usf.InsideHeight + Ecart
        For Each Ctrl In usf.Controls
            With Ctrl
                .Tag = CDec(.Left) & "|" & .Top & "|" & .Width & "|" & CDec(.Height)
                On Error Resume Next
                Fz = .Font.Size
                If Not Err Then .Tag = .Tag & "|" & Fz Else .Tag = .Tag & "|"
                Err.Clear
                
                If TypeName(Ctrl) = "ListBox" Then
                    cw = Ctrl.ColumnWidths: If cw = "" Then cw = Application.Rept("70;", Ctrl.ColumnCount + 1)
                    .Tag = .Tag & "|" & cw
                End If
            End With
        Next
        On Error GoTo 0
        
    ElseIf Mode = 2 Then
        If usf.InsideWidth < 200 Then usf.Width = 200 + 3
        If usf.InsideHeight < 200 Then usf.Height = 200 + 3
        If oldW = 0 Then Exit Sub
        For Each Ctrl In usf.Controls
            NW = CDec(usf.InsideWidth / oldW): NH = CDec(usf.InsideHeight / oldH)
            dimo = Split(Ctrl.Tag, "|")
            Ctrl.Move CDec(dimo(0)) * NW, dimo(1) * NH, CDec(dimo(2)) * NW, CDec(dimo(3)) * NH
            On Error Resume Next
            Ctrl.Font.Size = Round(dimo(4) * Application.Min(NW, NH), 0) - 1
            Err.Clear
            DoEvents
            If TypeName(Ctrl) = "ListBox" Then
                TW = Split(dimo(5), ";")
                For I = 0 To UBound(TW): TW(I) = Val(TW(I)) * NW: Next
                Ctrl.ColumnWidths = Join(TW, ";")
            End If
            
        Next
        On Error GoTo 0
        
    End If
End Sub
et dans le userform par exemple
Code:
Private Sub CommandButton1_Click()
    FullScreenU
    DoEvents
    Dimensions Me, 2
End Sub

Private Sub UserForm_Activate()
    'pour l'exemple je rempli un listbox à 2 colonnes
    For i = 1 To 50
        ListBox1.AddItem i: ListBox1.List(ListBox1.ListCount - 1, 1) = "Item " & i
    Next
    Dimensions Me, 1
End Sub
le module peut être utiliser pour x userform bien sur
Attention ca c'est le model fullscreenfixe
si tu veux un resize dynamique mouse c'est un autre
celui la te met le userform en fullscreen avec zoom au prorata +( font size au min prorata)
 

Pièces jointes

donc ce n'est pas fullscreen mais ça doit couvrir l'app excel?
Si l'appli excel couvre tout l'écran, alors c'est oui.
Mais si l'utilisateur agit sur les dimensions de l'appli excel, alors c'est non.
On risque d'avoir n'importe quoi.
L'objectif est que le formulaire prenne toute la surface de l'écran.
et ce, quelque soit l'espace occupé par l'appli excel.

En fait, sur la bécane tout est nickel (Excel 2010 32 bits).
J'ai partagé ton code avec un membre et il m'a rapporté qu'il plantait sur excel 2019 64 bits.
D'où cette discussion et il est lourd à la détente, je peux juste vous confirmer avec ce code.
Il n'a plus de plantage.
VB:
Option Explicit

#If VBA7 Then
    #If Win64 Then
        Public Declare PtrSafe Function SetWindowLongPtrA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
        Public Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
        Public Declare PtrSafe Function GetWindowLongPtrA Lib "user32" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
        Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
    #Else
        Public Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
        Public Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Public Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
        Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    #End If
#Else
    Public Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#End If

Public Sub SameSizeApplication(Usf As Object)
    Dim ctl As Control, ratioW As Double, ratioH As Double, tbCw, i As Long

    With Application
        ratioW = .UsableWidth / Usf.Width
        ratioH = .Height / Usf.Height
    End With

    Usf.Move 0, 0, Usf.Width * ratioW, Usf.Height * ratioH

    For Each ctl In Usf.Controls
        ctl.Move ctl.Left * ratioW, ctl.Top * ratioH, ctl.Width * ratioW, ctl.Height * ratioH
        On Error Resume Next
        ctl.Font.Size = Round(ctl.Font.Size * Application.Min(ratioH, ratioW))
        On Error GoTo 0

        If TypeName(ctl) = "ListBox" Then
            If ctl.ColumnWidths <> "" Then
                tbCw = Split(Replace(ctl.ColumnWidths, " pt", ""), ";")
                For i = LBound(tbCw) To UBound(tbCw)
                    tbCw(i) = Val(tbCw(i)) * ratioW
                Next
                ctl.ColumnWidths = Join(tbCw, ";")
            End If
        End If
    Next
End Sub

Public Sub ShowFullScreenUserForm(Usf As Object)
    Dim hWnd As Variant

    SameSizeApplication Usf
    DoEvents ' Assure que le UserForm est bien affiché

    hWnd = FindWindowA(vbNullString, Usf.Caption)
    If hWnd = 0 Then Exit Sub ' Sécurité si la fenêtre n'est pas trouvée

    #If VBA7 Then
        #If Win64 Then
            SetWindowLongPtrA hWnd, -16, &H94080080
        #Else
            SetWindowLongA hWnd, -16, &H94080080
        #End If
    #Else
        SetWindowLongA hWnd, -16, &H94080080
    #End If
    ShowWindow hWnd, 3 ' Maximiser la fenêtre
End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
46
Affichages
2 K
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…