A component that is not visible must respond to Windows operating systems or user-defined messages. However, because a non-visual component does not have a window, so it does not have a window handle, and naturally it cannot receive messages, in order to solve this problem, our idea is to create a hidden window so that non-visual components can receive messages.
To create a hidden window for your non-visual component, you need the following:
1. The HWND of a private variable type (private Variable) to get the window handle.
2. A function (a WndProc) that is used to capture a window sent to a component.
3. A call to Allcolatehwnd causes the window handle to be created and the WndProc is set.
In order to clearly explain the above ideas and show the creation process, we will take a concrete example to illustrate.
First we create a new component, in C + + builder, select file| NEW ... Double-click the component icon to display a new Component dialog box change ancestor type is Tcomponent and class name is TTest and set.
Then, switch to the header file for the new component and add the following declaration to the private section of the Class (private sections):
HWnd Fhandle;
void __fastcall WndProc (tmessage &msg);
The first line declares an HWND variable that invokes the Fhandle, which is used to capture the window handle after the window is created.
The second line declares a WndProc function for receiving messages. The declaration of this function must be identified to qualify it as a wndproc, and then construct the following declaration in the class declaration public (publicly) section:
Viod DoIt( );
这个公有函数将被我们用来测试组件,类声明应如下:
class PACKAGE Ttest:public Tcomponent
{
private:
HWnd Fhandle;
void __fastcall WndProc(Tmessage &Msg);
protected:
public:
__fastcall Ttest(Tcomponent *Owner);
void DoIt( );
__published:
};