📄 useldir.pas
字号:
if NewDrive <> #0 then
SetDirectory(Format('%s:%s', [NewDrive, DirPart]))
else
SetDirectory(DirPart);
if (Pos('*', FilePart) > 0) or (Pos('?', FilePart) > 0) then
SetMask (FilePart)
else if Length(FilePart) > 0 then
begin
SetFileName (FilePart);
if FileExists (FilePart) then
begin
if GetFileName = '' then
begin
SetMask(FilePart);
SetFileName (FilePart);
end;
end
else
raise EInvalidOperation.CreateFmt(SInvalidFileName, [EditText]);
end;
end;
function TFileListBox.GetFilePath: string;
begin
Result := '';
if GetFileName <> '' then
Result := SlashSep(FDirectory, GetFileName);
end;
procedure TFileListBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) then
begin
if (AComponent = FFileEdit) then FFileEdit := nil
else if (AComponent = FDirList) then FDirList := nil
else if (AComponent = FFilterCombo) then FFilterCombo := nil;
end;
end;
{ TFilterComboBox }
constructor TFilterComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Style := csDropDownList;
FFilter := SDefaultFilter;
MaskList := TStringList.Create;
end;
destructor TFilterComboBox.Destroy;
begin
MaskList.Free;
inherited Destroy;
end;
procedure TFilterComboBox.CreateWnd;
begin
inherited CreateWnd;
BuildList;
end;
function TFilterComboBox.IsFilterStored: Boolean;
begin
Result := SDefaultFilter <> FFilter;
end;
procedure TFilterComboBox.SetFilter(const NewFilter: string);
begin
if AnsiCompareFileName(NewFilter, FFilter) <> 0 then
begin
FFilter := NewFilter;
if HandleAllocated then BuildList;
Change;
end;
end;
procedure TFilterComboBox.SetFileListBox (Value: TFileListBox);
begin
if FFileList <> nil then FFileList.FFilterCombo := nil;
FFileList := Value;
if FFileList <> nil then
begin
FFileList.FreeNotification(Self);
FFileList.FFilterCombo := Self;
end;
end;
procedure TFilterComboBox.Click;
begin
inherited Click;
Change;
end;
function TFilterComboBox.GetMask: string;
begin
if ItemIndex < 0 then
ItemIndex := Items.Count - 1;
if ItemIndex >= 0 then
begin
Result := MaskList[ItemIndex];
end
else
Result := '*.*';
end;
procedure TFilterComboBox.BuildList;
var
AFilter, MaskName, Mask: string;
BarPos: Integer;
begin
Clear;
MaskList.Clear;
AFilter := Filter;
BarPos := AnsiPos('|', AFilter);
while BarPos <> 0 do
begin
MaskName := Copy(AFilter, 1, BarPos - 1);
Delete(AFilter, 1, BarPos);
BarPos := AnsiPos('|', AFilter);
if BarPos > 0 then
begin
Mask := Copy(AFilter, 1, BarPos - 1);
Delete(AFilter, 1, BarPos);
end
else
begin
Mask := AFilter;
AFilter := '';
end;
Items.Add(MaskName);
MaskList.Add(Mask);
BarPos := AnsiPos('|', AFilter);
end;
ItemIndex := 0;
end;
procedure TFilterComboBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFileList) then
FFileList := nil;
end;
procedure TFilterComboBox.Change;
begin
if FFileList <> nil then FFileList.Mask := Mask;
inherited Change;
end;
{ TSelectDirDlg }
constructor TSelectDirDlg.Create(AOwner: TComponent);
begin
inherited CreateNew(AOwner);
Caption := SSelectDirCap;
BorderStyle := bsDialog;
ClientWidth := 424;
ClientHeight := 255;
Position := poScreenCenter;
Font.Name := '宋体';
Font.Size := 10;
DirEdit := TEdit.Create(Self);
with DirEdit do
begin
Parent := Self;
SetBounds(8, 24, 313, 20);
Visible := False;
TabOrder := 1;
end;
with TLabel.Create(Self) do
begin
Parent := Self;
SetBounds(8, 8, 92, 13);
FocusControl := DirEdit;
Caption := SDirNameCap;
end;
DriveList := TDriveComboBox.Create(Self);
with DriveList do
begin
Parent := Self;
SetBounds(232, 192, 185, 19);
TabOrder := 2;
OnChange := DriveListChange;
end;
with TLabel.Create(Self) do
begin
Parent := Self;
SetBounds(232, 176, 41, 13);
Caption := SDrivesCap;
FocusControl := DriveList;
end;
DirLabel := TPathLabel.Create(Self);
with DirLabel do
begin
Parent := Self;
SetBounds(120, 8, 213, 13);
end;
DirList := TDirectoryListBox.Create(Self);
with DirList do
begin
Parent := Self;
SetBounds(8, 72, 213, 138);
TabOrder := 0;
TabStop := True;
ItemHeight := 17;
IntegralHeight := True;
OnChange := DirListChange;
end;
with TLabel.Create(Self) do
begin
Parent := Self;
SetBounds(8, 56, 66, 13);
Caption := SDirsCap;
FocusControl := DirList;
end;
FileList := TFileListBox.Create(Self);
with FileList do
begin
Parent := Self;
SetBounds(232, 72, 185, 93);
TabOrder := 6;
TabStop := True;
FileType := [ftNormal];
Mask := '*.*';
Font.Color := clGrayText;
ItemHeight := 13;
end;
with TLabel.Create(Self) do
begin
Parent := Self;
SetBounds(232, 56, 57, 13);
Caption := SFilesCap;
FocusControl := FileList;
end;
NetButton := TButton.Create(Self);
with NetButton do
begin
Parent := Self;
SetBounds(8, 224, 77, 27);
Visible := False;
TabOrder := 3;
Caption := SNetworkCap;
OnClick := NetClick;
end;
OKButton := TButton.Create(Self);
with OKButton do
begin
Parent := Self;
SetBounds(172, 224, 77, 27);
TabOrder := 4;
OnClick := OKClick;
Caption := SOKButton;
ModalResult := 1;
Default := True;
end;
CancelButton := TButton.Create(Self);
with CancelButton do
begin
Parent := Self;
SetBounds(256, 224, 77, 27);
TabOrder := 5;
Cancel := True;
Caption := SCancelButton;
ModalResult := 2;
end;
HelpButton := TButton.Create(Self);
with HelpButton do
begin
Parent := Self;
SetBounds(340, 224, 77, 27);
TabOrder := 7;
Caption := SHelpButton;
OnClick := HelpButtonClick;
end;
FormCreate(Self);
ActiveControl := DirList;
end;
procedure TSelectDirDlg.HelpButtonClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
procedure TSelectDirDlg.DirListChange(Sender: TObject);
begin
DirLabel.Caption := DirList.Directory;
FileList.Directory := DirList.Directory;
DirEdit.Text := DirLabel.Caption;
DirEdit.SelectAll;
end;
procedure TSelectDirDlg.FormCreate(Sender: TObject);
var
UserHandle: THandle;
NetDriver: THandle;
WNetGetCaps: function (Flags: Word): Word;
begin
{ is network access enabled? }
UserHandle := GetModuleHandle(User32);
@WNetGetCaps := GetProcAddress(UserHandle, 'WNETGETCAPS');
if @WNetGetCaps <> nil then
begin
NetDriver := WNetGetCaps(Word(-1));
if NetDriver <> 0 then
begin
@WNetConnectDialog := GetProcAddress(NetDriver, 'WNETCONNECTDIALOG');
NetButton.Visible := @WNetConnectDialog <> nil;
end;
end;
FAllowCreate := False;
DirLabel.BoundsRect := DirEdit.BoundsRect;
DirListChange(Self);
end;
procedure TSelectDirDlg.DriveListChange(Sender: TObject);
begin
DirList.Drive := DriveList.Drive;
end;
procedure TSelectDirDlg.SetAllowCreate(Value: Boolean);
begin
if Value <> FAllowCreate then
begin
FAllowCreate := Value;
DirLabel.Visible := not FAllowCreate;
DirEdit.Visible := FAllowCreate;
end;
end;
procedure TSelectDirDlg.SetDirectory(const Value: string);
var
Temp: string;
begin
if Value <> '' then
begin
Temp := ExpandFileName(SlashSep(Value,'*.*'));
if (Length(Temp) >= 3) and (Temp[2] = ':') then
begin
DriveList.Drive := Temp[1];
Temp := ExtractFilePath(Temp);
try
DirList.Directory := Copy(Temp, 1, Length(Temp) - 1);
except
on EInOutError do
begin
GetDir(0, Temp);
DriveList.Drive := Temp[1];
DirList.Directory := Temp;
end;
end;
end;
end;
end;
function TSelectDirDlg.GetDirectory: string;
begin
if FAllowCreate then
Result := DirEdit.Text
else
Result := DirLabel.Caption;
end;
procedure TSelectDirDlg.NetClick(Sender: TObject);
begin
if Assigned(WNetConnectDialog) then
WNetConnectDialog(Handle, WNTYPE_DRIVE);
end;
procedure TSelectDirDlg.OKClick(Sender: TObject);
begin
if AllowCreate and Prompt and (not DirectoryExists(Directory)) and
(MessageDlg(SConfirmCreateDir, mtConfirmation, [mbYes, mbNo],
0) <> mrYes) then
ModalResult := 0;
end;
function SelectDirectory(var Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
var
D: TSelectDirDlg;
begin
D := TSelectDirDlg.Create(Application);
try
D.Directory := Directory;
D.AllowCreate := sdAllowCreate in Options;
D.Prompt := sdPrompt in Options;
{ scale to screen res }
if Screen.PixelsPerInch <> 96 then
begin
D.ScaleBy(Screen.PixelsPerInch, 96);
D.FileList.ParentFont := True;
D.Left := (Screen.Width div 2) - (D.Width div 2);
D.Top := (Screen.Height div 2) - (D.Height div 2);
D.FileList.Font.Color := clGrayText;
end;
if HelpCtx = 0 then
begin
D.HelpButton.Visible := False;
D.OKButton.Left := D.CancelButton.Left;
D.CancelButton.Left := D.HelpButton.Left;
end
else D.HelpContext := HelpCtx;
Result := D.ShowModal = mrOK;
if Result then
begin
Directory := D.Directory;
if sdPerformCreate in Options then
ForceDirectories(Directory);
end;
finally
D.Free;
end;
end;
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(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;
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;
function DirectoryExists(const Name: string): Boolean;
begin
Result := SysUtils.DirectoryExists(Name);
end;
function ForceDirectories(const Dir: string): Boolean;
begin
Result := SysUtils.ForceDirectories(Dir);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -