Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Intercepting Windows messages in non-visual components Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 3.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Peter Johnson 

Sometimes we need a non-windowed component (i.e. one that isn't derived from 
TWinControl) to receive Windows messages - but non-windowed component don't have 
window handles. For example suppose we are developing a non-visual component that 
registers our application as a clipboard viewer so the application can respond to 
changes in the clipboard. To get information about clipboard changes our component 
needs to receive messages from Windows.


The Delphi library function AllocateHWnd is used to create a hidden window for us 
and the related DeallocateHWnd disposes of the window when we've finished with it. 

The hidden window needs a window procedure. We can use a method of our component 
class to provide the window procedure. AllocateHWnd takes a reference to the method 
its parameter - it takes care of the problem of registering the method as a window 
procedure for us. In the method we handle the messages we are interested in and 
hand the rest off to Windows using the DefWindowProc API call. 

The following code gives the skeleton of how to use AllocateHWnd. First, here's the 
class declaration from the interface section of code: 

1   type
2     // Our class derived from TComponent
3     // (or another ancestor class)
4     TMyClass = class(TComponent)
5     private
6       FHWnd: HWND;
7       // field to store the window handle
8     {...}
9     protected
10      procedure WndMethod(var Msg: TMessage); virtual;
11      // the window proc - called by Windows to handle
12      // the given message
13    {...}
14    public
15      constructor Create(AOwner: TComponent); override;
16      // create window proc here
17      destructor Destroy; override;
18      // free window proc here
19    {...}
20    end;
22  //And here's the implementation details: 
24  TMyClass.Create(AOwner: TComponent);
25  begin
26    inherited Create(AOwner);
27    {... }
28    // Create the window
29    FHWnd := AllocateHWnd(WndMethod);
30    { ...}
31  end;
33  TMyClass.Destroy;
34  begin
35    {...}
36    // Destroy the window
37    DeallocateHWnd(FHWnd);
38    {...}
39    inherited Destroy;
40  end;
42  TMyClass.WndMethod(var Msg: TMessage);
43  var
44    Handled: Boolean;
45  begin
46    // Assume we handle message
47    Handled := True;
48    case Msg.Msg of
49      WM_SOMETHING: DoSomething;
50      // Code to handle a message
51      WM_SOMETHINGELSE: DoSomethingElse;
52      // Code to handle another message
53    {...}
54    else
55      // We didn't handle message
56      Handled := False;
57    end;
58    if Handled then
59      // We handled message - record in message result
60      Msg.Result := 0
61    else
62      // We didn't handle message
63      // pass to DefWindowProc and record result
64      Msg.Result := DefWindowProc(FHWnd, Msg.Msg,
65        Msg.WParam, Msg.LParam);
66  end;

Of course, we could just use the Windows API to create a window the hard way and 
provide a windows procedure. But it is more difficult to use a method (rather than 
a simple procedure) as a window procedure if we do it this way. The clever features 
about AllocateHWnd are that (a) it creates the hidden window for us and (b) it 
allows us to use a method, rather than a simple procedure as the window procedure 
-- and a method is more useful since it has access to the class's private data. 

Component Download:

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC