📄 ajfilebrowser.pas
字号:
DateStr := Copy(DateStr, succ(TimePos), Length(DateStr)-TimePos);
h := StrToInt(Copy(DateStr, 1, 2));
m := StrToInt(Copy(DateStr, 4, 2));
s := StrToInt(Copy(DateStr, 7, 2));
n := StrToInt(Copy(DateStr, 10, 3));
Result := Result + EncodeTime(h, m, s, n);
end; {if}
end; {ReconstructDate}
{................................................................................................}
function CompareDates(DateStr1, Filename1, DateStr2, Filename2 : string) : integer;
var
Date1, Date2 : TDateTime; // Compare the dates.
begin
Date1 := ReconstructDate(DateStr1); // Well get the dates first.
Date2 := ReconstructDate(DateStr2);
if (Date1 > Date2) then
Result := 1
else if (Date1 < Date2) then
Result := -1
else // Same dates so sort them by name.
Result := CompareText(Filename1, Filename2);
end; {CompareDates}
{................................................................................................}
var
DirFile1, DirFile2 : string;
begin
DirFile1 := Item1.SubItems.Strings[pred(cColDirFile)]; // Get the file type ... D or F.
DirFile2 := Item2.SubItems.Strings[pred(cColDirFile)];
if (Pos('[', Item1.Caption) = 1) or (Pos('[', Item2.Caption) = 1) then begin
// Must keep the parent root at the
if (Pos('[', Item1.Caption) = 1) then // top of the list at all costs.
Compare := -1 // The '[' uniquely defines this
else // entry.
Compare := 1;
end else begin
if (DirFile1 = 'D') and (DirFile2 = 'F') then // If one's a file and the other's a
// directory then it's a no brainer ...
Compare := -1 // directories come first or last
// depending on the sort direction.
else if (DirFile1 = 'F') and (DirFile2 = 'D') then
Compare := 1
else begin
case fColumnToSort of // Choose our column ...
cColName : Compare := CompareText(DirFile1 + Item1.Caption,
DirFile2 + Item2.Caption);
cColSize : Compare := CompareFileSize(DirFile1, Item1.Caption, Item1.SubItems.Strings[pred(fColumnToSort)],
DirFile2, Item2.Caption, Item2.SubItems.Strings[pred(fColumnToSort)]);
cColType : Compare := CompareText(Item1.SubItems.Strings[pred(fColumnToSort)] + Item1.Caption,
Item2.SubItems.Strings[pred(fColumnToSort)] + Item2.Caption);
cColCreated,
cColModified,
cColAccessed : Compare := CompareDates(Item1.SubItems.Strings[pred(fColumnToSort)], Item1.Caption,
Item2.SubItems.Strings[pred(fColumnToSort)], Item2.Caption);
cColAttributes : Compare := CompareText(DirFile1 + Item1.SubItems.Strings[pred(fColumnToSort)] + Item1.Caption,
DirFile2 + Item2.SubItems.Strings[pred(fColumnToSort)] + Item2.Caption);
cColFullName : Compare := CompareText(Item1.SubItems.Strings[pred(fColumnToSort)],
Item2.SubItems.Strings[pred(fColumnToSort)]);
end; {case}
end; {if}
if not fSortAscending then // Switch our return value depending
Compare := Compare * -1; // on the sort direction.
end; {if}
end; {lvFilesCompare}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.FileSelected(Item : TListItem);
begin
if (Item <> nil) then begin
if (Item.SubItems[pred(cColDirFile)] = 'D') then begin
fFilename := Item.SubItems[pred(cColFullName)];
if (fFilename <> '') then begin
if (fFilename[Length(fFilename)] = '\') then
fFilename := ExtractFilePath(ExcludeTrailingBackslash(fFilename))
else
fFilename := fFilename + '\';
end; {if}
edFilename.Text := fFilename;
if (fFilename = '') then begin
StatusBar.Panels[0].Text := '';
StatusBar.Panels[1].Text := '';
lvDrives.Visible := true;
lvFiles.Visible := false;
HighlightFocusedItem(lvDrives);
end else begin
lvDrives.Visible := false;
lvFiles.Visible := true;
UpdateFileListView(fFileMask, fFileAttributes);
HighlightFocusedItem(lvFiles);
end; {if}
SetBookmarkControls(mnuPopBookmark.Tag);
end else // or a file ... so use its association.
ShellExecute(Application.Handle, 'Open', PChar(Item.SubItems[pred(cColFullName)]), nil, nil, SW_NORMAL);
end; {if}
end; {FileSelected}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvFilesDblClick(Sender : TObject);
begin
FileSelected(lvFiles.Selected); // A double click ... so do it !
end; {lvFilesDblClick}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvFilesKeyDown(Sender : TObject; var Key : Word; Shift : TShiftState);
begin
with lvFiles do begin
case Key of
VK_LEFT : if (Pos('[', Items[0].Caption) = 1) and (lvFiles.ViewStyle = vsReport) then begin
FileSelected(Items[0]);
Key := 0;
end; {if}
VK_RIGHT : if (Selected <> nil) and (lvFiles.ViewStyle = vsReport) then begin
if (Pos('[', Selected.Caption) = 0) and
(Selected.SubItems[pred(cColDirFile)] = 'D') then
FileSelected(Selected);
Key := 0;
end; {if}
VK_RETURN : FileSelected(Selected); // Return pressed ... so do that too !
end; {case}
end; {with}
end; {lvFilesKeyDown}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvFilesSelectItem(Sender : TObject; Item : TListItem; Selected : boolean);
begin
if Selected then begin
lvFiles.PopupMenu := lvFilesPopupMenu;
fFilename := Item.SubItems.Strings[pred(cColFullName)];
edFilename.Text := fFilename;
SetBookmarkControls(mnuPopBookmark.Tag);
end else
lvFiles.PopupMenu := nil;
end; {lvFilesSelectItem}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.RecentKeyClick(Keyname : string);
// Recover a previous bookmark - directory or file.
begin
Update;
Filename := Keyname;
if (Keyname[Length(KeyName)] = '\') then begin
KeyName := ExtractFilePath(ExcludeTrailingBackslash(KeyName));
KeyName := '[' + ExtractFileName(ExcludeTrailingBackslash(KeyName)) + ']';
if (Keyname = '[]') then
Keyname := '[..]';
end; {if}
if FileFindAndSelect(lvFiles, ExtractFilename(Keyname)) then
fajRecentKeys.UpdateRecentKeys(Keyname, tAdd)
else
fajRecentKeys.UpdateRecentKeys(Keyname, tDelete);
SetBookmarkControls(mnuPopBookmark.Tag);
end; {RecentFileClick}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvDrivesCompare(Sender : TObject; Item1, Item2 : TListItem; Data : integer; var Compare : integer);
begin
Compare := CompareText(Item1.SubItems.Strings[cColDriveName], Item2.SubItems.Strings[cColDriveName]);
end; {lvDrivesCompare}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvDrivesDblClick(Sender : TObject);
begin
if (lvDrives.Selected <> nil) then begin
fFilename := lvDrives.Selected.SubItems.Strings[cColDriveName];
UpdateFileListView(fFileMask, fFileAttributes);
HighlightFocusedItem(lvFiles);
end; {if}
end; {lvDrivesDblClick}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.SetBookmarkControls(Tag : integer);
var
Glyph : TBitmap;
EnableBookmarks : boolean;
begin
EnableBookmarks := lvFiles.Visible and (lvFiles.Selected <> nil);
if EnableBookmarks then
EnableBookmarks := (lvFiles.Selected.SubItems.Strings[pred(cColFullName)] <> '');
if EnableBookmarks then begin
btnBookmark.Enabled := true;
mnuPopBookmark.Enabled := true;
if fajRecentKeys.InList(lvFiles.Selected.SubItems.Strings[pred(cColFullName)]) then begin
if (Tag = cAddBookmark) then begin
Glyph := TBitmap.Create;
imButtons.GetBitmap(1, Glyph);
btnBookmark.Glyph.Assign(Glyph);
Glyph.Free;
btnBookmark.Hint := 'Remove bookmark';
btnBookmark.Tag := cRemoveBookmark;
mnuPopBookmark.ImageIndex := cRemoveBookmark;
mnuPopBookmark.Caption := '&Remove bookmark';
mnuPopBookmark.Tag := cRemoveBookmark;
end; {if}
end else begin
if (Tag = cRemoveBookmark) then begin
Glyph := TBitmap.Create;
imButtons.GetBitmap(0, Glyph);
btnBookmark.Glyph.Assign(Glyph);
Glyph.Free;
btnBookmark.Hint := 'Add bookmark';
btnBookmark.Tag := cAddBookmark;
mnuPopBookmark.ImageIndex := cAddBookmark;
mnuPopBookmark.Caption := '&Add bookmark';
mnuPopBookmark.Tag := cAddBookmark;
end; {if}
end; {if}
end else begin
btnBookmark.Enabled := false;
mnuPopBookmark.Enabled := false;
end; {if}
end; {SetBookmarkControls}
{--------------------------------------------------------------------------------------------------}
procedure TfrmFileBrowser.lvDrivesKeyDown(Sender : TObject; var Key : Word; Shift : TShiftState);
begin
with lvDrives do begin
case Key of
VK_LEFT : Key := 0;
VK_RIGHT : if (lvDrives.ViewStyle = vsReport) then begin
fFilename := lvDrives.Selected.SubItems.Strings[cColDriveName];
UpdateFileListView(fFileMask, fFileAttributes);
HighlightFocusedItem(lvFiles);
Key := 0;
end; {if}
VK_RETURN : begin
fFilename := lvDrives.Selected.SubItems.Strings[cColDriveName];
UpdateFileListView(fFileMask, fFileAttributes);
HighlightFocusedItem(lvFiles);
end;
end; {case}
end; {with}
end; {lvDrivesKeyDown}
{--------------------------------------------------------------------------------------------------}
{ajFileBrowser}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -