How to capture Windows messages not processed by VCL

Source: Internet
Author: User

---- C ++
Builer VCL provides a processing mechanism for most Windows messages.ProgramIt is enough, but VCL is not all-encompassing.
How can I capture Windows messages as needed? C ++
Builder adopts the message image table mechanism, and uses the message image tableCodeWhen the window captures a message, this function is called.
Event handles are very similar.

---- The format of the message image table defined in C ++ builder is as follows:

Begin_message_map
Message_handler (<message>,
<Message structure>, <message handler>)
......
Message_handler (<message>,
<Message structure>, <message handler>)
End_message_map (classname)

---- Classname is the base class name, the message parameter is the Windows message to be captured, and the message
The structure parameter is used to pass the VCL profiling message. The structure name of the message that is used to pass the parameter contains all the parameters required for processing the message. The message profiling structure of different messages is different.
The general message profiling structure is defined as follows:
Struct tmessage
{
Unsigned int MSG; // WINDOWS Message
Long wparam;
Long lparam;
Long result;
}

---- The key here is the result data member, which is used to set the return value after processing the message. The program can perform corresponding processing based on different return values. The message handler parameter is the name of the function that processes the message.
---- As the saying goes: a thousand words are not like a picture. The following is an example to illustrate. Let's drag a untitled window. The principle is to capture the wm_nchittest message and send the message when you click the client area of the form, let windows think it is the title bar of a form.

----
Create a new project, save the unit file as mainform. cpp, and save the project file as nocaption. BPR. First, you must create a form without a title bar.
If the bordericons set is set to null, the system menu, maximization, and minimization buttons of the window are deleted, and the borderstyle attribute is set to bsdialog. Note that
All created C ++
Builder forms have a title bar. You cannot simply obtain a form without a title bar by setting the caption attribute of form1 as in VB. you must
In the derived class of tform, The createparams function is reloaded to remove the corresponding window identifier and delete the Form title bar at runtime. Open mainform. h file, in
Add the createparams prototype to the private section:
// This article from C ++ builder research-http://www.ccrun.com/article.asp? I = 226 & D = 8vzf36
Void _ fastcall createparams (tcreateparams & Params );
The tcreateparams structure contains various parameters for creating a form,
Here we want to block the ws_caption bit.
Add the following code to mainform. cpp:
Void _ fastcall tform1: createparams (tcreateparams & Params)
{
Tform: createparams (Params); // call the member functions in the base class first.
Params. Style & = ~ Ws_caption;
}

---- Create a message image table below to capture the wm_nchittest message and enter the code in the public segment of mainform. h:
Begin_message_map
Message_handler
(Wm_nchittest, tmessage, wmnchittest)
End_message_map (tform)

---- The message wm_nchittest is named "non-customer zone hit test", which takes precedence over all other customer and non-customer zone mouse messages. Windows applications usually send this message to defwindowproc, then, Windows uses this message to generate all other mouse messages based on the mouse position.
---- Declaration of adding a message processing function to the private segment:

Void _ fastcall wmnchittest (tmessage & message );

---- Enter the definition of this function in mainform. cpp:
Void _ fastcall tform1: wmnchittest (tmessage & message)
{
If (getasynckeystate (vk_lbutton) <0)

---- Message. Result = htcaption; // If you press the left mouse button, the system notifies windows that the mouse is located in the title bar area, and Windows will complete the drag operation as required.
Else
Message. Result = htclient;
}

---- Windows is used here
The API function getasynckeystate checks the status of the mouse button in real time, and detects the mouse action by checking whether the high position of the virtual key code vk_lbutton is set. If you press
If you left the mouse button, the key code is set to 1 at a high position, and the getasynckeystate function returns a negative number.
---- Compile and run the program according to F9. Click any position of the form to drag the form as you click on the title bar. The mechanism to end the program running is not provided here, which can be achieved by adding popmenu.

---- The above Code is compiled and run in C ++ builder3 and pwin98 environments.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.