⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 childfrm.pas

📁 === === === MiniHex 1.61 源程序说明 ============================== “$(MiniHex)Source”目录中的所有
💻 PAS
📖 第 1 页 / 共 4 页
字号:
begin
  Value := HexEdit.DataSize;
  S := '长度: ';
  if OptMgr.Values[SOptStatusLengthNumBase] = raDec then
    S := S + Format('%10dD', [Value])
  else
    S := S + '  ' + IntToHex(Value, 8) + 'H';
  StatusBar.Panels[0].Text := S;
end;

procedure TChildForm.ShowBlockInfo;
var
  S: string;
begin
  S := '块信息: ';
  if HexEdit.SelLength > 0 then
  begin
    if OptMgr.Values[SOptStatusBlockInfoNumBase] = raDec then
      S := S + Format('%10dD', [HexEdit.SelStart]) +
        ' - '+ Format('%10dD', [HexEdit.SelStart + HexEdit.SelLength - 1])
    else
      S := S + '  ' + IntToHex(HexEdit.SelStart, 8) + 'H' +
              ' - ' + IntToHex(HexEdit.SelStart + HexEdit.SelLength, 8) + 'H';
  end else
  begin
    S := S + '(未定义)'
  end;
  StatusBar.Panels[2].Text := S;
end;

procedure TChildForm.ShowCurValue;
var
  S: string;
  P: Pointer;
  Offset: Integer;
begin
  Offset := HexEdit.Offset;
  P := HexEdit.DataAddr + HexEdit.Offset;
  S := '';
  case TValueType(OptMgr.Values[SOptStatusCurValueType]) of
    vt8s:
      S := IntToStr(PShortInt(P)^);
    vt8u:
      S := IntToStr(PByte(P)^);
    vt16s:
      if Offset + 2 <= HexEdit.DataSize then
        S := IntToStr(PSmallInt(P)^);
    vt16u:
      if Offset + 2 <= HexEdit.DataSize then
        S := IntToStr(PWord(P)^);
    vt32s:
      if Offset + 4 <= HexEdit.DataSize then
        S := IntToStr(PLongInt(P)^);
    vt32u:
      if Offset + 4 <= HexEdit.DataSize then
        S := IntToStr(PLongWord(P)^);
    vtSingle:
      if Offset + 4 <= HexEdit.DataSize then
        S := Format('%2.8e', [PSingle(P)^]);
    vtDouble:
      if Offset + 8 <= HexEdit.DataSize then
        S := Format('%2.8e', [PDouble(P)^]);
  end;
  StatusBar.Panels[3].Text := '当前值: ' +  S;
end;

procedure TChildForm.ShowModifyFlag;
var
  S: string;
begin
  if HexEdit.Modified then S := '已修改'
  else S := '未修改';
  StatusBar.Panels[4].Text := S;
end;

function TChildForm.SaveToFile(const FileName: string): Boolean;
begin
  Result := True;
  try
    HexEdit.SaveToFile(FileName);
  except
    Result := False;
  end;
end;

procedure TChildForm.AdjustUndoRedoEnabled;
begin
  UndoAction.Enabled := HexEdit.CanUndo;
  RedoAction.Enabled := HexEdit.CanRedo;
  MainForm.UndoToolButton.Enabled := UndoAction.Enabled;
  MainForm.RedoToolButton.Enabled := RedoAction.Enabled;
end;

procedure TChildForm.AdjustClipboardEnabled;
begin
  CutAction.Enabled := HexEdit.SelLength > 0;
  CopyAction.Enabled := HexEdit.SelLength > 0;
  PasteAction.Enabled := HexEdit.CanPaste;
  MainForm.CutToolButton.Enabled := CutAction.Enabled;
  MainForm.CopyToolButton.Enabled := CopyAction.Enabled;
  MainForm.PasteToolButton.Enabled := PasteAction.Enabled;
end;

procedure TChildForm.AdjustBlockEnabled;
var
  BlockExists: Boolean;
begin
  BlockExists := HexEdit.SelLength > 0;
  WriteBlockAction.Enabled := BlockExists;
  DeleteBlockAction.Enabled := BlockExists;
  FillBlockAction.Enabled := BlockExists;
  AdjustBlockAction.Enabled := BlockExists;
  PosGotoBlockStartItem.Enabled := BlockExists;
  PosGotoBlockEndItem.Enabled := BlockExists;
end;

function TChildForm.CheckSelExists: Boolean;
begin
  Result := HexEdit.SelLength > 0;
  if not Result then
  begin
    MsgBox('块还没有定义!');
  end;
end;

