Intercept Windows messages

Source: Internet
Author: User

---- Borland C ++ builder API Backdoor

---- Introduction

---- C ++
Builder is worthy of Borland's excellent product. It is used to develop WindowsProgramIt is very fast and efficient, but you will find some limitations in the programming process, making it impossible for you to implement
Your own ideas. For example, you cannot modify the system menu of the form. For example, if you use the tracking bar, you cannot find the starttrack and endtrack events, but your program needs these two events.
Windows
In API programming, you don't have to worry about this. You only need to process wm_syscommand and wm_hscroll (or wm_vscroll) messages to implement the above functions.
The disadvantage of Windows API is that programming is very troublesome, and too much time is required for details, but its function is the most powerful. C ++
The VCL of builder is only a subset of it in terms of functions. Because VCL is encapsulated on the basis of APIS, some rarely used functions are discarded during encapsulation. But the programmer's imagination is not blocked.
With greater enthusiasm, they always come up with an ingenious idea. modifying the system menu and adding starttrack and ndtrack events to the track bar is just a trick. However, VCL
These functions are not available. What should I do?

---- Fortunately, Borland did not block the road, but left a backdoor-allowing programmers to intercept and process Windows messages themselves, just like API programming. So there is a solution...

---- Method

---- The following steps are required to intercept Windows messages:
---- In the form header file (such as unit1.h)
---- 1. Create a message ing table in the class declaration, and grant the processing permission of a message to the custom message processing function.

Begin_message_map
Message_handler (Windows message name, tmessage, message processing function name)
Message_handler (...)
End_message_map (tform)

---- 2. Declare the message processing function in the private area of the class declaration.

PRIVATE: // user declarations
Void _ fastcall message processing function name (tmessage & message );
In form files (such as unit1.cpp)

---- 3. Write the message processing function to implement the functions you need here. For example
Void _ fastcall mainform: onwmhscroll (tmessage & message)
{
... // Add your ownCode
Tform: Dispatch (& message );
}

---- Explanation

---- 1. About tmessage

---- Tmessage is a predefined structure of VCL, which is defined as follows:
Struct tmessage
{
Unsigned int MSG; // message
Int wparam; // word Parameter
Int lparam; // long-character parameter
Int result; // message result
};

---- 2. About tform: Dispatch (& message)

---- It is best to add a tform: Dispatch (& message) at the end of the custom message processing function to make the message continue to be transmitted. Without this sentence, the message will be completely intercepted, and The VCL class may not be able to implement normal functions due to lack of messages.

---- Instance 1: Modify the System Menu

---- There are some programs, the main window is very small, and the menu does not exist. If you want to add a dialog box about or set up, the best way is to open the system menu. Windows
In API programming, modifying system menus is not as easy or difficult as implementing other functions. But in C ++
In builder, the Form class (tform) does not provide any attributes and methods related to the system menu. It is easy to implement other functions, but it is difficult to modify the system menu.

---- Fortunately, Borland allowed programmers to process window messages themselves, so the opportunity came!

1. Use the window API function to modify the system menu

Assume that the form is named mainform and the mainform: oncreate () function is set:

1. Use getsystemmenu (mainform-> handle, false) to obtain the system menu handle;
// This article from C ++ builder research-http://www.ccrun.com/article.asp? I = 228 & D = 45041 H

2. Use the appendmenu, deletemenu, and modifymenu functions to modify the system menu and assign the new ID to the custom menu item.
When you run the program, you can see that the system menu has been modified, but the custom menu item cannot be responded.

2. Intercept the wm_syscommand message to respond to custom menu items
In the form header file (such as unit1.h)

1. Add the message response table at the end of the Form class definition to obtain the message processing permission of wm_syscommand.
Begin_message_map
Message_handler (wm_syscommand, tmessage, onwmsyscommand)
End_message_map (tform)

2. Add the message processing function declaration to the private area defined in the Form class.
PRIVATE: // user declarations
Void _ fastcall onwmsyscommand (tmessage & message );

In form files (such as unit1.h)

3. Write the message Response Function
Void _ fastcall tform1: onwmsyscommand (tmessage & message)
{
If (message. wparam = id_sysmenu_myitem)
{
// Your code here, do something
}
Tform: Dispatch (& message );
}

3. Complete program example

Example 2: Add onstarttrack and onendtrack events to the trail bar.

When the tracking bar is used for progress control, onstarttrack and onendtrack are likely to be the events you need. For example, when you move the slider to control the progress of multimedia playback
The onstarttrack event is required to stop playing, and the onendtrack event is required to locate a new playing position. But Borland did not provide these two events. I am waiting for programmers
I had to be self-reliant and had to block windows messages.

1. Intercept the wm_hscroll message and add onstarttrack and onendtrack events to the tracking bar.

In the form header file (such as unit. h)

1. Add the message response table at the end of the Form class definition and assign the wm_hscroll Message Processing permission to the onwmhscroll function.
Begin_message_map
Message_handler (wm_hscroll, tmessage, onwmhscroll)
End_message_map (tform)

2. Add the onwmhscroll function declaration in the private area of the Form class definition.
PRIVATE: // user declarations
Void _ fastcall onwmhscroll (tmessage & message );

3. Add starttrack and endtrack function declaration in the private area of the Form class definition.
PRIVATE: // user declarations
Void _ fastcall trackbar1starttrack (tobject * sender );
Void _ fastcall trackbar1endtrack (tobject * sender );

In form files (such as unit. cpp)

4. Write the onwmhscroll function so that it can call the starttrack and endtrack functions based on message parameters to generate onstarttrack and onendtrack events in a practical sense.

5. Write the starttrack and endtrack functions.

In the vertical tracking column, change the preceding wm_hscroll to wm_vscroll.

2. Complete program example

Conclusion

In Borland C ++ Builder programming, blocking Windows messages is an advanced programming technology that allows you to explore the potential of windows as much as possible, especially to the programmers who used API programming. Blocking Windows messages is an API stage. When VCL cannot do anything for you, think of the underlying API.

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.