お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





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

"コンポのプロパティをコピーする"



みなさんこんにちは。

動的に生成したコンポのプロパティ設定はメンドイですね。
でも、そこに同じモノがあれば、published なプロパティを全部頂けます。



// -----------------------------------------------------------------

procedure SetProperties(Source, Dest: TComponent; TypeKinds: TTypeKinds;
  ExceptProp: String);
var
  PropList: PPropList;
  I, Count: Integer;
begin
  Count := GetPropList(Source.ClassInfo, TypeKinds, nil);
  GetMem(PropList, Count * SizeOf(PPropInfo));
  try
    GetPropList(Source.ClassInfo, TypeKinds, PropList);
    for I := 0 to Count - 1 do
      if Pos(PropList[I].Name, ExceptProp) = 0 then
      {$IFDEF VER90}
      case PropList[I].PropType.Kind of
      {$ELSE}
      case PropList[I].PropType^.Kind of
      {$ENDIF}
        tkFloat:
          SetFloatProp(
            Dest, PropList[I], GetFloatProp(Source, PropList[I]));
        tkInteger, tkChar, tkEnumeration, tkSet, tkClass:
          SetOrdProp(
            Dest, PropList[I], GetOrdProp(Source, PropList[I]));
        tkMethod:
          SetMethodProp(
            Dest, PropList[I], GetMethodProp(Source, PropList[I]));
        tkString:
          SetStrProp(
            Dest, PropList[I], GetStrProp(Source, PropList[I]));
        { ? tkArray, tkRecord, tkInterface, tkInt64, tkDynArray ? }
      end;
  finally
    FreeMem(PropList, Count * SizeOf(PPropInfo));
  end;
end;

// -----------------------------------------------------------------

// 使用例
// Memo を動的に生成して、色やフォント、イベントハンドラをも Memo1 と
// 同じに設定します。でも、Lines プロパティは要らないよねぇという場合。

procedure TForm1.Button1Click(Sender: TObject);
var
  Memo: TMemo;
begin
  Memo := TMemo.Create(Self);
  Memo.Parent := Self;
  SetProperties(Memo1, Memo, tkAny, 'Lines');
end;

{  tkAny           すべての型のプロパティ
   tkMethods       イベントに対するすべての型のプロパティ
   tkProperties    非イベントプロパティに対するすべての型

   をコピーすることが出来ます。by Inside Delphi  }

                                                   本田勝彦

Original document by 本田勝彦        氏 ID:(VYR01647)


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

Copyright 1996-2002 Delphi Users' Forum