procedure TChildForm.CreateBackup;
begin
  if OptMgr.Values[SOptCreateBak] and not FIsNewDoc then
    CreateBakFile(FFileName, 'bak');
end;

function TChildForm.NewBuffer(Size: Integer): Boolean;
begin
  Result := True;
  try
    HexEdit.DataSize := Size;
    HexEdit.Modified := False;
    FFileName := SDefaultFileName;
    FDisplayName := SDefaultFileName;
    Caption := SDefaultFileName;
    FIsNewDoc := True;
    ShowStatusInfo;
    FillDataInspector;
  except
    MsgBox('内存溢出错误。', MB_OK + MB_ICONERROR);
    Result := False;
  end;
end;

function TChildForm.LoadFromFile(const FileName: string): Boolean;
begin
  Result := True;
  try
    HexEdit.LoadFromFile(FileName);
    FFileName := GetFullFileName(FileName);
    FDisplayName := ExtractFileName(FFileName);
    Caption := FFileName;
    FIsNewDoc := False;
    ShowStatusInfo;
    FillDataInspector;
  except
    Result := False;
  end;
end;

function TChildForm.Save: Boolean;
var
  FileName: string;
  SaveDialog: TSaveDialog;
  R: Integer;
begin
  Result := False;
  BringToFront;

  if FIsNewDoc then
  begin
    SaveDialog := TSaveDialog.Create(Self);
    try
      SaveDialog.Title := '保存';
      SaveDialog.Filter := '所有文件(*.*)|*.*';
      SaveDialog.Options := SaveDialog.Options + [ofOverwritePrompt];
      if SaveDialog.Execute then
        FileName := SaveDialog.FileName
      else
        Exit;
    finally
      SaveDialog.Free;
    end;
  end else
  begin
    FileName := FFileName;
    if OptMgr.Values[SOptPromptSave] then
    begin
      R := MsgBox('确定要要保存吗?', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2);
      if R = ID_NO then Exit;
    end;
  end;

  CreateBackup;
  
  if SaveToFile(FileName) then
  begin
    FFileName := FileName;
    FDisplayName := ExtractFileName(FFileName);
    Caption := FFileName;
    HexEdit.Modified := False;
    ShowModifyFlag;
    MainForm.ChildRenameNotify(Self);
    MainForm.ChildSaveNotify(Self, FIsNewDoc);
    FIsNewDoc := False;
    Result := True;
  end else
  begin
    MsgBox(Format('文件“%s”保存失败。' + #13 +
      '此文件可能为只读,或者正被其它程序占用。', [FFileName]),
       MB_OK + MB_ICONERROR);
    Result := False;
  end;
end;

function TChildForm.SaveAs: Boolean;
var
  FileName: string;
  SaveDialog: TSaveDialog;
begin
  Result := False;

  SaveDialog := TSaveDialog.Create(Self);
  try
    SaveDialog.Title := '另存为';
    SaveDialog.Filter := '所有文件(*.*)|*.*';
    SaveDialog.Options := SaveDialog.Options + [ofOverwritePrompt];
    SaveDialog.FileName := FFileName;

    if SaveDialog.Execute then
    begin
      CreateBackup;

      FileName := SaveDialog.FileName;
      if SaveToFile(FileName) then
      begin
        FFileName := FileName;
        FDisplayName := ExtractFileName(FFileName);
        Caption := FFileName;
        HexEdit.Modified := False;
        ShowModifyFlag;
        MainForm.ChildRenameNotify(Self);
        MainForm.ChildSaveNotify(Self, True);
        FIsNewDoc := False;
        Result := True;
      end else
      begin
        MsgBox('无法保存,请检查磁盘是否可写!', MB_OK + MB_ICONERROR);
        Result := False;
      end;
    end;
  finally
    SaveDialog.Free;
  end;
end;

function TChildForm.Execute: Boolean;
var
  TempFileName: string;
  Hnd: THandle;
  Ret: Boolean;
  R: Integer;
begin
  Result := False;
  TempFileName := OptMgr.Values[SOptTempPath] + ExtractFileName(FFileName);
  if GetFullFileName(FFileName) = GetFullFileName(TempFileName) then
  begin
    MsgBox('无法执行该操作,请正确设置临时文件路径。');
    Exit;
  end;

  ShowProgressForm('正在为运行做准备...', True);
  try
    Ret :=  SaveToFile(TempFileName);
  finally
    ShowProgressForm('', False);
  end;

  if Ret then
  begin
    Hnd := ShellExecute(Application.Handle, 'Open', PChar(TempFileName), '', '', 1);
    if Hnd <= 32 then
      MsgBox(Format('文件 %s 执行失败。', [TempFileName]));
    R := MsgBox(Format('删除临时文件 %s 吗?', [TempFileName]), MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1);
    if R = ID_YES then
      if not DeleteFile(TempFileName) then
        MsgBox(Format('文件 %s 无法删除。', [TempFileName]));
    Result := (Hnd > 32);
  end else
  begin
    MsgBox(Format('无法建立临时文件 %s 。', [TempFileName]), MB_OK + MB_ICONERROR);
  end;
