RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Classes.AllocateHWnd Function

Creates a window that implements a specified window procedure.

Pascal
function AllocateHWnd(AMethod: TWndMethod): HWND;
C++
HWND AllocateHWnd(TWndMethod AMethod);

Call AllocateHWnd to create a window that is not associated with a windowed control. Typically, this method is used to create non-visual windows that respond to messages but that do not appear in the user interface. For example, the TTimer component uses this method to create a window that responds to timer messages from Windows.  

The Method parameter specifies the window procedure that the generated window uses to respond to messages.  

AllocateHWnd returns the handle of the newly created window.

Note: Use the DeallocateHWnd procedure to free windows that are created using AllocateHWnd.
 

Delphi Examples: 

 

{
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.
}
{
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 from the
 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;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!