回复 "刘那" 的问题: 你的例子挺好玩的, 我模拟做了一下

问题来源: http://www.cnblogs.com/del/archive/2008/05/14/1089344.html#1196271

本例效果图:

回复 "刘那" 的问题: 你的例子挺好玩的, 我模拟做了一下

代码文件:

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls, Grids;



type

  TForm1 = class(TForm)

    LabeledEdit1: TLabeledEdit;

    LabeledEdit2: TLabeledEdit;

    LabeledEdit3: TLabeledEdit;

    Button1: TButton;

    procedure FormCreate(Sender: TObject);

    procedure LabeledEdit2KeyPress(Sender: TObject; var Key: Char);

    procedure LabeledEdit3KeyPress(Sender: TObject; var Key: Char);

    procedure LabeledEdit1Change(Sender: TObject);

    procedure Button1Click(Sender: TObject);

  end;



  {自定义的类, 用于显示字符列表}

  TMyClass = class(TStringGrid)

  private

    FCharList: string;

    procedure SetCharList(const str: string);

  public

    constructor Create(AOwner: TComponent); override;

    procedure SetChar(ind: Integer; char: Char);

    property CharList: string read FCharList write SetCharList;

    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



var

  MyTable: TMyClass;



procedure TForm1.FormCreate(Sender: TObject);

begin

  LabeledEdit1.EditLabel.Caption := '字符列表';

  LabeledEdit2.EditLabel.Caption := '插入位置(数字)';

  LabeledEdit3.EditLabel.Caption := '要插入的字符(字母)';



  MyTable := TMyClass.Create(Self);

  MyTable.Parent := Self;

  MyTable.Left := 8;

  MyTable.Top := 12;

end;



procedure TForm1.LabeledEdit2KeyPress(Sender: TObject; var Key: Char);

begin

  if not (Key in ['0'..'9']) then Key := #0;

end;



procedure TForm1.LabeledEdit3KeyPress(Sender: TObject; var Key: Char);

begin

  if not (Key in ['A'..'Z', 'a'..'z']) then

    Key := #0

  else TLabeledEdit(Sender).SelectAll;

end;



procedure TForm1.LabeledEdit1Change(Sender: TObject);

begin

  MyTable.CharList := LabeledEdit1.Text;

end;



procedure TForm1.Button1Click(Sender: TObject);

var

  i: Integer;

  c: Char;

begin

  if LabeledEdit3.Text = '' then Exit;

  i := StrToIntDef(LabeledEdit2.Text, 0);

  c := LabeledEdit3.Text[1];

  MyTable.SetChar(i, c);

  LabeledEdit1.Text := MyTable.FCharList;

end;





{ 下面是 TMyClass 的代码 }



//建立时调整 StringGrid 的外观

constructor TMyClass.Create(AOwner: TComponent);

begin

  inherited;

  FixedCols := 0;

  FixedRows := 0;

  RowCount := 1;

  ColCount := 3;

  Font.Size := 10;

  Font.Color := clRed;

  Font.Style := [fsBold];



  DefaultColWidth := DefaultRowHeight;

  Height := DefaultRowHeight + 4;

  Width := (DefaultColWidth + 1) * ColCount + 3;

end;



//赋予字符列表

procedure TMyClass.SetCharList(const str: string);

var

  i: Integer;

begin

  if str = '' then Rows[0][0] := '' else

  begin

    FCharList := str;

    ColCount := Length(str);

    for i := 0 to ColCount - 1 do Rows[0][i] := str[i+1];

    Width := (DefaultColWidth + 1) * ColCount + 3;

  end;

end;



//让字符居中

procedure TMyClass.DrawCell(ACol, ARow: Integer; ARect: TRect;

  AState: TGridDrawState);

var

  str: string;

begin

  inherited;

  Canvas.FillRect(ARect);

  str := Cells[ACol,ARow];

  Canvas.TextRect(ARect, str, [tfSingleLine, tfCenter, tfVerticalCenter]);

end;



//插入字符

procedure TMyClass.SetChar(ind: Integer; char: Char);

var

  s1,s2: string;

  i: Integer;

begin

  if ind > Length(FCharList) then

    i := Length(FCharList)

  else i := ind;



  s1 := Copy(FCharList, 0, i);

  s2 := Copy(FCharList, i+1, Length(FCharList));

  FCharList := s1 + char + s2;

  SetCharList(FCharList);

end;



end.


 
   
窗体设计:

object Form1: TForm1

  Left = 384

  Top = 206

  Caption = 'Form1'

  ClientHeight = 202

  ClientWidth = 255

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  Position = poDesigned

  OnCreate = FormCreate

  PixelsPerInch = 96

  TextHeight = 13

  object LabeledEdit1: TLabeledEdit

    Left = 8

    Top = 75

    Width = 227

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit1'

    TabOrder = 0

    OnChange = LabeledEdit1Change

  end

  object LabeledEdit2: TLabeledEdit

    Left = 8

    Top = 123

    Width = 121

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit2'

    TabOrder = 1

    OnKeyPress = LabeledEdit2KeyPress

  end

  object LabeledEdit3: TLabeledEdit

    Left = 8

    Top = 167

    Width = 121

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit3'

    TabOrder = 2

    OnKeyPress = LabeledEdit3KeyPress

  end

  object Button1: TButton

    Left = 160

    Top = 165

    Width = 75

    Height = 25

    Caption = 'Button1'

    TabOrder = 3

    OnClick = Button1Click

  end

end


 
   

你可能感兴趣的:(例子)