end;

function TChildForm.OpenWithEditor: Boolean;
var
  TempFileName: string;
  Ret: Boolean;
  ExecResult: Integer;
  R: Integer;
begin
  Result := False;
  TempFileName := OptMgr.Values[SOptTempPath] + ExtractFileName(FFileName);
  if GetFullFileName(FFileName) = GetFullFileName(TempFileName) then
  begin
    MsgBox('无法执行该操作,请正确设置临时文件路径。');
    Exit;
  end;

  ShowProgressForm('正在为运行做准备...', True);
  try
    Ret := SaveToFile(TempFileName);
  finally
    ShowProgressForm('', False);
  end;

  if Ret then
  begin
    ExecResult := WinExec(PChar(string(OptMgr.Values[SOptDefEditor]) + ' ''' + TempFileName + ''''), 1);
    if ExecResult <= 32 then
      MsgBox(Format('文件 %s 执行失败。', [TempFileName]));
    R := MsgBox(Format('删除临时文件 %s 吗?', [TempFileName]), MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1);
    if R = ID_YES then
      if not DeleteFile(TempFileName) then
        MsgBox(Format('文件 %s 无法删除。', [TempFileName]));
    Result := (ExecResult > 32);
  end else
  begin
    MsgBox(Format('无法建立临时文件 %s 。', [TempFileName]), MB_OK + MB_ICONERROR);
  end;
end;

function TChildForm.Print: Boolean;
var
  FormData: TPrintData;
  Lines: TStrings;
  I, X, Y: Integer;
begin
  Result := ShowPrintForm(FormData);
  if Result then
  begin
    Lines := TStringList.Create;
    try
      ShowProgressForm('正在打印...', True);
      HexEdit.GetEditorText(Lines, FormData.IncAddr, FormData.OnlyBlock);
      ShowProgressForm('', False);
      if Lines.Count = 0 then Exit;

      if FormData.PrintFileName then
      begin
        Lines.Insert(0, FFileName);
        Lines.Insert(0, '');
      end;

      try
        Printer.BeginDoc;
        try
          X := 10;
          Y := 10;
          for I := 0 to Lines.Count - 1 do
          begin
            Printer.Canvas.TextOut(X, Y, Lines[I]);
            Y := Y + Printer.Canvas.TextHeight(Lines[I]) + 5;
          end;
        finally
          Printer.EndDoc;
        end;
      except
        MsgBox('打印时出现错误,请检查打印机是否正确安装。', MB_OK + MB_ICONERROR);
      end;
    finally
      Lines.Free;
    end;
  end;
end;

function TChildForm.ExportToText: Boolean;
var
  FormData: TExportData;
  Lines: TStrings;
begin
  Result := ShowExportForm(FormData);
  if Result then
  begin
    Lines := TStringList.Create;
    try
      ShowProgressForm('正在导出...', True);
      HexEdit.GetEditorText(Lines, FormData.IncAddr, FormData.OnlyBlock);
      ShowProgressForm('', False);
      Lines.SaveToFile(FormData.FileName);
    finally
      Lines.Free;
    end;
  end;
end;

//-----------------------------------------------------------------------------
// 描述: 载入磁盘逻辑扇区数据
// 参数:
//   Drive       - 驱动器(如 'C', 'D')
//   SectorStart - 起始扇区号(0-based)
//   SectorCount - 扇区数量
//-----------------------------------------------------------------------------
function TChildForm.LoadFromLogicSector(Drive: Char; SectorStart, SectorCount: Integer): Boolean;
var
  DataStream: TMemoryStream;
begin
  DataStream := TMemoryStream.Create;
  try
    Result := DiskIOObject.ReadLogicalSector(Drive, SectorStart, SectorCount, DataStream);
    if Result then
    begin
      DataStream.Position := 0;
      HexEdit.LoadFromStream(DataStream);
      HexEdit.Modified := False;
      FFileName := SDefaultFileName;
      FDisplayName := Format('%s:扇区 %d - %d', [UpCase(Drive), SectorStart, SectorStart + SectorCount - 1]);
      Caption := FDisplayName;
      FIsNewDoc := True;
      ShowStatusInfo;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -