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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
{
Copyright (c) 1999 by Michael van Canneyt
Win32 menu creation example.
}
{ Changes by G”ran Andersson: major
const FileMenus[] removed
const filename removed
type TFileName added
SelectFile() added *
LoadText() added *
SaveText():
SelectFile used *
uses GetWindowTextLength & GetWindowText
also saves empty files
uses untyped file
FreeMem frees Len+1, not Len
AskSave() added *
NewText() added *
WindowProc():
WM_Close added
WM_Size don't assumes StatH=16
WM_Command 101 calls NewText
WM_Command 102 calls LoadText
WM_Command 104 issues a WM_Close instead of doing a Halt
WinRegister():
with structure used
EditCreate():
takes Status object as parameter
const EditText changed to empty string
unused variable DC removed
WS_HScroll & WS_VScroll added to const CS_Start
ES_AutoHScroll & ES_AutoVScroll removed from CS_Start
don't assumes StatH=16
AddText() commented out
WinCreate():
menu creation moved inside if structure
Options menu item uses the newly created SubMenu
main:
slight structure change (avoid unneccesary Exit)
call to StatusCreate before EditCreate
2do:
make edit panel always active
add 3d inner bevel to edit panel
window background color : white
only ask to save if edited
cut
copy
paste
settings
help
}
Program menudemo;
{$APPTYPE GUI}
{$MODE DELPHI}
Uses Strings,Windows,CommDlg,CommCtrl;
Const AppName = 'MenuDemo';
Var AMessage: Msg;
hWindow,hStatus,Hedit: HWnd;
WindowClass: WndClass;
Menu: hMenu;
Const
EditMenus: Array[201..203] Of pchar = ('Cut','copy','paste');
Type
TFileName = Array[0..Max_Path] Of Char;
Function SelectFile(Var FName:TFileName; Open:Boolean): Boolean;
Const
Filter: PChar = 'Text files (*.txt)'#0'*.txt'#0'All files (*.*)'#0'*.*'#0;
Ext: PChar = 'txt';
Var
NameRec: OpenFileName;
Begin
FillChar(NameRec,SizeOf(NameRec),0);
FName[0] := #0;
With NameRec Do
Begin
LStructSize := SizeOf(NameRec);
HWndOwner := HWindow;
LpStrFilter := Filter;
LpStrFile := @FName;
NMaxFile := Max_Path;
Flags := OFN_Explorer Or OFN_HideReadOnly;
If Open Then
Begin
Flags := Flags Or OFN_FileMustExist;
End;
LpStrDefExt := Ext;
End;
If Open Then
SelectFile := GetOpenFileName(@NameRec)
Else
SelectFile := GetSaveFileName(@NameRec);
End;
Procedure SaveText;
Var Len: Longint;
P: PChar;
F: File;
FName: TFileName;
Begin
If SelectFile(FName,False) Then
Begin
Assign(F,@FName);
Rewrite(F,1);
Len := GetWindowTextLength(HEdit);
GetMem(P,Len+1);
P[Len] := #0;
If Len>0 Then
Begin
GetWindowText(HEdit,P,Len+1);
BlockWrite(F,P^,Len);
End;
Close(F);
FreeMem(P,Len+1);
End;
End;
Procedure AskSave;
Begin
If MessageBox(HWindow,'Save text?','Edited',MB_IconQuestion Or MB_YesNo)=IdYes Then
Begin
SaveText;
End;
End;
Procedure LoadText;
Var
FName: TFileName;
F: File;
Len: LongInt;
P: PChar;
Begin
AskSave;
If SelectFile(FName,True) Then
Begin
Assign(F,@FName);
Reset(F,1);
Len := FileSize(F);
GetMem(P,Len+1);
P[Len] := #0;
If Len>0 Then BlockRead(F,P^,Len);
Close(F);
SetWindowText(HEdit,P);
FreeMem(P,Len+1);
End;
End;
Procedure NewText;
Const
Empty: PChar = '';
Begin
AskSave;
SendMessage(HEdit,WM_SetText,1,LongInt(Empty));
End;
Function WindowProc (Window:HWnd;AMessage : UINT; WParam : WParam; LParam:LParam): LResult;
stdcall;
export;
Var ps: paintstruct;
r: rect;
StatH: Word;
nrmenu : longint;
Begin
WindowProc := 0;
Case AMessage Of
wm_Paint:
Begin
BeginPaint(Window,@ps);
GetClientRect(Window,@r);
EndPaint(Window,ps);
Exit;
End;
wm_Close:
Begin
AskSave;
End;
wm_Destroy:
Begin
PostQuitMessage (0);
Exit;
End;
wm_Size:
Begin
if HStatus<>0 then
begin
GetClientRect(HStatus,@R);
StatH := R.Bottom-R.Top;
GetClientRect(Window,@R);
MoveWindow (hStatus,r.left,r.bottom-StatH,r.right,r.bottom,true);
if HEdit<>0 then
MoveWindow (HEdit,0,0,r.right-r.left,r.bottom-r.top-StatH,true);
end;
End;
wm_Command:
Begin
NrMenu := WParam And $FFFF;
Case NrMenu Of
101 : NewText;
102 : LoadText;
103 : SaveText;
104 : PostMessage(Window,WM_Close,0,0);
201..203: MessageBox(Window,EditMenus[NrMenu],
'Edit operation not implemented',MB_OK Or
MB_IconInformation);
End;
End;
End;
WindowProc := DefWindowProc(Window,AMessage,WParam,LParam);
End;
Function WinRegister: Boolean;
Begin
With WindowClass Do
Begin
Style := cs_hRedraw Or cs_vRedraw;
lpfnWndProc := WndProc(@WindowProc);
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := system.MainInstance;
hIcon := LoadIcon (0,idi_Application);
hCursor := LoadCursor (0,idc_Arrow);
hbrBackground := GetStockObject(GRAY_BRUSH);
lpszMenuName := 'Files';
lpszClassName := AppName;
End;
Result := RegisterClass (WindowClass)<>0;
End;
Function EditCreate(ParentWindow,Status:HWnd): HWnd;
Const
CS_Start = WS_Child or WS_HScroll or WS_VScroll or ES_MultiLine or ES_Left;
EdiTText: PChar = '';
Var
HEdit: HWND;
R: TRect;
StatH: Word;
{ rev 1.5 : comment out
Procedure AddText (S:String);
begin
S:=S+#0;
SendMessage(HEdit,em_replacesel,0,longint(pchar(@S[1])));
end;
}
Begin
GetClientRect(Status,@R);
StatH := R.Bottom-R.Top;
GetClientRect(ParentWindow,@R);
HEdit := CreateWindow ('EDIT',EditText,CS_Start,0,0,
R.Right-R.Left,R.Bottom-R.top-StatH,ParentWindow,0,
System.MainInstance,Nil);
If HEdit<>0 Then
Begin
ShowWindow(Hedit,cmdShow);
UpdateWindow(HEdit);
End;
Result := HEdit;
End;
Function WinCreate: HWnd;
Var hWindow: HWnd;
SubMenu: hMenu;
Begin
hWindow := CreateWindow (AppName,'MenuDemo',ws_OverlappedWindow,
cw_UseDefault,cw_UseDefault,cw_UseDefault,
cw_UseDefault,0,0,system.MainInstance,Nil);
If hWindow<>0 Then
Begin
Menu := CreateMenu;
SubMenu := CreateMenu;
AppendMenu(Submenu,MF_STRING,101,'&New...');
AppendMenu(Submenu,MF_STRING,102,'&Open...');
AppendMenu(Submenu,MF_STRING,103,'&Save...');
AppendMenu(Submenu,MF_SEPARATOR,0,Nil);
AppendMenu(SubMenu,MF_String,104,'E&xit');
AppendMenu(Menu,MF_POPUP,SubMenu,'&Files');
SubMenu := CreateMenu;
AppendMenu(SubMenu,MF_String,201,'&Cut');
AppendMenu(SubMenu,MF_String,202,'&Copy');
AppendMenu(SubMenu,MF_STRING,203,'&Paste');
AppendMenu(Menu,MF_POPUP,SubMenu,'&Edit');
SubMenu := CreateMenu;
AppendMenu(SubMenu,MF_String,301,'&Settings');
AppendMenu(Menu,MF_POPUP,SubMenu,'&Options');
AppendMenu(Menu,MF_STRING,0,'&Help');
SetMenu(hWindow,menu);
ShowWindow(hWindow,CmdShow);
ShowWindow(hWindow,SW_SHOW);
UpdateWindow(hWindow);
End;
Result := hWindow;
End;
Function StatusCreate (parent:hwnd): HWnd;
Begin
StatusCreate := CreateStatusWindow (WS_CHILD Or WS_VISIBLE,LPCSTR('Ready...'),parent,$7712);
End;
Begin
If Not WinRegister Then
Begin
MessageBox (0,'Register failed',Nil, mb_Ok);
End
Else
Begin
hWindow := WinCreate;
If longint(hWindow)=0 Then
Begin
MessageBox (0,'WinCreate failed',Nil,MB_OK);
End
Else
Begin
HStatus := statuscreate(hwindow);
HEdit := EditCreate(HWindow,HStatus);
While GetMessage(@AMessage,0,0,0) Do
Begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
End;
Halt(AMessage.wParam);
End;
End;
End.
|