Author: Jonas Bilinkevicius
I have an application that auto-minimizes itself after 4 seconds, when maximized
(using TTimer). The main form has a menu. I want the Timer to be disabled while the
user browses the menu items. Is that possible?
Answer:
Solve 1:
Add this line to your form declaration:
1 protected2 3 procedure WMMenuSelect(var msg: TWMMenuSelect); message WM_MenuSelect;
4 5 //and then add this procedure:6 7 procedure TForm1.WMMenuSelect(var msg: TWMMenuSelect);
8 begin9 tmrAutoClose.Enabled := (msg.MenuFlag = $FFFF);
10 end;
Solve 2:
Yes. The form will get one special message when the menu is first opened
(WM_ENTERMENULOOP) and a second when the menu is finally closed (WM_EXITMENULOOP).
Add handlers for these and disable/ enable the timer in them.
11 { ... }12 private13 14 procedure WMEnterMenuLoop(var msg: TMessage);
15 message WM_ENTERMENULOOP;
16 procedure WMExitMenuLoop(var msg: TMessage);
17 message WM_EXITMENULOOP;
18 { ... }19 20 procedure TForm1.WMEnterMenuLoop(var msg: TMessage);
21 begin22 IdleTimer.Enabled := false;
23 inherited;
24 end;
25 26 procedure TForm1.WMExitMenuLoop(var msg: TMessage);
27 begin28 IdleTimer.Enabled := true;
29 inherited;
30 end;