File: BaseComp.pas

package info (click to toggle)
morserunner 0.9%2Blinux-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,384 kB
  • sloc: pascal: 5,344; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//------------------------------------------------------------------------------
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
unit BaseComp;

{$MODE Delphi}

interface

uses
  LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Controls, Forms;

type
  {this component adds a window handle to the standard TControl
  in order to receive windows messages and makes sure that
  DoSetEnabled is called only at run time}

  TBaseComponent = class (TComponent)
  private
    FHandle: THandle;
    FEnabled : boolean;
    procedure SetEnabled(AEnabled: boolean);
    procedure WndProc(var Message: TMessage);
    //procedure WMQueryEndSession(var Message: TMessage); message WM_QUERYENDSESSION;
  protected
    procedure DoSetEnabled(AEnabled: boolean); virtual;
    procedure Loaded; override;
    property Handle: THandle read FHandle;
  public
    property Enabled: boolean read FEnabled write SetEnabled default false;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;


implementation

 { TBaseComponent }

constructor TBaseComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FHandle := 0;
  FEnabled := false;
end;


destructor TBaseComponent.Destroy;
begin
  Enabled := false;
  inherited Destroy;
end;


procedure TBaseComponent.Loaded;
begin
  inherited Loaded;

  if FEnabled then
    begin
    FEnabled := false;
    SetEnabled(true);
    end;
end;


procedure TBaseComponent.WndProc(var Message: TMessage);
begin
  try
    Dispatch(Message);
  except
    Application.HandleException(Self);
  end;
end;

//procedure TBaseComponent.WMQueryEndSession(var Message: TMessage);
//begin
//  try Enabled := false; except; end;
//  inherited;
//  Message.Result := integer(true);
//end;

procedure TBaseComponent.SetEnabled (AEnabled: boolean);
begin
  if (not (csDesigning in ComponentState)) and
     (not (csLoading in ComponentState)) and
     (AEnabled <> FEnabled)
    then DoSetEnabled(AEnabled);
  FEnabled := AEnabled;
end;


procedure TBaseComponent.DoSetEnabled (AEnabled: boolean);
begin
  if AEnabled
    then
      FHandle := AllocateHwnd(WndProc)
    else
      begin
      if FHandle <> 0 then DeallocateHwnd(FHandle);
      FHandle := 0;
      end;
end;


end.