📄 mainfrm.pas
字号:
Result := nil;
for I := 0 to MDIChildCount - 1 do
if MDIChildren[I] is TChildForm then
begin
Form := (MDIChildren[i] as TChildForm);
if CompareText(GetFullFileName(Form.FileName), FileName) = 0 then
begin
Result := Form;
Break;
end;
end;
end;
function TMainForm.OpenFile(FileName: string): Boolean;
var
Form: TChildForm;
R, FileSize: Integer;
begin
Result := False;
if not FileExists(FileName) then
begin
MsgBox(Format('文件 %s 不存在!', [FileName]));
end else
begin
FileSize := GetFileSize(FileName);
if FileSize = 0 then
MsgBox(Format('文件 “%s” 长度为 0 字节!', [FileName]))
else if FileSize >= WarningFileSize then
begin
R := MsgBox(Format('文件 “%s” 长度为 %s ,要打开它需要耗费大量的内存。' + #13 +
'确实要打开这个文件吗?', [ExtractFileName(FileName), GetSizeString(FileSize)]),
MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2);
if R = ID_NO then Exit;
end;
Form := FindChildForm(FileName);
if Form = nil then
begin
Form := TChildForm.Create(Self);
if Form.LoadFromFile(FileName) then
begin
FileName := Form.FileName;
Form.Caption := FileName;
MRUManager.AddMRU(FileName);
InitMRUMenuItem;
AddPageButton(Form, Form.DisplayName);
Result := True;
end else
begin
Form.Free;
MsgBox(Format('加载文件 %s 时出错,请检查文件是否存在或被其它应用程序占用。',
[FileName]), MB_OK + MB_ICONERROR);
end;
end else
begin
Form.BringToFront;
end;
end;
end;
procedure TMainForm.AdjustUIEnabled(Active: Boolean);
begin
SaveAction.Enabled := Active;
SaveAllAction.Enabled := Active;
SaveAsAction.Enabled := Active;
CloseAction.Enabled := Active;
CloseAllAction.Enabled := Active;
PrintAction.Enabled := Active;
FilePrintSetupItem.Enabled := Active;
ExecuteAction.Enabled := Active;
EditorAction.Enabled := Active;
PropertyAction.Enabled := Active;
ExportAction.Enabled := Active;
SearchToolButton.Enabled := Active;
GotoToolButton.Enabled := Active;
if not Active then
begin
CutToolButton.Enabled := Active;
CopyToolButton.Enabled := Active;
PasteToolButton.Enabled := Active;
UndoToolButton.Enabled := Active;
RedoToolButton.Enabled := Active;
end;
end;
procedure TMainForm.AddPageButton(Form: TChildForm; const DisplayName: string);
function CalcNewButtonRect: TRect;
var
I, MaxLeft: Integer;
Button: TSpeedButton;
begin
MaxLeft := -PageButtonWidth;
for I := 0 to PageHeaderPanel.ControlCount - 1 do
if PageHeaderPanel.Controls[I] is TSpeedButton then
begin
Button := (PageHeaderPanel.Controls[I] as TSpeedButton);
if Button.Left > MaxLeft then MaxLeft := Button.Left;
end;
Result.Left := MaxLeft + PageButtonWidth;
Result.Top := 2;
Result.Right := Result.Left + PageButtonWidth;
Result.Bottom := Result.Top + PageButtonHeight;
end;
function CreatePageButton: TSpeedButton;
var
Rect: TRect;
begin
Rect := CalcNewButtonRect;
Result := TSpeedButton.Create(Self);
with Result do
begin
Parent := PageHeaderPanel;
BoundsRect := Rect;
Flat := True;
Margin := 4;
GroupIndex := 1;
Glyph.LoadFromResourceName(HInstance, 'PageButtonDot');
Caption := RestrictStrWidth(DisplayName, Canvas, PageButtonWidth - 15);
if Caption <> DisplayName then
Hint := DisplayName;
Tag := Integer(Form);
OnClick := PageButtonClick;
end;
PageHeaderPanel.Width := Result.Left + Result.Width;
end;
begin
with CreatePageButton do
begin
Down := True;
end;;
end;
procedure TMainForm.DeletePageButton(Form: TChildForm);
var
I, Left: Integer;
Button: TSpeedButton;
begin
// 删除
Button := FindPageButton(Form);
if Button <> nil then
Button.Free;
// 重排
Left := 0;
for I := 0 to PageHeaderPanel.ControlCount - 1 do
if PageHeaderPanel.Controls[I] is TSpeedButton then
begin
Button := (PageHeaderPanel.Controls[I] as TSpeedButton);
Button.Left := Left;
Inc(Left, PageButtonWidth);
end;
PageHeaderScrollerResize(nil);
end;
function TMainForm.FindPageButton(Form: TChildForm): TSpeedButton;
var
I: Integer;
Button: TSpeedButton;
begin
Result := nil;
for I := 0 to PageHeaderPanel.ControlCount - 1 do
if PageHeaderPanel.Controls[I] is TSpeedButton then
begin
Button := (PageHeaderPanel.Controls[I] as TSpeedButton);
if Button.Tag = Integer(Form) then
begin
Result := Button;
Break;
end;
end;
end;
procedure TMainForm.ActivePageButton(Form: TChildForm);
var
Button: TSpeedButton;
begin
Button := FindPageButton(Form);
if Button <> nil then
Button.Down := True;
end;
procedure TMainForm.RenamePageButton(Form: TChildForm);
var
Button: TSpeedButton;
begin
Button := FindPageButton(Form);
if Button <> nil then
Button.Caption := ExtractFileName(Form.FileName);
end;
procedure TMainForm.ShowMDIClientEdge;
var
Style: Longint;
begin
if ClientHandle <> 0 then
begin
Style := GetWindowLong(ClientHandle, GWL_EXSTYLE);
if Style and WS_EX_CLIENTEDGE = 0 then
Style := Style or WS_EX_CLIENTEDGE
else
Exit;
SetWindowLong(ClientHandle, GWL_EXSTYLE, Style);
SetWindowPos(ClientHandle, 0, 0,0,0,0, SWP_FRAMECHANGED or SWP_NOACTIVATE or
SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;
procedure TMainForm.WMActivate(var Message: TWMActivate);
begin
case Message.Active of
WA_ACTIVE, WA_CLICKACTIVE:
begin
if GetCurDoc <> nil then
GetCurDoc.FormActivate(nil);
ShowKeyState;
end;
WA_INACTIVE:
begin
end;
end;
inherited;
end;
procedure TMainForm.WMOpenFile(var Message: TMessage);
var
S: array[0..MAX_PATH] of Char;
begin
ShowMainWindow;
GlobalGetAtomName(Message.LParam, S, MAX_PATH);
OpenFile(S);
end;
procedure TMainForm.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
case Msg.Message of
WM_DROPFILES:
DoFileDropMessage(Msg, Handled);
WM_KEYDOWN:
ShowKeyState;
end;
end;
procedure TMainForm.AppMinimize(Sender: TObject);
begin
FAppMinimized := True;
if TMinimizeMode(OptMgr.Values[SOptMinimizeMode]) = msSysTray then
begin
ShowWindow(Application.Handle, SW_HIDE);
SysTray.Active := True;
end;
end;
procedure TMainForm.AppRestore(Sender: TObject);
begin
FAppMinimized := False;
end;
procedure TMainForm.ToolsItemClick(Sender: TObject);
var
MenuItem: TMenuItem;
Idx, RunMode: Integer;
Item: TToolsItem;
Hnd: THandle;
begin
MenuItem := Sender as TMenuItem;
Idx := MenuItem.Tag;
Item := ToolsManager.Items[Idx];
if Item.RunMode = 0 then RunMode := SW_SHOWDEFAULT
else if Item.RunMode = 1 then RunMode := SW_SHOWMAXIMIZED
else RunMode := SW_SHOWMINIMIZED;
Hnd := ShellExecute(Application.Handle, 'Open', PChar(Item.CmdLine), PChar(Item.Para), PChar(Item.WorkDir), RunMode);
if Hnd <= 32 then
MsgBox(Format('常用工具 %s 执行失败。', [MenuItem.Caption]));
end;
procedure TMainForm.PageButtonClick(Sender: TObject);
var
Button: TSpeedButton;
begin
Button := Sender as TSpeedButton;
Button.Down := True;
TChildForm(Button.Tag).BringToFront;
end;
procedure TMainForm.DoFileDropMessage(var Msg: TMsg; var Handled: Boolean);
const
BufferLength = MAX_PATH;
var
DroppedFileName: string;
FileIndex: UINT;
DroppedFiles: Word;
FileNameBuffer: array[0..BufferLength] of Char;
begin
case Msg.Message of
WM_DROPFILES:
begin
FileIndex := $FFFFFFFF;
DroppedFiles := DragQueryFile(Msg.WParam, FileIndex, FileNameBuffer, BufferLength);
for FileIndex := 0 to (DroppedFiles - 1) do
begin
DragQueryFile(Msg.WParam, FileIndex, FileNameBuffer, BufferLength);
DroppedFileName := FileNameBuffer;
OpenFile(DroppedFileName);
end;
DragFinish(Msg.WParam);
Handled := True;
end;
end;
end;
procedure TMainForm.ShowMainWindow;
begin
if FAppMinimized then
begin
if SysTray.Active then
STPopupShowMainWinItemClick(nil)
else
Application.Restore;
end else
SetForegroundWindow(Handle);
end;
procedure TMainForm.AddToContentMenu(Add: Boolean);
var
R: TRegistry;
Key: string;
begin
R := TRegistry.Create;
R.RootKey := HKEY_CLASSES_ROOT;
Key := '\*\Shell\用 MiniHex 打开';
R.OpenKey(Key + '\Command', True);
if Add then
begin
R.WriteString('', Application.ExeName + ' "%1"');
end else
R.DeleteKey(Key);
R.Free;
end;
function TMainForm.GetMDIClientRect: TRect;
begin
Result.Left := 0;
Result.Top := 0;
Result.Right := ClientWidth - 4;
Result.Bottom := ClientHeight - CoolBar.Height -
TopSplitterPanel.Height - StatusBar.Height - 4;
end;
procedure TMainForm.ShowStatusMsg(Msg: string);
begin
if Msg = '' then Msg := Format('%s %s', [SSoftName, SVersion]);
StatusBar.Panels[0].Text := Msg;
StatusBar.Refresh;
end;
procedure TMainForm.ReDrawAllDoc;
var
I: Integer;
Form: TChildForm;
begin
for I := 0 to Self.MDIChildCount - 1 do
begin
if Self.MDIChildren[I] is TChildForm then
begin
Form := Self.MDIChildren[I] as TChildForm;
Form.ReDraw;
end;
end;
end;
procedure TMainForm.ChangeMaxUndo(Value: Integer);
var
I: Integer;
Form: TChildForm;
begin
for I := 0 to Self.MDIChildCount - 1 do
begin
if Self.MDIChildren[I] is TChildForm then
begin
Form := Self.MDIChildren[I] as TChildForm;
Form.HexEdit.UndoLimit := Value;
end;
end;
end;
procedure TMainForm.ArrangeForCompare(Form1, Form2: TChildForm);
var
I: Integer;
Form: TChildForm;
begin
for I := 0 to Self.MDIChildCount - 1 do
begin
if Self.MDIChildren[I] is TChildForm then
begin
Form := Self.MDIChildren[I] as TChildForm;
if (Form <> Form1) and (Form <> Form2) then
Form.WindowState := wsMinimized;
WinHoriTileActionExecute(nil);
end;
end;
end;
procedure TMainForm.ChildOpenNotify(Form: TChildForm);
begin
if OptMgr.Values[SOptAutoShowPageHdr] then
ShowPageHeader(GetDocCount >= 2);
AdjustUIEnabled(GetDocCount > 0);
end;
procedure TMainForm.ChildCloseNotify(Form: TChildForm);
var
LeftDocCount: Integer;
begin
LeftDocCount := GetDocCount - 1;
DeletePageButton(Form);
AdjustUIEnabled(LeftDocCount > 0);
if OptMgr.Values[SOptAutoShowPageHdr] then
ShowPageHeader(LeftDocCount > 1);
end;
procedure TMainForm.ChildActiveNotify(Form: TChildForm);
begin
ActivePageButton(Form);
end;
procedure TMainForm.ChildRenameNotify(Form: TChildForm);
begin
RenamePageButton(Form);
end;
procedure TMainForm.ChildSaveNotify(Form: TChildForm; AddToMRU: Boolean);
begin
if AddToMRU then
begin
MRUManager.AddMRU(Form.FileName);
InitMRUMenuItem;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
IniFileName: string;
begin
IniFileName := OptMgr.FileName;
MRUManager := TMRUMgr.Create(IniFileName);
ToolsManager := TToolsMgr.Create(IniFileName);
BkmkManager := TBkmkMgr.Create(GetBookmarkFileName);
DragAcceptFiles(Handle, True);
Application.OnMessage := AppMessage;
Application.OnMinimize := AppMinimize;
Application.OnRestore := AppRestore;
OptMgr.Load;
InitMRUMenuItem;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -