1 | unit ButtonBase;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, Graphics, Controls;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TButtonBase = class(TGraphicControl)
|
---|
10 | protected
|
---|
11 | FDown, FPermanent: boolean;
|
---|
12 | FGraphic: TBitmap;
|
---|
13 | // FDownSound, FUpSound: string;
|
---|
14 | ClickProc: TNotifyEvent;
|
---|
15 | DownChangedProc: TNotifyEvent;
|
---|
16 | procedure SetDown(x: boolean);
|
---|
17 | // procedure PlayDownSound;
|
---|
18 | // procedure PlayUpSound;
|
---|
19 | procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
|
---|
20 | x, y: integer); override;
|
---|
21 | procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
|
---|
22 | x, y: integer); override;
|
---|
23 | procedure MouseMove(Shift: TShiftState; x, y: integer); override;
|
---|
24 | private
|
---|
25 | Active: boolean;
|
---|
26 | public
|
---|
27 | constructor Create(aOwner: TComponent); override;
|
---|
28 | property Graphic: TBitmap read FGraphic write FGraphic;
|
---|
29 | // property DownSound: string read FDownSound write FDownSound;
|
---|
30 | // property UpSound: string read FUpSound write FUpSound;
|
---|
31 | published
|
---|
32 | property Visible;
|
---|
33 | property Down: boolean read FDown write SetDown;
|
---|
34 | property Permanent: boolean read FPermanent write FPermanent;
|
---|
35 | property OnClick: TNotifyEvent read ClickProc write ClickProc;
|
---|
36 | property OnDownChanged: TNotifyEvent read DownChangedProc
|
---|
37 | write DownChangedProc;
|
---|
38 | end;
|
---|
39 |
|
---|
40 | implementation
|
---|
41 |
|
---|
42 | // uses
|
---|
43 | // MMSystem;
|
---|
44 |
|
---|
45 | constructor TButtonBase.Create(aOwner: TComponent);
|
---|
46 | begin
|
---|
47 | inherited;
|
---|
48 | // FDownSound:='';
|
---|
49 | // FUpSound:='';
|
---|
50 | FGraphic := nil;
|
---|
51 | Active := false;
|
---|
52 | FDown := false;
|
---|
53 | FPermanent := false;
|
---|
54 | ClickProc := nil;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TButtonBase.MouseDown(Button: TMouseButton; Shift: TShiftState;
|
---|
58 | x, y: integer);
|
---|
59 | begin
|
---|
60 | Active := true;
|
---|
61 | MouseMove(Shift, x, y)
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure TButtonBase.MouseUp(Button: TMouseButton; Shift: TShiftState;
|
---|
65 | x, y: integer);
|
---|
66 | begin
|
---|
67 | if ssLeft in Shift then
|
---|
68 | exit;
|
---|
69 | MouseMove(Shift, x, y);
|
---|
70 | if Active and FDown then
|
---|
71 | begin
|
---|
72 | // PlayUpSound;
|
---|
73 | Active := false;
|
---|
74 | if FDown <> FPermanent then
|
---|
75 | begin
|
---|
76 | FDown := FPermanent;
|
---|
77 | Invalidate;
|
---|
78 | if @DownChangedProc <> nil then
|
---|
79 | DownChangedProc(self);
|
---|
80 | end;
|
---|
81 | if (Button = mbLeft) and (@ClickProc <> nil) then
|
---|
82 | ClickProc(self)
|
---|
83 | end
|
---|
84 | else
|
---|
85 | begin
|
---|
86 | // if FDown then PlayUpSound;
|
---|
87 | Active := false;
|
---|
88 | if FDown then
|
---|
89 | begin
|
---|
90 | FDown := false;
|
---|
91 | Invalidate;
|
---|
92 | if @DownChangedProc <> nil then
|
---|
93 | DownChangedProc(self);
|
---|
94 | end;
|
---|
95 | end
|
---|
96 | end;
|
---|
97 |
|
---|
98 | procedure TButtonBase.MouseMove(Shift: TShiftState; x, y: integer);
|
---|
99 | begin
|
---|
100 | if Active then
|
---|
101 | if (x >= 0) and (x < Width) and (y >= 0) and (y < Height) then
|
---|
102 | if (ssLeft in Shift) and not FDown then
|
---|
103 | begin
|
---|
104 | { PlayDownSound; }
|
---|
105 | FDown := true;
|
---|
106 | Paint;
|
---|
107 | if @DownChangedProc <> nil then
|
---|
108 | DownChangedProc(self);
|
---|
109 | end
|
---|
110 | else
|
---|
111 | else if FDown and not FPermanent then
|
---|
112 | begin
|
---|
113 | { PlayUpSound; }
|
---|
114 | FDown := false;
|
---|
115 | Paint;
|
---|
116 | if @DownChangedProc <> nil then
|
---|
117 | DownChangedProc(self);
|
---|
118 | end
|
---|
119 | end;
|
---|
120 |
|
---|
121 | procedure TButtonBase.SetDown(x: boolean);
|
---|
122 | begin
|
---|
123 | FDown := x;
|
---|
124 | Invalidate;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | // procedure TButtonBase.PlayDownSound;
|
---|
128 | // begin
|
---|
129 | // if DownSound<>'' then SndPlaySound(pchar(DownSound),SND_ASYNC)
|
---|
130 | // end;
|
---|
131 |
|
---|
132 | // procedure TButtonBase.PlayUpSound;
|
---|
133 | // begin
|
---|
134 | // if UpSound<>'' then SndPlaySound(pchar(UpSound),SND_ASYNC)
|
---|
135 | // end;
|
---|
136 |
|
---|
137 | end.
|
---|