SelectDiretory

xiaoxiao2021-03-06  55

There is a function in Delphi, with two forms: Function SelectDirectory (Const Caption: String; Contly: WideString; Out Directory: String): Boolean; OVERLOAD

Function SelectDirectory (var Directory: String; Helpctx: longint): boolean; overload; can call Win32 standard selection directory dialog box, the second method is popped up is a Delphi custom style conversation frame. We are commonly used, but I found in use, using this function that cannot initialize the start directory of the dialog, such as the right: I hope that the dialog box is positioned to a directory, it is not available.

I have never been single, I haven't found the answer for a long time, until one day finally registered "Dahoma Forum" (in fact, I have known the Japanese Forum for a long time, just registration, I can't register), I have given the question is "how to specify" Starting Directory of SelectDirectory. The problem quickly learned, the answer was provided by Cakk, as follows: Send the message to the window: SendMessage (hwnd, bffm_setSelection, ORD (TRUE), Longint (Pchar (PCHAR (PCHAR (PCHAR (PCHAR (PCHAR (PCH));

The key is how to get the handle of the window? Borland omits the LPFN property of BrowseInfo when writing the SelectDirectory function. This property points to a callback function that implements your program and the dialog window. The callback function is declared as: int BrowseCallbackProc (HWND HWND, UINT UMSG, LPARAM LPARAM LParam LPDATA);

The HWND parameter is the handle of the dialog passed, get this handle, you can use the sendMessage setting path before I am.

Also, you should judge the path when you accept the BFFM_Initialized message in the BrowseCallbackProc function, that is, when: umsg: = bffm_initialized. The specific implementation is as follows:

1. Cannot use the SelectDirectory function (if you want to modify its source code), you need to directly call the API function ShbrowseForfolder. 2. To put the SHLOBJ and ACRIVEX two units into it. Unit Unit1; Interface Uses ... shlobj, ActiveX; ......

Var Form1: TFORM1; PATH: STRING; // Start path

IMPLEMENTATION

{$ R * .dfm}

function BrowseCallbackProc (hwnd: HWND; uMsg: UINT; lParam: Cardinal; lpData: Cardinal): integer; stdcall; begin if uMsg = BFFM_INITIALIZED then result: = SendMessage (Hwnd, BFFM_SETSELECTION, Ord (TRUE), Longint (PChar (Path) )) Else Result: = 1 End;

function SelDir (const Caption: string; const Root: WideString; out Directory: string): Boolean; var WindowList: Pointer; BrowseInfo: TBrowseInfo; Buffer: PChar; RootItemIDList, ItemIDList: PItemIDList; ShellMalloc: IMalloc; IDesktopFolder: IShellFolder; Eaten, Flags: LongWord; begin Result: = False; Directory: = ''; 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 (Application.Handle, nil, POleStr (Root), Eaten, RootItemIDList, Flags); end; with BrowseInfo do begin hwndOwner: = Application.Handle; pidlRoot: = RootItemIDList; pszDisplayName: = Buffer; lpszTitle: = PChar (Caption); ulFlags: = BIF_RETURNONLYFSDIRS; lpfn: = @ BrowseCallbackPro c; lParam: = BFFM_INITIALIZED; end; WindowList: = DisableTaskWindows (0); try ItemIDList: = ShBrowseForFolder (BrowseInfo); finally EnableTaskWindows (WindowList); end; Result: = ItemIDList <> nil; if Result then begin ShGetPathFromIDList (ItemIDList, Buffer; shellmalloc.free (itemidlist); Directory: = Buffer; End; Finally Shellmalloc.free (buffer); end; end;

Procedure TForm1.SpeedButton1Click (Sender: TOBJECT); VAR PATH1: STRING; begin path: = Edit1.text; SELDIR ('selectdiRectory Sample,' ', Path1); edit1.text: = path1 end; end.

Posted in "Agan's Home" August 18, 2000

转载请注明原文地址:https://www.9cbs.com/read-116460.html

New Post(0)