2 Copyright (c
) 1999 by Michael van Canneyt
4 Win32 menu creation example
.
7 { Changes by G”ran Andersson
: major
8 const FileMenus
[] removed
15 uses GetWindowTextLength
& GetWindowText
16 also saves empty files
18 FreeMem frees Len
+1, not Len
23 WM_Size don
't assumes StatH=16
24 WM_Command 101 calls NewText
25 WM_Command 102 calls LoadText
26 WM_Command 104 issues a WM_Close instead of doing a Halt
30 takes Status object as parameter
31 const EditText changed to empty string
32 unused variable DC removed
33 WS_HScroll & WS_VScroll added to const CS_Start
34 ES_AutoHScroll & ES_AutoVScroll removed from CS_Start
35 don't assumes StatH
=16
36 AddText() commented out
38 menu creation moved inside
if structure
39 Options menu item uses the newly created SubMenu
41 slight structure
change (avoid unneccesary Exit
)
42 call to StatusCreate before EditCreate
45 make edit panel always active
46 add
3d inner bevel to edit panel
47 window background color
: white
48 only ask to save
if edited
62 Const AppName
= 'MenuDemo';
65 hWindow
,hStatus
,Hedit
: HWnd
;
66 WindowClass
: WndClass
;
70 EditMenus
: Array
[201..203] Of pchar
= ('Cut','copy','paste');
73 TFileName
= Array
[0..Max_Path
] Of Char
;
75 Function
SelectFile(Var FName
:TFileName; Open
:Boolean): Boolean
;
78 Filter
: PChar
= 'Text files (*.txt)'#0'*.txt'#0'All files (*.*)'#0'*.*'#0;
82 NameRec
: OpenFileName
;
84 FillChar(NameRec
,SizeOf(NameRec
),0);
88 LStructSize
:= SizeOf(NameRec
);
90 LpStrFilter
:= Filter
;
93 Flags
:= OFN_Explorer Or OFN_HideReadOnly
;
96 Flags
:= Flags Or OFN_FileMustExist
;
101 SelectFile
:= GetOpenFileName(@NameRec)
103 SelectFile
:= GetSaveFileName(@NameRec);
113 If
SelectFile(FName
,False
) Then
117 Len
:= GetWindowTextLength(HEdit
);
122 GetWindowText(HEdit
,P
,Len
+1);
123 BlockWrite(F
,P^
,Len
);
132 If
MessageBox(HWindow
,'Save text?','Edited',MB_IconQuestion Or MB_YesNo
)=IdYes Then
147 If
SelectFile(FName
,True
) Then
154 If Len
>0 Then
BlockRead(F
,P^
,Len
);
156 SetWindowText(HEdit
,P
);
167 SendMessage(HEdit
,WM_SetText
,1,LongInt(Empty
));
170 Function
WindowProc (Window
:HWnd;AMessage
,WParam
,LParam
:Longint): Longint
;
183 BeginPaint(Window
,@ps);
184 GetClientRect(Window
,@r);
199 GetClientRect(HStatus
,@R);
200 StatH
:= R
.Bottom-R
.Top
;
201 GetClientRect(Window
,@R);
202 MoveWindow (hStatus
,r
.left
,r
.bottom-StatH
,r
.right
,r
.bottom
,true);
203 MoveWindow (HEdit
,0,0,r
.right-r
.left
,r
.bottom-r
.top-StatH
,true);
207 NrMenu
:= WParam And
$FFFF;
212 104 : PostMessage(Window
,WM_Close
,0,0);
213 201..203: MessageBox(Window
,EditMenus
[NrMenu
],
214 'Edit operation not implemented',MB_OK Or
219 WindowProc
:= DefWindowProc(Window
,AMessage
,WParam
,LParam
);
222 Function WinRegister
: Boolean
;
226 Style
:= cs_hRedraw Or cs_vRedraw
;
227 lpfnWndProc
:= WndProc(@WindowProc);
230 hInstance
:= system
.MainInstance
;
231 hIcon
:= LoadIcon (0,idi_Application
);
232 hCursor
:= LoadCursor (0,idc_Arrow
);
233 hbrBackground
:= GetStockObject(GRAY_BRUSH
);
234 lpszMenuName
:= 'Files';
235 lpszClassName
:= AppName
;
237 Result
:= RegisterClass (WindowClass
)<>0;
240 Function
EditCreate(ParentWindow
,Status
:HWnd): HWnd
;
243 CS_Start
= WS_Child
or WS_HScroll
or WS_VScroll
or ES_MultiLine
or ES_Left
;
244 EdiTText
: PChar
= '';
250 { rev
1.5 : comment out
251 Procedure
AddText (S
:String);
254 SendMessage(HEdit
,em_replacesel
,0,longint(pchar(@S[1])));
258 GetClientRect(Status
,@R);
259 StatH
:= R
.Bottom-R
.Top
;
260 GetClientRect(ParentWindow
,@R);
261 HEdit
:= CreateWindow ('EDIT',EditText
,CS_Start
,0,0,
262 R
.Right-R
.Left
,R
.Bottom-R
.top-StatH
,ParentWindow
,0,
263 System
.MainInstance
,Nil
);
266 ShowWindow(Hedit
,cmdShow
);
272 Function WinCreate
: HWnd
;
277 hWindow
:= CreateWindow (AppName
,'MenuDemo',ws_OverlappedWindow
,
278 cw_UseDefault
,cw_UseDefault
,cw_UseDefault
,
279 cw_UseDefault
,0,0,system
.MainInstance
,Nil
);
283 SubMenu
:= CreateMenu
;
284 AppendMenu(Submenu
,MF_STRING
,101,'&New...');
285 AppendMenu(Submenu
,MF_STRING
,102,'&Open...');
286 AppendMenu(Submenu
,MF_STRING
,103,'&Save...');
287 AppendMenu(Submenu
,MF_SEPARATOR
,0,Nil
);
288 AppendMenu(SubMenu
,MF_String
,104,'E&xit');
289 AppendMenu(Menu
,MF_POPUP
,SubMenu
,'&Files');
290 SubMenu
:= CreateMenu
;
291 AppendMenu(SubMenu
,MF_String
,201,'&Cut');
292 AppendMenu(SubMenu
,MF_String
,202,'&Copy');
293 AppendMenu(SubMenu
,MF_STRING
,203,'&Paste');
294 AppendMenu(Menu
,MF_POPUP
,SubMenu
,'&Edit');
295 SubMenu
:= CreateMenu
;
296 AppendMenu(SubMenu
,MF_String
,301,'&Settings');
297 AppendMenu(Menu
,MF_POPUP
,SubMenu
,'&Options');
298 AppendMenu(Menu
,MF_STRING
,0,'&Help');
299 SetMenu(hWindow
,menu
);
300 ShowWindow(hWindow
,CmdShow
);
301 UpdateWindow(hWindow
);
306 Function
StatusCreate (parent
:hwnd): HWnd
;
308 StatusCreate
:= CreateStatusWindow (WS_CHILD Or WS_VISIBLE
,'Ready...',parent
,$7712);
312 If Not WinRegister Then
314 MessageBox (0,'Register failed',Nil
, mb_Ok
);
318 hWindow
:= WinCreate
;
319 If
longint(hWindow
)=0 Then
321 MessageBox (0,'WinCreate failed',Nil
,MB_OK
);
325 HStatus
:= statuscreate(hwindow
);
326 HEdit
:= EditCreate(HWindow
,HStatus
);
327 While
GetMessage(@AMessage,0,0,0) Do
329 TranslateMessage(AMessage
);
330 DispatchMessage(AMessage
);
332 Halt(AMessage
.wParam
);
339 Revision
1.1 2002/02/19 08:24:26 sasu
342 Revision
1.1 2000/07/13 06:30:21 michael
345 Revision
1.7 2000/01/21 00:44:51 peter
349 Revision
1.6 1999/08/10 08:21:45 michael
350 + Log keyword placement correction
.
352 Revision
1.5 1999/08/10 08:20:49 michael
353 + Enhancements by Göran Andersson
355 Revision
1.4 1999/07/16 12:20:57 michael
356 + Added saving
in fixed file
358 Revision
1.3 1999/06/28 16:15:11 peter
361 Revision
1.2 1999/05/03 18:04:39 peter