📄 main.pas
字号:
unit Main;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Menus, SrchIni,
SrchU, ComCtrls, InitWiz;
type
TMainForm = class(TForm)
FileLB: TListBox;
PopupMenu1: TPopupMenu;
Font1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
FontDialog1: TFontDialog;
StatusBar: TStatusBar;
AlignPanel: TPanel;
ControlPanel: TPanel;
ParamsGB: TGroupBox;
LFileSpec: TLabel;
LToken: TLabel;
lPathName: TLabel;
EFileSpec: TEdit;
EToken: TEdit;
PathButton: TButton;
OptionsGB: TGroupBox;
cbCaseSensitive: TCheckBox;
cbFileNamesOnly: TCheckBox;
cbRecurse: TCheckBox;
SearchButton: TBitBtn;
CloseButton: TBitBtn;
PrintButton: TBitBtn;
PriorityButton: TBitBtn;
View1: TMenuItem;
EPathName: TEdit;
procedure SearchButtonClick(Sender: TObject);
procedure PathButtonClick(Sender: TObject);
procedure FileLBDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure Font1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure PrintButtonClick(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
procedure FileLBDblClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure PriorityButtonClick(Sender: TObject);
procedure ETokenChange(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FOldShowHint: TShowHintEvent;
procedure ReadIni;
procedure WriteIni;
procedure DoShowHint(var HintStr: string; var CanShow: Boolean;
var HintInfo: THintInfo);
procedure WMGetMinMaxInfo(var M: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
public
Running: Boolean;
SearchPri: integer;
SearchThread: TSearchThread;
procedure EnableSearchControls(Enable: Boolean);
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses Printers, ShellAPI, MemMap, FileCtrl, PriU;
procedure PrintStrings(Strings: TStrings);
{ This procedure prints all of the string in the Strings parameter }
var
Prn: TextFile;
i: word;
begin
if Strings.Count = 0 then // Are there strings?
begin
MessageDlg('No text to print!', mtInformation, [mbOk], 0);
Exit;
end;
AssignPrn(Prn); // assign Prn to printer
try
Rewrite(Prn); // open printer
try
for i := 0 to Strings.Count - 1 do // iterate over all strings
writeln(Prn, Strings.Strings[i]); // write to printer
finally
CloseFile(Prn); // close printer
end;
except
on EInOutError do
MessageDlg('Error Printing text.', mtError, [mbOk], 0);
end;
end;
procedure TMainForm.WMGetMinMaxInfo(var M: TWMGetMinMaxInfo);
begin
inherited;
// prevent user from sizing form too small
with M.MinMaxInfo^ do
begin
ptMinTrackSize.x := OptionsGB.Left + OptionsGB.Width - ParamsGB.Left + 10;
ptMinTrackSize.y := 200;
end;
end;
procedure TMainForm.EnableSearchControls(Enable: Boolean);
{ Enables or disables certain controls so options can't be modified }
{ while search is executing. }
begin
SearchButton.Enabled := Enable; // enabled/disable proper controls
cbRecurse.Enabled := Enable;
cbFileNamesOnly.Enabled := Enable;
cbCaseSensitive.Enabled := Enable;
PathButton.Enabled := Enable;
EPathName.Enabled := Enable;
EFileSpec.Enabled := Enable;
EToken.Enabled := Enable;
Running := not Enable; // set Running flag
ETokenChange(nil);
with CloseButton do
begin
if Enable then
begin // set props of Close/Stop button
Caption := '&Close';
Hint := 'Close Application';
end
else begin
Caption := '&Stop';
Hint := 'Stop Searching';
end;
end;
end;
procedure TMainForm.SearchButtonClick(Sender: TObject);
{ Called when Search button is clicked. Invokes search thread. }
begin
EnableSearchControls(False); // disable controls
FileLB.Clear; // clear listbox
{ start thread }
SearchThread := TSearchThread.Create(cbCaseSensitive.Checked,
cbFileNamesOnly.Checked, cbRecurse.Checked, EToken.Text,
EPathName.Text, EFileSpec.Text);
end;
procedure TMainForm.ETokenChange(Sender: TObject);
begin
SearchButton.Enabled := not Running and (EToken.Text <> '');
end;
procedure TMainForm.PathButtonClick(Sender: TObject);
{ Called when Path button is clicked. Allows user to choose new path. }
var
ShowDir: string;
begin
ShowDir := EPathName.Text;
if SelectDirectory(ShowDir, [], 0) then
EPathName.Text := ShowDir;
end;
procedure TMainForm.FileLBDblClick(Sender: TObject);
{ Called when user double-clicks in listbox. Loads file into IDE }
var
FileName: string;
Len: Integer;
begin
FileName := FileLB.Items[FileLB.ItemIndex];
{ make sure user clicked on a file... }
if (FileName <> '') and (Pos('File ', FileName) = 1) then
begin
{ Trim "File " and ":" from string }
FileName := Copy(FileName, 6, Length(FileName));
Len := Length(FileName);
if FileName[Len] = ':' then SetLength(FileName, Len - 1);
{ Open the project or file }
if CompareText(ExtractFileExt(FileName), '.DPR') = 0 then
ActionSvc.OpenProject(FileName, True)
else
ActionSvc.OpenFile(FileName);
end;
end;
procedure TMainForm.FileLBDrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
{ Called in order to owner draw listbox. }
var
CurStr: string;
begin
with FileLB do
begin
CurStr := Items.Strings[Index];
Canvas.FillRect(Rect); // clear out rect
if not cbFileNamesOnly.Checked then // if not filename only...
{ if current line is file name... }
if (Pos('File ', CurStr) = 1) and
(CurStr[Length(CurStr)] = ':') then
begin
Canvas.Font.Style := [fsUnderline]; // underline font
Canvas.Font.Color := clRed; // paint red
end
else
Rect.Left := Rect.Left + 15; // otherwise, indent
DrawText(Canvas.Handle, PChar(CurStr), Length(CurStr), Rect, dt_SingleLine);
end;
end;
procedure TMainForm.Font1Click(Sender: TObject);
{ Allows user to pick new font for listbox }
begin
{ Pick new listbox font }
if FontDialog1.Execute then
FileLB.Font := FontDialog1.Font;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
{ OnDestroy event handler for form }
begin
WriteIni;
end;
procedure TMainForm.FormCreate(Sender: TObject);
{ OnCreate event handler for form }
begin
Application.HintPause := 0; // don't wait to show hints
FOldShowHint := Application.OnShowHint; // set up hints
Application.OnShowHint := DoShowHint;
ReadIni; // read reg INI file
end;
procedure TMainForm.DoShowHint(var HintStr: string; var CanShow: Boolean;
var HintInfo: THintInfo);
{ OnHint event handler for Application }
begin
{ Display application hints on status bar }
StatusBar.Panels[0].Text := HintStr;
{ Don't show tool tip if we're over our own controls }
if (HintInfo.HintControl <> nil) and
(HintInfo.HintControl.Parent <> nil) and
((HintInfo.HintControl.Parent = ParamsGB) or
(HintInfo.HintControl.Parent = OptionsGB) or
(HintInfo.HintControl.Parent = ControlPanel)) then
CanShow := False;
FOldShowHint(HintStr, CanSHow, HintInfo);
end;
procedure TMainForm.PrintButtonClick(Sender: TObject);
{ Called when Print button is clicked. }
begin
if MessageDlg('Send search results to printer?', mtConfirmation,
[mbYes, mbNo], 0) = mrYes then
PrintStrings(FileLB.Items);
end;
procedure TMainForm.CloseButtonClick(Sender: TObject);
{ Called to stop thread or close application }
begin
// if thread is running then terminate thread
if Running then SearchThread.Terminate
// otherwise close app
else Close;
end;
procedure TMainForm.FormResize(Sender: TObject);
{ OnResize event handler. Centers controls in form. }
begin
{ divide status bar into two panels with a 1/3 - 2/3 split }
with StatusBar do
begin
Panels[0].Width := Width div 3;
Panels[1].Width := Width * 2 div 3;
end;
{ center controls in the middle of the form }
ControlPanel.Left := (AlignPanel.Width div 2) - (ControlPanel.Width div 2);
end;
procedure TMainForm.PriorityButtonClick(Sender: TObject);
{ Show thread priority form }
begin
ThreadPriWin.Show;
end;
procedure TMainForm.ReadIni;
{ Reads default values from Registry }
begin
with SrchIniFile do
begin
EPathName.Text := ReadString('Defaults', 'LastPath', 'C:\');
EFileSpec.Text := ReadString('Defaults', 'LastFileSpec', '*.*');
EToken.Text := ReadString('Defaults', 'LastToken', '');
cbFileNamesOnly.Checked := ReadBool('Defaults', 'FNamesOnly', False);
cbCaseSensitive.Checked := ReadBool('Defaults', 'CaseSens', False);
cbRecurse.Checked := ReadBool('Defaults', 'Recurse', False);
Left := ReadInteger('Position', 'Left', 100);
Top := ReadInteger('Position', 'Top', 50);
Width := ReadInteger('Position', 'Width', 510);
Height := ReadInteger('Position', 'Height', 370);
end;
end;
procedure TMainForm.WriteIni;
{ writes current settings back to Registry }
begin
with SrchIniFile do
begin
WriteString('Defaults', 'LastPath', EPathName.Text);
WriteString('Defaults', 'LastFileSpec', EFileSpec.Text);
WriteString('Defaults', 'LastToken', EToken.Text);
WriteBool('Defaults', 'CaseSens', cbCaseSensitive.Checked);
WriteBool('Defaults', 'FNamesOnly', cbFileNamesOnly.Checked);
WriteBool('Defaults', 'Recurse', cbRecurse.Checked);
WriteInteger('Position', 'Left', Left);
WriteInteger('Position', 'Top', Top);
WriteInteger('Position', 'Width', Width);
WriteInteger('Position', 'Height', Height);
end;
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
Application.OnShowHint := FOldShowHint;
MainForm := nil;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -