source: branches/delphi/Messg.pas

Last change on this file was 6, checked in by chronos, 7 years ago
  • Modified: Formated all project source files using Delphi formatter as original indentation and other formatting was really bad.
File size: 8.1 KB
Line 
1{$INCLUDE switches}
2unit Messg;
3
4interface
5
6uses
7 ScreenTools,
8
9 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ButtonBase,
10 ButtonA,
11 ButtonB, Area;
12
13const
14 WM_PLAYSOUND = WM_USER;
15
16type
17 TDrawDlg = class(TForm)
18 constructor Create(AOwner: TComponent); override;
19 public
20 procedure SmartInvalidate; virtual;
21 protected
22 TitleHeight: integer;
23 // defines area to grip the window for moving (from top)
24 procedure InitButtons();
25 procedure OnEraseBkgnd(var m: TMessage); message WM_ERASEBKGND;
26 procedure OnHitTest(var Msg: TMessage); message WM_NCHITTEST;
27 end;
28
29 TBaseMessgDlg = class(TDrawDlg)
30 procedure FormCreate(Sender: TObject);
31 procedure FormPaint(Sender: TObject);
32 public
33 MessgText: string;
34 protected
35 Lines, TopSpace: integer;
36 procedure SplitText(preview: boolean);
37 procedure CorrectHeight;
38 end;
39
40 TMessgDlg = class(TBaseMessgDlg)
41 Button1: TButtonA;
42 Button2: TButtonA;
43 procedure FormCreate(Sender: TObject);
44 procedure FormPaint(Sender: TObject);
45 procedure FormShow(Sender: TObject);
46 procedure Button1Click(Sender: TObject);
47 procedure Button2Click(Sender: TObject);
48 procedure FormKeyPress(Sender: TObject; var Key: char);
49 public
50 Kind: integer;
51 OpenSound: string;
52 private
53 procedure OnPlaySound(var Msg: TMessage); message WM_PLAYSOUND;
54 end;
55
56const
57 // message kinds
58 mkOK = 1;
59 mkOKCancel = 2;
60 mkYesNo = 3;
61
62 Border = 3;
63 MessageLineSpacing = 20;
64
65var
66 MessgDlg: TMessgDlg;
67
68procedure SimpleMessage(SimpleText: string);
69procedure SoundMessage(SimpleText, SoundItem: string);
70
71implementation
72
73{$R *.DFM}
74
75constructor TDrawDlg.Create(AOwner: TComponent);
76begin
77 inherited;
78 TitleHeight := 0;
79end;
80
81procedure TDrawDlg.OnEraseBkgnd(var m: TMessage);
82begin
83end;
84
85procedure TDrawDlg.OnHitTest(var Msg: TMessage);
86var
87 i: integer;
88 ControlBounds: TRect;
89begin
90 if BorderStyle <> bsNone then
91 inherited
92 else
93 begin
94 if integer(Msg.LParamHi) >= Top + TitleHeight then
95 Msg.result := HTCLIENT
96 else
97 begin
98 for i := 0 to ControlCount - 1 do
99 if Controls[i].Visible then
100 begin
101 ControlBounds := Controls[i].BoundsRect;
102 if (integer(Msg.LParamLo) >= Left + ControlBounds.Left) and
103 (integer(Msg.LParamLo) < Left + ControlBounds.Right) and
104 (integer(Msg.LParamHi) >= Top + ControlBounds.Top) and
105 (integer(Msg.LParamHi) < Top + ControlBounds.Bottom) then
106 begin
107 Msg.result := HTCLIENT;
108 exit;
109 end;
110 end;
111 Msg.result := HTCAPTION
112 end;
113 end
114end;
115
116procedure TDrawDlg.InitButtons();
117var
118 cix: integer;
119 // ButtonDownSound, ButtonUpSound: string;
120begin
121 // ButtonDownSound:=Sounds.Lookup('BUTTON_DOWN');
122 // ButtonUpSound:=Sounds.Lookup('BUTTON_UP');
123 for cix := 0 to ComponentCount - 1 do
124 if Components[cix] is TButtonBase then
125 begin
126 TButtonBase(Components[cix]).Graphic := GrExt[HGrSystem].Data;
127 // if ButtonDownSound<>'*' then
128 // DownSound:=HomeDir+'Sounds\'+ButtonDownSound+'.wav';
129 // if ButtonUpSound<>'*' then
130 // UpSound:=HomeDir+'Sounds\'+ButtonUpSound+'.wav';
131 if Components[cix] is TButtonA then
132 TButtonA(Components[cix]).Font := UniFont[ftButton];
133 if Components[cix] is TButtonB then
134 TButtonB(Components[cix]).Mask := GrExt[HGrSystem].Mask;
135 end;
136end;
137
138procedure TDrawDlg.SmartInvalidate;
139var
140 i: integer;
141 r0, r1: HRgn;
142begin
143 r0 := CreateRectRgn(0, 0, ClientWidth, ClientHeight);
144 for i := 0 to ControlCount - 1 do
145 if not(Controls[i] is TArea) and Controls[i].Visible then
146 begin
147 with Controls[i].BoundsRect do
148 r1 := CreateRectRgn(Left, Top, Right, Bottom);
149 CombineRgn(r0, r0, r1, RGN_DIFF);
150 DeleteObject(r1);
151 end;
152 InvalidateRgn(Handle, r0, false);
153 DeleteObject(r0);
154end;
155
156procedure TBaseMessgDlg.FormCreate(Sender: TObject);
157begin
158 Left := (Screen.Width - ClientWidth) div 2;
159 Canvas.Font.Assign(UniFont[ftNormal]);
160 Canvas.Brush.Style := bsClear;
161 MessgText := '';
162 TopSpace := 0;
163 TitleHeight := Screen.Height;
164 InitButtons();
165end;
166
167procedure TBaseMessgDlg.FormPaint(Sender: TObject);
168var
169 i, cix: integer;
170begin
171 PaintBackground(self, 3 + Border, 3 + Border, ClientWidth - (6 + 2 * Border),
172 ClientHeight - (6 + 2 * Border));
173 for i := 0 to Border do
174 Frame(Canvas, i, i, ClientWidth - 1 - i, ClientHeight - 1 - i,
175 $000000, $000000);
176 Frame(Canvas, Border + 1, Border + 1, ClientWidth - (2 + Border),
177 ClientHeight - (2 + Border), MainTexture.clBevelLight,
178 MainTexture.clBevelShade);
179 Frame(Canvas, 2 + Border, 2 + Border, ClientWidth - (3 + Border),
180 ClientHeight - (3 + Border), MainTexture.clBevelLight,
181 MainTexture.clBevelShade);
182 SplitText(false);
183
184 for cix := 0 to ControlCount - 1 do
185 if (Controls[cix].Visible) and (Controls[cix] is TButtonBase) then
186 BtnFrame(Canvas, Controls[cix].BoundsRect, MainTexture);
187end;
188
189procedure TBaseMessgDlg.SplitText(preview: boolean);
190var
191 Start, Stop, OrdinaryStop, LinesCount: integer;
192 s: string;
193begin
194 Start := 1;
195 LinesCount := 0;
196 while Start < Length(MessgText) do
197 begin
198 Stop := Start;
199 while (Stop < Length(MessgText)) and (MessgText[Stop] <> '\') and
200 (BiColorTextWidth(Canvas, Copy(MessgText, Start, Stop - Start + 1)) <
201 ClientWidth - 56) do
202 inc(Stop);
203 if Stop <> Length(MessgText) then
204 begin
205 OrdinaryStop := Stop;
206 repeat
207 dec(OrdinaryStop)
208 until (MessgText[OrdinaryStop + 1] = ' ') or
209 (MessgText[OrdinaryStop + 1] = '\');
210 if (OrdinaryStop + 1 - Start) * 2 >= Stop - Start then
211 Stop := OrdinaryStop
212 end;
213 if not preview then
214 begin
215 s := Copy(MessgText, Start, Stop - Start + 1);
216 LoweredTextOut(Canvas, -1, MainTexture,
217 (ClientWidth - BiColorTextWidth(Canvas, s)) div 2,
218 19 + Border + TopSpace + LinesCount * MessageLineSpacing, s);
219 end;
220 Start := Stop + 2;
221 inc(LinesCount)
222 end;
223 if preview then
224 Lines := LinesCount;
225end;
226
227procedure TBaseMessgDlg.CorrectHeight;
228var
229 i: integer;
230begin
231 ClientHeight := 72 + Border + TopSpace + Lines * MessageLineSpacing;
232 Top := (Screen.Height - ClientHeight) div 2;
233 for i := 0 to ControlCount - 1 do
234 Controls[i].Top := ClientHeight - (34 + Border);
235end;
236
237procedure TMessgDlg.FormCreate(Sender: TObject);
238begin
239 inherited;
240 OpenSound := '';
241end;
242
243procedure TMessgDlg.FormShow(Sender: TObject);
244begin
245 Button1.Visible := true;
246 Button2.Visible := not(Kind in [mkOK]);
247 if Button2.Visible then
248 Button1.Left := 101
249 else
250 Button1.Left := 159;
251 if Kind = mkYesNo then
252 begin
253 Button1.Caption := Phrases.Lookup('BTN_YES');
254 Button2.Caption := Phrases.Lookup('BTN_NO')
255 end
256 else
257 begin
258 Button1.Caption := Phrases.Lookup('BTN_OK');
259 Button2.Caption := Phrases.Lookup('BTN_CANCEL');
260 end;
261
262 SplitText(true);
263 CorrectHeight;
264end;
265
266procedure TMessgDlg.FormPaint(Sender: TObject);
267begin
268 inherited;
269 if OpenSound <> '' then
270 PostMessage(Handle, WM_PLAYSOUND, 0, 0);
271end; { FormPaint }
272
273procedure TMessgDlg.Button1Click(Sender: TObject);
274begin
275 ModalResult := mrOK;
276end;
277
278procedure TMessgDlg.Button2Click(Sender: TObject);
279begin
280 ModalResult := mrIgnore;
281end;
282
283procedure TMessgDlg.FormKeyPress(Sender: TObject; var Key: char);
284begin
285 if Key = #13 then
286 ModalResult := mrOK
287 // else if (Key=#27) and (Button2.Visible) then ModalResult:=mrCancel
288end;
289
290procedure SimpleMessage(SimpleText: string);
291begin
292 with MessgDlg do
293 begin
294 MessgText := SimpleText;
295 Kind := mkOK;
296 ShowModal;
297 end
298end;
299
300procedure SoundMessage(SimpleText, SoundItem: string);
301begin
302 with MessgDlg do
303 begin
304 MessgText := SimpleText;
305 OpenSound := SoundItem;
306 Kind := mkOK;
307 ShowModal;
308 end
309end;
310
311procedure TMessgDlg.OnPlaySound(var Msg: TMessage);
312begin
313 Play(OpenSound);
314 OpenSound := '';
315end;
316
317end.
Note: See TracBrowser for help on using the repository browser.