home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1996 August / VPR9608A.BIN / del20try / install / data.z / DEMOLBL.PAS < prev    next >
Pascal/Delphi Source File  |  1996-05-08  |  3KB  |  101 lines

  1. { Demonstrates FreeNotication to safely link to controls in other forms
  2.   through form linking and demonstrates preventing a component from being used
  3.   in form inheritence }
  4.  
  5. unit DemoLbl;
  6.  
  7. interface
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  11.  
  12. type
  13.   TDemoLabel = class(TGraphicControl)
  14.   private
  15.     FFocusControl: TWinControl;
  16.     procedure SetFocusControl(Value: TWinControl);
  17.     procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  18.     procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  19.   protected
  20.     procedure Notification(AComponent: TComponent;
  21.       Operation: TOperation); override;
  22.     procedure Paint; override;
  23.   public
  24.     constructor Create(AOwner: TComponent); override;
  25.   published
  26.     property Caption;
  27.     property Color;
  28.     property FocusControl: TWinControl read FFocusControl write SetFocusControl;
  29.     property Font;
  30.     property ParentColor;
  31.     property ParentFont;
  32.   end;
  33.  
  34. procedure Register;
  35.  
  36. implementation
  37.  
  38. { TDemoLabel }
  39.  
  40. constructor TDemoLabel.Create(AOwner: TComponent);
  41. begin
  42.   inherited Create(AOwner);
  43.   FComponentStyle := FComponentStyle - [csInheritable];
  44. end;
  45.  
  46. procedure TDemoLabel.Notification(AComponent: TComponent;
  47.   Operation: TOperation);
  48. begin
  49.   inherited Notification(AComponent, Operation);
  50.   if (Operation = opRemove) and (AComponent = FFocusControl) then
  51.     FFocusControl := nil;
  52. end;
  53.  
  54. procedure TDemoLabel.SetFocusControl(Value: TWinControl);
  55. begin
  56.   FFocusControl := Value;
  57.  
  58.   { Calling FreeNotification ensures that this component will receive an
  59.     opRemove when Value is either removed from its owner or when it is
  60.     destroyed. }
  61.  
  62.   Value.FreeNotification(Value);
  63. end;
  64.  
  65. procedure TDemoLabel.Paint;
  66. var
  67.   Rect: TRect;
  68. begiN
  69.   Rect := ClientRect;
  70.   Canvas.Font := Font;
  71.   Canvas.Brush.Color := Color;
  72.   Canvas.FillRect(Rect);
  73.   DrawText(Canvas.Handle, PChar(Caption), Length(Caption), Rect,
  74.     DT_EXPANDTABS or DT_WORDBREAK or DT_LEFT);
  75. end;
  76.  
  77. procedure TDemoLabel.CMDialogChar(var Message: TCMDialogChar);
  78. begin
  79.   if (FFocusControl <> nil) and Enabled and
  80.     IsAccel(Message.CharCode, Caption) then
  81.     with FFocusControl do
  82.       if CanFocus then
  83.       begin
  84.         SetFocus;
  85.         Message.Result := 1;
  86.       end;
  87. end;
  88.  
  89. procedure TDemoLabel.CMTextChanged(var Message: TMessage);
  90. begin
  91.   inherited;
  92.   Invalidate;
  93. end;
  94.  
  95. procedure Register;
  96. begin
  97.   RegisterComponents('Samples', [TDemoLabel]);
  98. end;
  99.  
  100. end.
  101.