//==============================================================================
//复制控件(支持复制子控件和事件)**********************************************
//==============================================================================
function CopyComponent(Source: TComponent; Parent: TComponent; Owner: TComponent): Boolean; { 返回复制元件是否成功 }
var
  vMemoryStream: TMemoryStream;
  vComponent: TComponent;
  vPropList: PPropList;
  vPropInfo: PPropInfo;
  vReader: TReader;
  i: Integer;
begin
  Result := false;
  vMemoryStream := TMemoryStream.Create;
  vReader := TReader.Create(vMemoryStream, 256);
  try
    try
      vMemoryStream.WriteComponent(Source);
      vMemoryStream.Position := 0;
      vReader.Parent := Parent;
      vComponent := vReader.ReadRootComponent(nil);
      for i:=1 to GetPropList(Source, vPropList) do
      begin
        vPropInfo := vPropList^[i-1];
        if vPropInfo^.PropType^.Kind = tkMethod
        then SetMethodProp(vComponent, vPropInfo^.Name, GetMethodProp(Source, vPropInfo^.Name));
      end;
    except
      Result := true;
      Exit;
    end;
  finally
    vReader.Free;
    vMemoryStream.Free;
  end;
  if Source is TWinControl then
    for i:=1 to TWinControl(Source).ControlCount do
      if not CopyComponent(TWinControl(Source).Controls[i-1], vComponent, Owner) then Exit;
  Result := true;
end;
exam:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm_Main.Button16Click(Sender: TObject);
var Rect: TRect;
    i: integer;
begin
  StringGrid.Canvas.Brush.Style := bsBDiagonal;
  StringGrid.Canvas.Font.Color := clBlack;  StringGrid.ColCount := Table1.FieldCount;
  for i:=1 to StringGrid.ColCount do
  begin
    Rect := StringGrid.CellRect(i-1,0);
    DrawText(StringGrid.Canvas.Handle,PChar(Table1.Fields[i-1].FieldName),Length(Table1.Fields[i-1].FieldName),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
  end;
end;

解决方案 »

  1.   

    exam://~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    procedure TForm_Main.Button16Click(Sender: TObject);
    var Rect: TRect;
        i: integer;
    begin
      StringGrid.Canvas.Brush.Style := bsBDiagonal;
      StringGrid.Canvas.Font.Color := clBlack;  StringGrid.ColCount := Table1.FieldCount;
      for i:=1 to StringGrid.ColCount do
      begin
        Rect := StringGrid.CellRect(i-1,0);
        DrawText(StringGrid.Canvas.Handle,PChar(Table1.Fields[i-1].FieldName),Length(Table1.Fields[i-1].FieldName),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
      end;
    end;
    //==============================================================================
    //复制控件(支持复制子控件和事件)**********************************************
    //==============================================================================
    function CopyComponent(Source: TComponent; Parent: TComponent; Owner: TComponent): Boolean; { 返回复制元件是否成功 }
    var
      vMemoryStream: TMemoryStream;
      vComponent: TComponent;
      vPropList: PPropList;
      vPropInfo: PPropInfo;
      vReader: TReader;
      i: Integer;
    begin
      Result := false;
      vMemoryStream := TMemoryStream.Create;
      vReader := TReader.Create(vMemoryStream, 256);
      try
        try
          vMemoryStream.WriteComponent(Source);
          vMemoryStream.Position := 0;
          vReader.Parent := Parent;
          vComponent := vReader.ReadRootComponent(nil);
          for i:=1 to GetPropList(Source, vPropList) do
          begin
            vPropInfo := vPropList^[i-1];
            if vPropInfo^.PropType^.Kind = tkMethod
            then SetMethodProp(vComponent, vPropInfo^.Name, GetMethodProp(Source, vPropInfo^.Name));
          end;
        except
          Result := true;
          Exit;
        end;
      finally
        vReader.Free;
        vMemoryStream.Free;
      end;
      if Source is TWinControl then
        for i:=1 to TWinControl(Source).ControlCount do
          if not CopyComponent(TWinControl(Source).Controls[i-1], vComponent, Owner) then Exit;
      Result := true;
    end;
      

  2.   

    仅供参考,Table1的Database是BCDEMOS,表是animals.dbfunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids, DBGrids, Db, DBTables;type
      TForm1 = class(TForm)
        DataSource1: TDataSource;
        Table1: TTable;
        DBGrid1: TDBGrid;
        procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
        procedure DBGrid1CellClick(Column: TColumn);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      R: TRect;
    begin
      if Column.FieldName = 'SIZE' then
      begin
        with DBGrid1.Canvas do
        begin
          FillRect(Rect);
          R := Rect;
          InflateRect(R, -2, -2);
          if Column.Field.AsInteger mod 2 = 0 then
            DrawFrameControl(Handle, R, DFC_BUTTON, DFCS_BUTTONCHECK or DFCS_CHECKED)
          else
            DrawFrameControl(Handle, R, DFC_BUTTON, DFCS_BUTTONCHECK);
        end;
      end;
    end;procedure TForm1.DBGrid1CellClick(Column: TColumn);
    begin
      if Column.FieldName = 'SIZE' then
      begin
        Table1.Edit;
        if Column.Field.AsInteger mod 2 = 0 then
          Table1.FieldByName('SIZE').AsInteger := Table1.FieldByName('SIZE').AsInteger + 1
        else
          Table1.FieldByName('SIZE').AsInteger := Table1.FieldByName('SIZE').AsInteger - 1;
        Table1.Post;
      end;
    end;end.