?
Solved

ShBrowseForFolder with Make New Folder button

Posted on 2004-08-05
4
Medium Priority
?
2,213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 34

Expert Comment

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

Author Comment

by:JohnStevenson
ID: 11750933
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 34

Accepted Solution

by:
Slick812 earned 800 total points
ID: 11756756
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
ID: 11764304
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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