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

📄 diskedirunt.pas

📁 D7设计的小巧磁盘编辑器 Window2000/Xp下的小巧磁盘编辑器
💻 PAS
字号:
unit diskedirunt;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, shellapi,FileCtrl, Grids, ComCtrls;

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    Button1: TButton;
    startsector: TSpinEdit;
    nsectors: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    StatusBar1: TStatusBar;
    StringGrid1: TStringGrid;
    Button2: TButton;
    Label3: TLabel;
    procedure FormShow(Sender: TObject);
    procedure DriveComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: String);
    procedure StringGrid1Exit(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure nsectorsChange(Sender: TObject);
    procedure startsectorChange(Sender: TObject);
  private
   driver:pchar;
   bytepersectors:integer;
   s,n:integer;
   seca,secb:array of integer;
   procedure BytesToGrid;
   procedure OnFocusChange;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  ColOld: Integer = 1;
  RowOld: Integer = 1;
  CellKeyPress: Integer;
  CharSellsCount:integer =17;
  hdevicehandle:thandle;
  Form1: TForm1;

implementation

uses wlt;

{$R *.dfm}

procedure TForm1.OnFocusChange;
var
  I: Integer;
  C: Char;
begin
  I := ColOld-1+(RowOld-1)*16;
  with StringGrid1 do
    if Length(Cells[ColOld,RowOld])>2 then begin
      Cells[ColOld,RowOld] := IntToHex(seca[i],2);
    end else
    try
      secb[i] := StrToInt('$'+Cells[ColOld,RowOld]);
      C := Chr(secb[i]);
      if c<' ' then c:='.';
      Cells[ColOld+17,RowOld] := C;
    except; end;
end;

procedure tform1.BytesToGrid;
var
  i,j,k:Integer;
  c: Char;
  fbuf:pchar;
begin
   n:=nsectors.Value;
   s:=startsector.Value;
   setlength(seca,n*bytepersectors);
   setlength(secb,n*bytepersectors);
   StatusBar1.Panels[1].Text := '';
   hDeviceHandle := CreateFile(driver, GENERIC_ALL, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
   if (hDeviceHandle<> INVALID_HANDLE_VALUE) then
   begin
   fbuf:=allocmem(n*bytepersectors);
   FileSeek(hDevicehandle,s*bytepersectors,0);
    if FileRead(hDevicehandle,fbuf[0],n*bytepersectors)<>n*bytepersectors then
           raise exception.create('读磁盘错误!');
    stringgrid1.Rowcount:=n*32+1;
    for i:=0 to ((n*bytepersectors) div 16)-1 do
        StringGrid1.Cells[0,i+1] := IntToHex(i*16,4)+':';
    for i:=0 to ((n*bytepersectors) div 16)-1 do
    for j:=1 to 16 do
      begin
      seca[j-1+16*i]:=integer(fbuf[j-1+16*i]);
      secb[j-1+16*i]:=seca[j-1+16*i];
      end;
    for i:=0 to ((n*bytepersectors) div 16)-1 do begin
    StringGrid1.Cells[0,i+1] := IntToHex(i*16,4)+':';
    for j:=1 to 16 do begin
      K := seca[j-1+16*i];
      StringGrid1.Cells[j,i+1]:=format('%.2x',[integer(fbuf[j-1+16*i])]);
      C :=chr(k);
      if c<' ' then c:='.';
      StringGrid1.Cells[j+17,i+1] := c;
      StatusBar1.Panels[1].Text := Format('逻辑磁盘 '+DriveComboBox1.Drive+'第 %D 扇区起连续'+inttostr(n)+'个扇区',[s]);
    end;
   end;
   freemem(fbuf);
   closehandle(hDeviceHandle);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  driver:=pchar('\\.\'+drivecombobox1.Drive+':');
  BytesToGrid;
end;

procedure TForm1.DriveComboBox1Change(Sender: TObject);
begin
  Button2Click(self);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
   startsector.Value:=0;
   nsectors.Value:=1;
   bytepersectors:=512;
   with StringGrid1 do begin
    ColWidths[0] := 50;
    for i := 17 to 33 do begin
      TabStops[i] := False;
      ColWidths[i] := 11;
    end;
    for i := 1 to 16 do Cells[i,0] := IntToHex(i-1,2);
    for i := 18 to 33 do Cells[i,0] := IntToHex(i-18,1);
    Cells[0,0] := '偏移';
 end;
end;

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if Key<>#8 then begin
    Key := UpCase(Key);
    if not (Key in ['0'..'9','A'..'F']) then Key := #0 else
    with StringGrid1 do
      if CellKeyPress>1 then begin
        if Length(Cells[Col,Row])>1 then begin
          if (Col+1+CharSellsCount)<ColCount then begin
            Col := Col+1;
            CellKeyPress := 1;
          end else begin
            if (Row+1)<RowCount then begin
              Col := 1; Row := Row+1;
              CellKeyPress := 1;
            end else Key := #0;
          end;
        end;
      end else Inc(CellKeyPress);
  end;
  if Key<>#0 then StatusBar1.Panels[1].Text := Format('修改逻辑磁盘 '+DriveComboBox1.Drive+'第 %D 扇区',[startsector.Value]);
end;


procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
var
  S: String;
  L: Integer;
begin
  with StringGrid1 do begin
    S := Cells[ColOld,RowOld];
    L := Length(S);
    case L of
      0: Cells[ColOld,RowOld] := '00';
      1: Cells[ColOld,RowOld] := '0'+S;
    end;
  end;
  ColOld := ACol;
  RowOld := ARow;
end;


procedure TForm1.StringGrid1Exit(Sender: TObject);
begin
with StringGrid1 do
    if Col<16 then
      Col := Col+1
    else begin
      Col := 1; Row := Row+1;
    end;
  OnFocusChange;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
if aCol>16 then CanSelect := False;
  CellKeyPress := 0;
  OnFocusChange;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
i:integer;
begin
with StringGrid1.Canvas do
    if (aCol>0) and (aRow>0) then begin
      case aCol of
        1..16: I := aCol-1+(aRow-1)*16;
        18..33: I := aCol-18+(aRow-1)*16;
      else I := -1;
      end;
      if I>=0 then
        if seca[I]<>secb[I] then begin
          Brush.Color := clred;
          FillRect(Rect);
          TextOut(Rect.Left+2,Rect.Top+2,StringGrid1.Cells[aCol,aRow]);
        end;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 driver:=pchar('\\.\'+drivecombobox1.Drive+':');
 BytesToGrid;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
p:pchar;
i,j:integer;
writesec:string;
begin
    if Form2.ShowModal <> mrOk then Exit;
    driver:=pchar('\\.\'+form2.DriveComboBox1.Drive+':');
    p:=allocmem(n*bytepersectors);
    setlength(writesec,n*bytepersectors);
    writesec:='';
    for i:=0 to ((n*bytepersectors) div 16)-1 do
    for j:=1 to 16 do
     writesec := writesec + chr(secb[j-1+16*i]);
     p:=pchar(writesec);
    hDeviceHandle := CreateFile(driver, GENERIC_ALL,
    FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
    if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
    begin
     FileSeek(hDevicehandle,form2.SpinEdit1.Value*bytepersectors,0);
    if FileWrite(hDevicehandle,p[0],n*bytepersectors)<>n*bytepersectors then
       raise exception.create('Write错误%d');
    StatusBar1.Panels[1].Text :='写逻辑磁盘 '+form2.DriveComboBox1.Drive+'成功!';
    closehandle(hDeviceHandle);
 end;
end;

procedure TForm1.nsectorsChange(Sender: TObject);
begin
   if nsectors.Value<1 then
   begin
   MessageBox(0, '扇区数不能为0!','小巧磁盘编辑器', MB_OK + MB_ICONERROR + MB_DEFBUTTON2 +
       MB_TOPMOST);
   nsectors.Value:=1;
   end;
   Button2Click(self);
end;

procedure TForm1.startsectorChange(Sender: TObject);
begin
Button2Click(self);
end;

end.

⌨️ 快捷键说明

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