Solved

ShBrowseForFolder with Make New Folder button

Posted on 2004-08-05
4
2,102 Views
Last Modified: 2008-01-09
Could someone please post an example of how to call ShBrowseForFolder which displays the Make New Folder button, and which uses a callback function to set the initial directory.

Thanks
0
Comment
Question by:JohnStevenson
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
are you asking how to add a "Make New Folder button"  to a ShBrowseForFolder  Dialog box?
0
 

Author Comment

by:JohnStevenson
Comment Utility
No, I know how to do this, by adding the BIF_NEWDIALOGSTYLE flag to the ulFlags parameterof the BrowseInfo structure.

My problem is that when I do this, I get a working "Make New Folder" button but I loose the initial directory setting of the folder dialog. Here's the code:

function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
var
  Path: array[0..MAX_PATH] of Char;
  ptPos: TPoint;
  Rect : TRect;

begin

  case uMsg of
    BFFM_INITIALIZED:
      begin
        {Position folder window}
        GetWindowRect(hWnd, Rect);
        ptPos := GetFormPosition(frmMain, Rect.Right - Rect.Left,
          Rect.Bottom - Rect.Top, 1);
        MoveWindow(hWnd, ptPos.X, ptPos.Y, Rect.Right - Rect.Left,
          Rect.Bottom - Rect.Top, True);
        SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData);
        SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, lpData);
      end;
    BFFM_SELCHANGED:
      begin
        {Set the status text to the currently selected path.}
        if SHGetPathFromIDList(Pointer(lParam), Path) then
          SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, Integer(@Path));
      end;
  end;

  Result := 0;

end; { BrowseCallbackProc }


function SelectDirectory(const Caption, InitialDir: string; const Root: WideString;
  ShowStatus: Boolean; out Directory: string): Boolean;
var BrowseInfo: TBrowseInfo;
    Buffer: PChar;
    RootItemIDList,
    ItemIDList: PItemIDList;
    ShellMalloc: IMalloc;
    IDesktopFolder: IShellFolder;
    Eaten, Flags: LongWord;
    Windows: Pointer;
    Path: String;

begin

  Result := False;

  Directory := '';
  Path := InitialDir;

  if (Length(Path) > 0) and (Path[Length(Path)] = '\') then
    Delete(Path, Length(Path), 1);

  FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);

  if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
  begin

    Buffer := ShellMalloc.Alloc(MAX_PATH);

    try
      SHGetDesktopFolder(IDesktopFolder);
      IDesktopFolder.ParseDisplayName(Application.Handle, nil, PWideChar(Root),
        Eaten, RootItemIDList, Flags);

      with BrowseInfo do
      begin
        hwndOwner := Application.Handle;
        pidlRoot := RootItemIDList;
        pszDisplayName := Buffer;
        lpszTitle := PChar(Caption);
        ulFlags := BIF_RETURNONLYFSDIRS or BIF_NEWDIALOGSTYLE;

        if ShowStatus then
          ulFlags := ulFlags or BIF_STATUSTEXT;

        lParam := Integer(PChar(Path));
        lpfn := BrowseCallbackProc;

      end;
 
      Result := ItemIDList <> nil;

      if Result then
      begin
        ShGetPathFromIDList(ItemIDList, Buffer);
        ShellMalloc.Free(ItemIDList);
        Directory := Buffer;
      end;

    finally
      ShellMalloc.Free(Buffer);
    end;

  end;

end; { SelectDirectory }
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 200 total points
Comment Utility
OK, here is some code that I tried, and it seems to be able to do the browse dialog with the required stuff, Although, when I did it I could NOT get a Status Text to show when I used the BIF_NEWDIALOGSTYLE, no matter what I did, so I added a static Label and just set the text for that, I do Not have time now to fully test this, but it worked on my machine with windows XP

OH, the  MAX_PATH  for a maximum text length went out with win 95 first edition, I do not think that is a valid setting for win XP, I usually use a 2 KB buffer, but as far as I have seen there is NO LIMIT for the text path for XP in shell ops


implementation

{$R *.DFM}

var
hStatic1: Integer;



function BrowseCallbackProc(hWnd,iMsg, lParam, pData: Integer): Integer; stdcall;
//function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
var
  Path: array[0..2047] of Char;
  ptPos: TPoint;
  Rect : TRect;

