本例:
代碼檔案:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}{擷取 Button1 的父類}procedure TForm1.Button1Click(Sender: TObject);var ClassRef: TClass;begin ListBox1.Clear; ClassRef := Sender.ClassType; while ClassRef nil do begin ListBox1.Items.Add(ClassRef.ClassName); ClassRef := ClassRef.ClassParent; end;end;{擷取 ListBox1 的父類}procedure TForm1.Button2Click(Sender: TObject);var ClassRef: TClass;begin ListBox1.Clear; ClassRef := ListBox1.ClassType; while ClassRef nil do begin ListBox1.Items.Add(ClassRef.ClassName); ClassRef := ClassRef.ClassParent; end;end;{擷取當前 Form 的父類}procedure TForm1.Button3Click(Sender: TObject);var ClassRef: TClass;begin ListBox1.Clear; ClassRef := Self.ClassType; while ClassRef nil do begin ListBox1.Items.Add(ClassRef.ClassName); ClassRef := ClassRef.ClassParent; end;end;end.
表單檔案:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 147 ClientWidth = 282 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 0 Top = 0 Width = 161 Height = 147 Align = alLeft ItemHeight = 13 TabOrder = 0 ExplicitHeight = 155 end object Button1: TButton Left = 184 Top = 24 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 184 Top = 64 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 2 OnClick = Button2Click end object Button3: TButton Left = 184 Top = 104 Width = 75 Height = 25 Caption = 'Button3' TabOrder = 3 OnClick = Button3Click endend