SelectDirectory无法设置初始目录的问题?(195分)

  • 主题发起人 主题发起人 chcw
  • 开始时间 开始时间
C

chcw

Unregistered / Unconfirmed
GUEST, unregistred user!
我程序中调用SelectDirectory选择目录,如下:
procedure TForm1.Button1Click(Sender: TObject);
var
Dir: string;
begin
Dir := 'F:/';
SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt], 0);
end;

但是打开的SelectDirectory的初始目录不是F:/,请问如何解决?
 
F:/盘存在吗
 
当然存在了。
 
FileCtrl单元
老兄,我用你的源代码,没有一点问题
 
Delphi6已经解决了该问题,如果你用D5的话,把下面的源代码写进去吧
function SelectDirectory(const Caption: string; const Root: WideString;
var Directory: string): Boolean;
var
WindowList: Pointer;
BrowseInfo: TBrowseInfo;
Buffer: PChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Eaten, Flags: LongWord;
begin
Result := False;
if not DirectoryExists(Directory) then
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;
if Directory <> '' then
begin
lpfn := SelectDirCB;
lParam := Integer(PChar(Directory));
end;
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;
end;


调用:
var
s:string;
begin
s:='d:/';
selectdirectory('please select:','',s);
end;
 
to 郭玉梁:
我是在2000下,Delphi 5, Update Pack1. 我可以把Source Code和编译好的代码发给您。
 
呵呵,一定是忘了在uses中添加FileCtrl单元了。
 
to PFans:
没忘,uses里有FileCtrl单元的。
 
用chdir('F:/')不行吗?
 
没问题呀
我是在xp下,Delphi 5, Update Pack1.
 
var dir1:string;
selectdirectory('','f:/',dir1);
就可以了。
注:selectdirectory是重載函數,其有兩種帶參數格式。
可查看Filectrl單元。
 
如果你要opendialog打开固定目录,可以用edit1.txt:='f:/';
opendialog1.initialdir:=edit1.text;
其他的可以推理得知
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
710
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部