TTimerFWindowHandle (Delphi)
Description
The following code is from the implementation of TTimer. It shows how the timer component's constructor creates a hidden window to respond to Timer messages and how the destructor frees that window.
Code
{
TTimer implements a WndProc method that becomes the window
procedure for the hidden window.
procedure MyTTimer.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_TIMER then { Check for timer messages. }
try
Timer; { This calls the OnTimer event handler. }
except
Application.HandleException(Self);
end
else
{ Any other messages are passed to DefWindowProc,
which tells Windows to handle the message.
Note that the first parameter, FWindowHandle, is the handle
of the window receiving this message. It is obtained with a
call to AllocateHWnd in the constructor. }
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;
{
The TTimer constructor uses AllocateHWnd to create the
window and save its handle.
constructor MyTTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
FInterval := 1000;
FWindowHandle := AllocateHWnd(WndProc);
end;
{ The TTimer destructor calls DeallocateHWnd to free the hidden window. }
destructor MyTTimer.Destroy;
begin
FEnabled := False;
// UpdateTimer; done by TTimer Destroy
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
Uses
- System.Classes.AllocateHWnd ( fr | de | ja )
- System.Classes.DeallocateHWnd ( fr | de | ja )
- Vcl.Forms.TApplication.HandleException ( fr | de | ja )