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