Why do you do this?
Sometimes we need a non-windowing component (such as a component that is not inherited from Twincontrl) to accept Windows messages. A window handle is required to accept the message, but the non-window component has no handle. This article will tell you how to let a component without a handle accept a message through a hidden window
How did this happen?
For example
My clipboard view component is a non-visual component. This form can receive information about changing the clipboard message.
The Allocatehwnd function inside the Delphi Library helps us to create a hidden window , while the associated Deallocatehwnd function frees up the hidden window when we are done with it.
This hidden window will command the window procedure.
Usually when Windows calls a stdcall function, the Allocatehwnd function allows us to use the same method as the form procedure.
We resolve the problem by using a method that references the Allocatehwnd function and registering it as a window procedure .
Inside this registered method We can process the message we are interested in and pass it to Windows
The following code Listing 2 shuts down a framework for how to use the Allocatehwnd function. Nonetheless, our code Listing 1 defines the outline of a component class :
------------------Code Listing 1------------------
Type
{*******************************
Our class derived from tcomponent
or another ancestor class
********************************}
Tmyclass = Class (Tcomponent)
Private
Fhwnd:hwnd;
{*******************************
field to store the window handle
Fields that store window handles
********************************}
...
Protected
Procedure Wndmethod (var msg:tmessage); Virtual
{*******************************
Window proc-called by Windows window procedure (window proc) called by Windows system
To handle messages passed
Hidden window procedure is used to process the ********************************} that is passed to our hidden window
...
Public
Constructor Create (aowner:tcomponent); Override
{*******************************
Create hidden window here:
Store Handle in Fhwnd
Create a hidden form here, and store its handle in the Fhwnd field.
********************************}
destructor Destroy; Override
{*******************************
Free Hidden windows here
Destroying hidden window procedures
********************************}
...
End
------------------Code Listing 1------------------
The following will be the detailed code for the implementation section :
------------------Code Listing 2------------------
Constructor Tmyclass.
Create(aowner:tcomponent);
Begin
Inherited Create (Aowner);
...
//Create a hidden form and use the Wndmethod process
Fhwnd: = Allocatehwnd (Wndmethod);
...
End
destructor Tmyclass.destroy;
Begin
...
//Destroy hidden window
Deallocatehwnd (fhwnd);
...
Inherited Destroy;
End
procedure Tmyclass.wndmethod (var msg:tmessage);
Var
Handled:boolean;
Begin
Suppose we can process the message
Handled: = True;
Case Msg.msg of
wm_something:dosomething;
Code to process messages
Wm_somethingelse:dosomethingelse;
Code to process another message
We're dealing with other messages here.
Else
We no longer process messages
Handled: = False;
End
If Handled Then
We're dealing with messages in the message log.
Msg.result: = 0
Else
We pass the DefWindowProc function
Do not process messages and record results at the same time
Msg.result: = DefWindowProc (Fhwnd,
Msg.msg,
Msg.wparam,
Msg.lparam);
End
------------------Code Listing 2------------------
Original: Of course, we could just use the Windows APIs to create a window of the hard and provide a Windows procedure.
But it's more difficult to use a method as a window procedure if we do it the This method.
The clever features about Allocatehwnd is (a) it creates the hidden window for us and (b) It allows us-use a meth OD, rather than a simple procedure,
As the window Procedure–and a method is more useful since it had access to the class ' private data.
We have, of course, created a window (window) and provided a windowing procedure using only the more difficult ways that the Windows API provides.
However, if we take this approach, it will be difficult to use a method as a window procedure.
The flexible features of Allocatehwnd are:
(a) It creates a hidden window for us to use
(b) It allows us to use a method rather than a simple process (procedure) As the window procedure-more useful when using this method to access the private data of the class.
PostScript: The first translation of the article, there may be some local understanding is not very thorough, so the original address of the appendix is as follows.
Original: http://www.delphidabbler.com/articles?article=1
How to let a non-window component accept messages from Windows