2006. szeptember 1., péntek

How to disable a TTimer while browsing a menu


Problem/Question/Abstract:

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:

protected

procedure WMMenuSelect(var msg: TWMMenuSelect); message WM_MenuSelect;

and then add this procedure:

procedure TForm1.WMMenuSelect(var msg: TWMMenuSelect);
begin
  tmrAutoClose.Enabled := (msg.MenuFlag = $FFFF);
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.

{ ... }
private

procedure WMEnterMenuLoop(var msg: TMessage);
  message WM_ENTERMENULOOP;
procedure WMExitMenuLoop(var msg: TMessage);
  message WM_EXITMENULOOP;
{ ... }

procedure TForm1.WMEnterMenuLoop(var msg: TMessage);
begin
  IdleTimer.Enabled := false;
  inherited;
end;

procedure TForm1.WMExitMenuLoop(var msg: TMessage);
begin
  IdleTimer.Enabled := true;
  inherited;
end;

Nincsenek megjegyzések:

Megjegyzés küldése