To intercept Windows messages, take the following steps:
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 own code here
TForm: Dispatch (& Message );
}
------ Explanation
1. About TMessage
TMessage is a predefined structure of VCL and is defined as follows:
Struct TMessage
{
Unsigned int Msg; file: // message
Int WParam; file: // word Parameter
Int LParam; file: // long-character parameter
Int Result; file: // 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. In Windows API programming, it is not easy or difficult to modify the system menu, just like implementing other functions. However, in C ++ 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 by 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:
Use GetSystemMenu (MainForm-> Handle, false) to obtain the system menu Handle;
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)
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)
Add the message processing function declaration to the private area of the Form class definition.
Private: // User declarations
Void _ fastcall OnWMSysCommand (TMessage & Message );
In form files (such as Unit1.h)
Message response Writing 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, you need the OnStartTrack event to stop playback and the OnEndTrack event to locate a new playing position. But Borland did not provide these two events, so I waited for programmers to be self-reliant and 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)
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)
Add the OnWMHScroll function declaration in the private area of the Form class definition.
Private: // User declarations
Void _ fastcall OnWMHScroll (TMessage & Message );
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)
Write the OnWMHScroll function so that it can call StartTrack and EndTrack functions based on message parameters to generate OnStartTrack and OnEndTrack events in a practical sense.
Write 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.