{ ----------------------------------- SelectDirectory関数改造ユニット 2003/04/27 VCLのfilectrlユニットからコピーして SelectDirectory関数の hwndOwner := Application.Handle; をOwnerFormのハンドルが渡されるように変更した これで、VCL標準機能ではフォルダ選択ダイアログが 画面の右下の方に表示されていたものが OwnerFormの上にのって表示されるようになる。 //----------------------------------- } unit SelectDirectoryUnit; interface uses Windows, Forms, filectrl; function SelectDirectory(Wnd: HWND; const Caption: string; const Root: WideString; var Directory: string): Boolean; overload; implementation uses ShlObj, ActiveX; function SelectDirCB(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer stdcall; begin if (uMsg = BFFM_INITIALIZED) and (lpData <> 0) then SendMessage(Wnd, BFFM_SETSELECTION, Integer(True), lpdata); result := 0; end; function SelectDirectory(Wnd: HWND; const Caption: string; const Root: WideString; var Directory: string): Boolean; var WindowList: Pointer; BrowseInfo: TBrowseInfo; Buffer: PChar; OldErrorMode: Cardinal; 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; hwndOwner := Wnd; 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); OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS); try ItemIDList := ShBrowseForFolder(BrowseInfo); finally SetErrorMode(OldErrorMode); 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; end.