お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"バイナリーエディタ"






【タイトル】バイナリーエディタ。

 StringGridを使った簡単なバイナリーエディタです。
 フォームにStringGridとButtonを二つ配置してます。
 下記のソースコードは Delphi4 用です。

===================== Unit1.pasそのものです ==========================
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, DBCtrls, Grids, ExtCtrls;

const
  _ColCount=8;  // StrinGrid内のセルのX方向個数
  _RowCount=32; //          "       Y   "
  _DRowCount=8; // 表示する行数

type
  TForm1 = class(TForm)
    StringGridBinEdit: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure StringGridBinEditGetEditMask(Sender: TObject; ACol,
      ARow: Integer; var Value: String);
    procedure StringGridBinEditKeyPress(Sender: TObject; var Key:
Char);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    _Buff: array [0..255] of Byte;
    procedure _BinEdit_BuffGrid(var pbBuff:Array of Byte;
fDir:Integer);
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//-----------------------------------------------------------
// <EVENT> OnCreate
//-----------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  //---------------------------------------------
  // バッファに適当に値をセット
  //---------------------------------------------
  for i:=0 to 255 do _Buff[i]:=i and $ff;
  //---------------------------------------------
  // StringGridの初期化
  //---------------------------------------------
  with StringGridBinEdit do begin
    FixedCols:=0;
    FixedRows:=0;
    GridLineWidth:=0;
    ScrollBars:=ssNone;
    Options:=[ goEditing ];
    Font.Pitch:=fpFixed;

    ColCount:=_ColCount;                        // X方向の個数
    RowCount:=_RowCount;                        // Y方向の個数
    DefaultColWidth:=Font.Size * 4;             // 1升の幅
    DefaultRowHeight:=-(Font.Height)+8;         // 1升の高さ
    Width:=_ColCount * DefaultColWidth + 4;     // 全体の幅
    Height:=_DRowCount * DefaultRowHeight + 4;  // 全体の高さ
  end;
end;
//-----------------------------------------------------------
// <EVENT> Button1 Click(バッファの内容をStrinGridに出力する)
//-----------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
  _BinEdit_BuffGrid(_Buff,1);
end;
//-----------------------------------------------------------
// <EVENT> Button2 Click(StrinGridの内容をバッファに出力する)
//-----------------------------------------------------------
procedure TForm1.Button2Click(Sender: TObject);
begin
  _BinEdit_BuffGrid(_Buff,0);
end;
//-----------------------------------------------------------
// <EVENT> Grid OnGetEditMask
//-----------------------------------------------------------
procedure TForm1.StringGridBinEditGetEditMask(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  // このマスクをもっと旨く設定すればOnKeyPressは不要なはず
  Value:='>AA';
end;
//-----------------------------------------------------------
// <EVENT> Grid OnKeyPress
//-----------------------------------------------------------
procedure TForm1.StringGridBinEditKeyPress(Sender: TObject; var Key:
Char);
begin
  if UpCase(Key)>='G' then key:=#0;
end;
//-----------------------------------------------------------
// バッファ ←→ Grid間のデータ変換
//-----------------------------------------------------------
procedure TForm1._BinEdit_BuffGrid(var pbBuff:Array of Byte;
fDir:Integer);
var
  x,y,cnt: Integer;
  s: String;
begin
  cnt:=0;
  for y:=0 to _RowCount-1 do begin
    for x:=0 to _ColCount-1 do begin
      if 1=fDir then begin  // Grid ← バッファ
        StringGridBinEdit.Cells[x,y]:=IntToHex(pbBuff[cnt],2);
      end else begin        // バッファ ← Grid
        s:=Trim(StringGridBinEdit.Cells[x,y]);
        pbBuff[cnt]:=StrToInt('$'+s);
      end;
      Inc(cnt);
    end;
  end;
end;

end.

   99/2/18(Thu) 07:29pm クソじじい try@michinoku.ne.jp

Original document by クソじじい      氏 ID:(KEH00160)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum