クラス名を知るには?

解決


ガリレオ  2007-11-20 23:10:58  No: 28581

クラスネームの派生元に指定したクラスネームがあるか
チェックする方法を教えてください。

例えば、

TPanel を派生元に TPanelEx を作ったとします。
プログラム中において

PE: TPanelEx;
とした場合に以下のように  TWinControl が派生元に有る場合に
処理を行いたいのです。

if PE.Get('TWinControl") = true then

もしくは、以下のように、PEが指定したプロパティを
持っているかどうかを判断して処理する方法を教えてください。

if PE.Get('TabOrder') = true then


そうねぇ  2007-11-21 00:02:20  No: 28582

type
 TPanelEx = class(TPanel)
 public
  function Get(CName: string): Boolean;
 end;

function TPanelEx.Get(CName: string): Boolean;
var
  aClass: TClass;
begin
  result := True;
  aClass := Self.ClassType;
  while aClass <> nil do begin
// if aClass.ClassName = CName then exit;
   if aClass.ClassNameIs(CName) then exit;
   aClass := aClass.ClassParent;
  end;
  result := False;
end;

var
 PE: TPanelEx;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PE := TPanelEx.Create(Self);
  if PE.Get('TWinControl') then ShowMessage('当たり');
end;


ガリレオ  2007-11-21 00:13:55  No: 28583

どうもありがとうございました。
それからすみません。

BevelWidth を変更しようとしたら、アクセスできないとでてきました。
よく見るとBevelWidth は  プロテクトされていました。

そこで、前回の質問の2
>PEが指定したプロパティを持っているかどうかを判断して処理する方法

もしくは、PEのクラスネーム "TPanel" を利用してPEをキャスト変換して
(Class(PE->ClassName))PE->BevelWidth
のようなことはできませんか?


KHE00221  2007-11-21 00:49:32  No: 28584

継承元のクラスで処理を分けるには以下のようにすれば可能です

 if TObject(Button1) is TGraphicControl then Caption := Caption + ' GraphicControl';
 if TObject(Button1) is TWinControl     then Caption := Caption + ' WinControl';
 if TObject(Button1) is TButtonControl  then Caption := Caption + ' ButtonControl';

 例では Button は TWinControl と TButtonControl を継承していますので
 Caption に WinControl と ButtonControl が表示されます

関数に Sender を与えているのならば

 if Sender is TButtonControl then Caption := Caption + ' ButtonControl';

のような形でも可能です


ガリレオ  2007-11-21 00:50:12  No: 28585

現在以下の状態で動作していますが、
アプリ終了時に「無効なポインタ操作」で
エラーが発生します。
以下コードの
>        TPanel(Compo).BorderWidth    := 10;
>        TTabSheet(Compo).BorderWidth := 10;
>        TRichEdit(Compo).BorderWidth := 10;
この部分を変更して、
BorderWidth  をもつ全てのコンポーネントの
設定を同じ値に変更するようにできませんか?

procedure TForm1.FormShow(Sender: TObject);
var
  Index: Integer;
  Compo:  TComponent;
  CName:  AnsiString;
  ClassRef: TClass;
begin
  for Index := 1 to ComponentCount do begin
    Compo := Components[Index - 1];
    CName := Compo.ClassName;
    ClassRef := Compo.ClassType;
    while ClassRef <> Nil do begin
      if ClassRef.ClassName = 'TWinControl' then begin
        RichEdit1.Lines.Add(CName);
        TPanel(Compo).BorderWidth    := 10;
        TTabSheet(Compo).BorderWidth := 10;
        TRichEdit(Compo).BorderWidth := 10;
      end;
      ClassRef := ClassRef.ClassParent;
    end;
  end;
end;


KHE00221  2007-11-21 01:32:29  No: 28586

こんなのはどうでしょう?

  TWinControlEx = class(TWinControl)
  public
    property BorderWidth;
  end;

  として

var
    I : Integer;
begin
    for I:=0 to Form1.ComponentCount -1 do
    begin
      if Form1.Components[I] is TWinControl then
      begin
        TWinControlEx(Form1.Components[I]).BorderWidth := 10;
       end;
    end;
end;


ガリレオ  2007-11-21 20:35:02  No: 28587

ありがとうございます。
うまくいきました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加