begin
  case iMsg of
    BFFM_INITIALIZED:
      begin
        {Position folder window}
        GetWindowRect(hWnd, Rect);
        //ptPos := GetFormPosition(frmMain, Rect.Right - Rect.Left,
        //  Rect.Bottom - Rect.Top, 1);
        ptPos.x := Form1.Left+20;
        ptPos.y := Form1.Top+20;
        MoveWindow(hWnd, ptPos.X, ptPos.Y, Rect.Right - Rect.Left,
          Rect.Bottom - Rect.Top, False);
        hStatic1 := CreateWindow('Static', PChar(OleStrToString(PWideChar(pData))){'Number of Messages'},
         WS_VISIBLE or WS_CHILD,6,26,(Rect.Right - Rect.Left)+52,32,hWnd,0,hInstance,nil);
         SendMessage(hStatic1,WM_SETFONT,GetStockObject(ANSI_VAR_FONT),0);
        if pData > 0 then
        begin
        SendMessage(hwnd, WM_USER + 103{BFFM_SETSELECTION}, 1, pData);
        //SendMessage(hwnd, WM_USER + 104{BFFM_SETSTATUSTEXT}, 0, pData);
        end;
//define BFFM_SETSELECTIONA      (WM_USER + 102)
//define BFFM_SETSELECTIONW      (WM_USER + 103)
//define BFFM_SETSTATUSTEXTW     (WM_USER + 104)
      end;
    BFFM_SELCHANGED:
      begin
        {Set the status text to the currently selected path.}
        //if SHGetPathFromIDList(PItemIDList(LParam), ListFolder) then
        if SHGetPathFromIDList(Pointer(lParam), Path) then
          //SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, Integer(@Path));
          SetWindowText(hStatic1, Path);
      end;
  end;

  Result := 0;

end; { BrowseCallbackProc }

//function SelectDirectory(const Caption, InitialDir: string; const Root: WideString;
//  ShowStatus: Boolean; out Directory: string): Boolean;
function SelectDirectory(const Caption, InitialDir: string; const Root: String;
  ShowStatus: Boolean; out Directory: string): Boolean;
var BrowseInfo: TBrowseInfo;
    //Buffer: PChar;
    CharBuf: array[0..2047] of Char;
    RootItemIDList,
    pBrowseIDL: PItemIDList;
    ShMemAlloc: IMalloc;
    IDesktopFolder: IShellFolder;
    Eaten, Flags: LongWord;
    //Windows: Pointer;
    Path: String;

begin
  Result := False;

  Directory := '';
  Path := InitialDir;

  if (Length(Path) > 0) and (Path[Length(Path)] = '\') then
    Delete(Path, Length(Path), 1);

  FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);

  if (ShGetMalloc(ShMemAlloc) = S_OK) and (ShMemAlloc <> nil) then
  begin

    //Buffer := ShellMalloc.Alloc(2048);

    //try
      SHGetDesktopFolder(IDesktopFolder);
    //IDesktopFolder.ParseDisplayName(Application.Handle, nil, PWideChar(Root),
    //    Eaten, RootItemIDList, Flags);
      IDesktopFolder.ParseDisplayName(Application.Handle, nil, PWideChar(WideString(Root)),
        Eaten, RootItemIDList, Flags);

      with BrowseInfo do
      begin
        hwndOwner := Application.Handle;
        pidlRoot := RootItemIDList;
        pszDisplayName := @CharBuf;
        lpszTitle := PChar(Caption);
        ulFlags := BIF_RETURNONLYFSDIRS or $40{or BIF_NEWDIALOGSTYLE};
 //define BIF_NEWDIALOGSTYLE     0x0040
        //if ShowStatus then
        //  ulFlags := ulFlags or BIF_STATUSTEXT;

        //lParam := Integer(PChar(Path));
        lParam := Integer(PWideChar(WideString(Path)));
        lpfn := @BrowseCallbackProc;

      end;
      pBrowseIDL := SHBrowseForFolder(BrowseInfo);
      Result := pBrowseIDL <> nil;

      if Result then
      begin
        SHGetPathFromIDList(pBrowseIDL, @CharBuf);
        ShMemAlloc.Free(pBrowseIDL);
        Directory := CharBuf;
      end;

    //finally
      //ShellMalloc.Free(Buffer);
    //end;

  end;

end;

procedure TForm1.sbut_Browse4FolderClick(Sender: TObject);
var
Dir1: String;
begin
if SelectDirectory('A Browse Dialog WHAT', 'C:\Stuff\Games', 'C:\', True, Dir1) then
  Showmessage(Dir1);
end;
0
 

Author Comment

by:JohnStevenson
Comment Utility
Thanks Slick812.

I got it working using your sample. Incidentally, using BFFM_SETEXPANDED (WM_USER + 106) in the callback and passing a Pidl rather than a pointer to a wide string, seems to work better than using BFFM_SETSELECTION. I had problems with this when my BrowseInfo.pidlRoot was something other than the Desktop (or null).
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now