unit testLabel2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, StdCtrls; type TtestLabel = class(TLabel) public FoldWndProc:TWndMethod; constructor Create(AOwner: TComponent);override; destructor Destroy;override; procedure MyWndProc(var Message: TMessage); end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [TtestLabel]); end; constructor TtestLabel.Create(AOwner: TComponent); begin inherited; if (AOwner is TCustomForm) then begin FOldWndProc := TCustomForm(AOwner).WindowProc; TCustomForm(AOwner).WindowProc := MyWndProc; end else begin FOldWndProc := nil; end; end; //------------------------------- //メソッドが一致するかどうか調べる関数 function SameMethod(const A, B: TWndMethod): Boolean; begin if (TMethod(A).Code = TMethod(B).Code) and (TMethod(A).Data = TMethod(B).Data) then Result := True else Result := False; end; destructor TtestLabel.Destroy; begin if SameMethod( Self.MyWndProc, TCustomForm(Owner).WindowProc ) then begin TCustomForm(Owner).WindowProc := FoldWndProc end else begin raise EComponentError.Create('サブクラス化解除失敗'); end; inherited; end; procedure TtestLabel.MyWndProc(var Message: TMessage); begin case Message.Msg of WM_CHAR: begin //コンポーネントの動作チェック用 text:=Char(Message.WPARAM); if assigned(FoldWndProc) then FoldWndProc(Message); end; else if assigned(FoldWndProc) then FoldWndProc(Message); end; end; end.