Message processing techniques for C ++ Builder non-visual components

zhaozj2021-02-08  227

Message processing techniques for C Builder non-visual components

A non-visual component must respond to a Windows operating system or user-defined message. However, since a non-visual component has no window, it does not have a window handle, naturally it does not receive the message, in order to solve this problem, our idea is to create a hidden window to make non-visual components to receive news.

To create a hidden window for your non-visual components, you need to have the following:

1. A private variable type (private variable type) hWnd to obtain a window handle.

2. A function (A WndProc) used to capture the window sent to the component.

3. Create a window handle and set WNDPROC for the call to AllColatehWnd.

In order to clearly explain the above ideas and display creation, the following we will explain with a specific example. First we first create a new component, in C Builder, select File | New ... Double-click Component icon to display a new component dialog to change Ancestor Type to tComponent and Class Name TTEST and is set. Then, switch to the header file of the new component, add the following declarations in the private part of the class:

HWND FHANDLE; VOID-FastCall WndProc (TMESSAGE & MSG);

The first line declares an HWND variable that calls FHANDLE, which will be used to capture the window handle after the window is created. The second line declares a WndProc function for receiving messages. The statement of this function must be identified in order to define it is a WndProc, then construct the following statement in the class declaration public (public) section:

VIOD DOIT ();

This public function will be used to test components, and the class declaration should be as follows:

Class Package TTEST: PUBLIC

Tcomponent {private:

HWND FHANDLE; VOID-FastCall WndProc (TMESSAGE & MSG);

protected:

PUBLIC:

-Fastcall ttest (tComponent * Owner);

Void DOIT (); -published:

}

Now switch to the component's code unit, add the following line to the top of the unit (maybe a good place on the function)

#Define my-message.wm_user 1

This line declares that when the DOIT function is called, the component will send it to its own user-defined message. At this point we have to assign a window handle for the component. This handle will provide a hidden window that allows us to capture messages in the component. Find the component constructor, add the following code:

-Fastcall test :: test (tcomponent * oowner): tComponent (OWNER) {fhandle = allocatehwnd (wndproc);

Ok, the important step has been completed, the allocatehwnd function creates a hidden window and returns its handle, note that we will send Windows where to send a message, pass the address of WndProc;

Now let's create a part of the WndProc. Add:

Void-fastcall ttest :: WndProc (TMESSAGE & MSG) {if (msg.msg == my_MESSAGE) MessageBox (0, "Got Here!", "Message", 0); Try {Dispatch (& MSG);} catch (.. .) {Application-> HandleException (this);

}

}

Whenever Windows sends a message to the component, Windows calls this function whenever Windows. This part of the code has completed two things. First, it checks if the received message is our user-defined message. If so, a message box will be displayed, you can see the message we have received. Secondly, this code transmits messages during the system (or VCL) process, and the TRY / CATCH block is used to ensure that if an abnormality occurs, it will become a handle under the default style.

In summary, the WndProc function is sent to all other messages for the default handle to monitor all customer messages. Now we create a DOIT function, complete our components, join us to create a DOIT function, complete our components, join the code:

Void TTEST :: DOIT ()

{

Postmessage (Fhaandle, My-Message, 0, 0);

}

This function sends a window handle of a message component (remember that this window handle is previously stored in the FHANDLE data finished product). Now we have completed the creation of component selection, with SELECTFILE | ColSeall to save our work test components.

The next step will test the component. If you use BCB3, you must join the component into "PackGE), then use Componet | Install (you can use DCLSTD35 Packe to quickly test). Select the testbb.cpp you just exist. Once you have installed the completion of the component, it will appear on the component board. Double-click the button, create the following code for the button's onclick event:

TEST1-> DOIT ();

Now run the program. When you click the button, you will see a message box to display "Got Here".

Listinga and B include header files and source code below.

Summary: A non-visual component that can respond to Windows messages has many purposes. The most obvious is to encapsulate some aspects of WindowsAPI. For example: TAPI and WINSOCK send messages to the specified user of the event. If you write the components encapsulated a such API. You will need to capture messages sent by Windows. Join the hidden window in your component will help you do this.

The above programs are debugged in C Builder 3.0.

转载请注明原文地址:https://www.9cbs.com/read-3213.html

New Post(0)