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

API, COM et SDKs Delphi Discussion :

[D7] Recherche d'un dossier


Sujet :

API, COM et SDKs Delphi

  1. #1
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut [D7] Recherche d'un dossier
    Bonjour,

    pour sélectionner un dossier, j'ai trouvé ceci sur le net.
    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
    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
        implementation
     
        uses
          Windows, Forms, shlobj;
     
        var
          lg_StartFolder: String;
     
        // With later versions of Delphi you may not need these constants.
        const
          BIF_NEWDIALOGSTYLE=$40;
          BIF_NONEWFOLDERBUTTON=$200;
     
    { ============================================================================================= }
        ////////////////////////////////////////////////////////////////////////
        // Call back function used to set the initial browse directory.
        ////////////////////////////////////////////////////////////////////////
        function BrowseForFolderCallBack(Wnd: HWND; uMsg: UINT; lParam,
        lpData: LPARAM): Integer stdcall;
        begin
          if uMsg = BFFM_INITIALIZED then
            SendMessage(Wnd,BFFM_SETSELECTION, 1, Integer(@lg_StartFolder[1]));
          result := 0;
        end;
     
    { ============================================================================================= }
        ////////////////////////////////////////////////////////////////////////
        // This function allows the user to browse for a folder
        //
        // Arguments:-
        //         browseTitle : The title to display on the browse dialog.
        //       initialFolder : Optional argument. Use to specify the folder
        //                       initially selected when the dialog opens.
        //  mayCreateNewFolder : Flag indicating whether the user can create a
        //                       new folder.
        //
        // Returns: The empty string if no folder was selected (i.e. if the user
        //          clicked cancel), otherwise the full folder path.
        ////////////////////////////////////////////////////////////////////////
        function BrowseForFolder2(
          const initialFolder: String =''; const browseTitle: String = 'Choose a Directory' ;
          mayCreateNewFolder: Boolean = False): String;
        var
          browse_info: TBrowseInfo;
          folder: array[0..MAX_PATH] of char;
          find_context: PItemIDList;
        begin
          //--------------------------
          // Initialise the structure.
          //--------------------------
          FillChar(browse_info,SizeOf(browse_info),#0);
          lg_StartFolder := initialFolder;
          browse_info.pszDisplayName := @folder[0];
          browse_info.lpszTitle := PChar(browseTitle);
          browse_info.ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;
          if not mayCreateNewFolder then
            browse_info.ulFlags := browse_info.ulFlags or BIF_NONEWFOLDERBUTTON;
     
          browse_info.hwndOwner := Application.Handle;
          if initialFolder <> '' then
            browse_info.lpfn := BrowseForFolderCallBack;
          find_context := SHBrowseForFolder(browse_info);
          if Assigned(find_context) then
          begin
            if SHGetPathFromIDList(find_context,folder) then
              result := folder
            else
              result := '';
            GlobalFreePtr(find_context);
          end
          else
            result := '';
        end;
    { ============================================================================================= }
    Cela fonctionne bien, mais :

    1 - le dossier initial est bien sélectionné, mais il n'apparait pas forcement dans le Treeview. J'aimerais bien le voir affiché . Ext ce possible ?

    2 - le titre du dialogue est "Rechercher un dossier" est il possible dans le TBrowseInfo (ou ailleurs ?) de le modifier ? (sachant que ce n'est pas BrowseTitle !)

    Merci
    A+
    Charly

  2. #2
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 459
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 459
    Points : 24 873
    Points
    24 873
    Par défaut
    Comme le code semble le même, autant lire : https://www.developpez.net/forums/d1...r/#post6805171

    BROWSEINFOA.pszDisplayName : Receive the display name of the folder selected by the user
    BROWSEINFOA.lpszTitle : displayed above the tree view control in the dialog box
    Rien d'autres

    Ajoute BIF_EDITBOX ( $10 ) qui change l'aspect

    Dans BrowseForFolderCallBack
    Tu peux faire un SetWindowText pour changer le titre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     if uMsg = BFFM_INITIALIZED then
     begin
            SendMessage(Wnd,BFFM_SETSELECTION, 1, Integer(@lg_StartFolder[1]));
            SetWindowText(Wnd, 'Pas Testé mais c''est en théorie');
     end;

    SelectDirectory ne te convenait pas ?
    Attention, il y a deux formes radicalement différent en terme d'affichage.

    TOpenDialog pouvait être modifié pour sélectionner un dossier mais ce n'est pas idéal.
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  3. #3
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Merci Shai,

    SélectDirectory ne permet pas de changer de disque, ni de créer un nouveau dossier ? (sauf forme 2, mais on perds le caption ?)

    Je vais tester tes solutions

    Merci
    A+
    Charly

  4. #4
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Super pour le titre : SetWindowText fonctionne bien, mais comment passer la variable Title à BrowseForFolderCallBack car browse_info.lpfn n'accepte pas de modification des paramètres de BrowseForFolderCallBack ?

    mais BIF_EDITBOX ajoute un TEdit avec le nom du répertoire sélectionné. Ce n'est pas ce que je voulais. Je voulais simplement que le dossier sélectionné soit visible dans le treeview. Actuellement il faut utiliser l’ascenseur.

    A+
    Charly

  5. #5
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    c'est Ok pour le titre, j'ai ajouté une variable globale dans l'unité qui contient BrowseForFolder2

    A+
    Charly

  6. #6
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 345
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 345
    Points : 3 123
    Points
    3 123
    Par défaut
    Bonjour,
    Finalement, j'ai opté pour un SelectDirectory amélioré. On peut mettre un titre, afficher un label et créer ou non des dossiers :

    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
    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    unit DirDialog;
     
    (*  Affiche une boite de sélection de répertoires
     
    Utilisation :
     
    procedure TF_Princ.Button2Click(Sender: TObject);
    Var
        H1       : HWND   ;
        Path1    : string ;
        Caption1 : String ;
        Root1    : string ;
        uFlag1   : DWORD  ;
        Titre    : String
    Begin
       H1      := F_Princ.Handle ;
       uFlag1  := $65  ;  ou $65 + $200  sans création de nouveau dossier
       Caption1 := 'Sélectionnez un dossier' ;    //  Label supérieur
       Titre    := 'Choix d''un dossier' ;        // Titre du dialogue
       Path1 := RepApp ;   // Répertoire initial et de sortie
       Root1 := ''  ;     // Racine du bureau
       SelectDirectoryEx(H1, Path1, Caption1, Root1, uFlag1, Titre)  ;
    End ;   *)
     
    Interface
     
    Uses ShlObj, ActiveX, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,  Grids, DBGrids, StdCtrls, DBCtrls, ComCtrls,
      DB, Menus, ExtCtrls, Shellapi, Buttons, FileCtrl, StrUtils, ShellCtrls;
     
    Function SelectDirectoryEx(hOwn: HWND; var Path: string; Caption, Root: string;
      uFlag: DWORD = $65; Titre : String = 'Choix d''un dossier'): Boolean;
     
    Var
      Title : String ;
     
    Implementation
    { ===================================================================== }
    Function SelectDirectoryEx(hOwn: HWND; var Path: string; Caption, Root: string;
      uFlag: DWORD = $65; Titre : String = 'Choix d''un dossier'): Boolean;
    Const
      //  pour uFlag :
      BIF_NEWDIALOGSTYLE = $0040;         // Style moderne
      BIF_NONEWFOLDERBUTTON = $0200 ;     // pas de création de répertoire
      BIF_VALIDATE = $0020 ;              // Possibilité de message d'erreur
      BIF_RETURNONLYFSDIRS = $0001 ;      // Bouton Ok grisé si pas de sélection
      BIF_STATUSTEXT = $0004 ;            // Affichage d'un label haut
    var
      BrowseInfo: TBrowseInfo;
      Buffer: PChar;
      RootItemIDList, ItemIDList: PItemIDList;
      ShellMalloc: IMalloc;
      IDesktopFolder: IShellFolder;
      Dummy: LongWord;
     
      { ---------------------------------------------------------------- }
      function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: Cardinal;
        lpData: Cardinal): Integer; stdcall;
      var
        PathName: array[0..MAX_PATH] of Char;
      begin
        case uMsg of
          BFFM_INITIALIZED:
            Begin
            SendMessage(Hwnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
            SetWindowText(Hwnd, Pchar(Title));
     
            End ;
          BFFM_SELCHANGED:
            begin
              SHGetPathFromIDList(PItemIDList(lParam), @PathName);
              SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, Longint(PChar(@PathName)));
            end;
        end;
        Result := 0;
      end;
     
      { ---------------------------------------------------------------- }
    begin
      Title := Titre ;
      Result := False;
      FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
      if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
      begin
        Buffer := ShellMalloc.Alloc(MAX_PATH);
        try
          RootItemIDList := nil;
          if Root <> '' then
          begin
            SHGetDesktopFolder(IDesktopFolder);
            IDesktopFolder.ParseDisplayName(hOwn, nil, POleStr(WideString(Root)),
              Dummy, RootItemIDList, Dummy);
          end;
          with BrowseInfo do
          begin
            hwndOwner := hOwn;
            pidlRoot := RootItemIDList;
            pszDisplayName := Buffer;
            lpszTitle := PChar(Caption);
            ulFlags := uFlag;
            lpfn := @BrowseCallbackProc;
            lParam := Integer(PChar(Path));
          end;
          ItemIDList := ShBrowseForFolder(BrowseInfo);
          Result := ItemIDList <> nil;
          if Result then
          begin
            ShGetPathFromIDList(ItemIDList, Buffer);
            ShellMalloc.Free(ItemIDList);
            Path := StrPas(Buffer);
          end;
        finally
          ShellMalloc.Free(Buffer);
        end;
      end;
    end;
    { ======================================================================= }
    end.
    A+
    Charly

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 17/05/2010, 10h43
  2. Recherche fichier et dossier
    Par sophie447 dans le forum VBScript
    Réponses: 2
    Dernier message: 04/11/2009, 11h16
  3. [BATCH] Recherche dans sous-dossiers
    Par tonf dans le forum Scripts/Batch
    Réponses: 9
    Dernier message: 13/08/2008, 15h17
  4. recherche sur des dossiers
    Par Marcus15 dans le forum Langage
    Réponses: 6
    Dernier message: 27/02/2007, 15h17
  5. Recherche d'un dossier dans le disque
    Par com-ace dans le forum Access
    Réponses: 6
    Dernier message: 31/08/2006, 10h51

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