Intercept Windows Message

xiaoxiao2021-03-06  40

Intercepting Windows Message C Builder is worthy of Borland's excellent products, using it to develop Windows programs very fast, but you will also find some restrictions in programming, so you can't achieve your own ideas. For example, you can't modify the system menu of the form; for example, when using the tracking bar, you can't find the StartTrack and EndTrack events, and your procedure requires these two events. In Windows API programming, you will not have these troubles, just process the WM_SYSCOMMAND and WM_HSCROLL (or WM_VSCROLL) messages, the above functions can be implemented. The disadvantage of the Windows API is that it is very troublesome, too much time to consume on the details, but its function is the most powerful. The VCL of C Builder is functionally only a subset because the VCL is encapsulated on the API, and some unused functions are prepared. But the programmer's imagination is not encapsulated. They always have a big enthusiasm to achieve other ideas, modify the system menu and adding StartTrack and EndTrack events for the tracking bar. But the VCL does not have these functions, what should I do? Fortunately, Borland did not block roads, but left a back door - allowing programmers to intercept and handle Windows messages, just like API programming. So, the method has ...

------ Method interception Windows message requires the following steps: In the table single header file (such as Unit1.h) 1. Create a message mapping table in the class declaration, handle the processing of a message to 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); within the form file (such as Unit1.cpp)

3. Write the message processing function, implement the features you need. such as

Void __fastcall mainform :: onwmhscroll (tMESSAGE & Message) {... // Add your own code TFORM :: Dispatch (& Message);

----- Explanation 1. About TMESSAGE TMESSAGE is a VCL predefined structure, defined as follows: struct tMessage {unsigned int msg; // message int wparam; // word parameter int Lparam; // long word parameter int result; // message result};

2. About TForm :: Dispatch (& Message) Custom Message Processing Function It is best to add a TForm :: Dispatch (& Message), this sentence is to let messages continue to pass. If this sentence is not, the message will be completely intercepted, and the VCL class may fail to achieve normal functions due to the message.

----- Example 1: Modify the system menu has some programs, the main window is small, the menu is not, if you want to join the relevant or setup dialog, the best way is to take the system menu. In Windows API programming, modify the system menu is the same as implementing other functions, not too easy, and it will not be too difficult. However, in C Builder, the table class (TFORM) does not provide any properties and methods of the system menu, implementing other features, and the modification system menu seems to be difficult to go on the sky. Fortunately, Borland allows programmers to handle Window messages, so the opportunity is coming! First, use the Window API function to modify the system menu assume that the table is MAINFORM, set the mainform :: oncreate () function:

Use getSystemMenu (Mainform-> Handle, False) to get the system menu handle; modify the system menu with Appendmenu, DELETEMENU, and ModifyMenu function, assign the new ID number to the custom menu item. At this time, you can see that the system menu is also modified, but the custom menu item cannot be responded.

Second, intercept the WM_SYSCOMMAND message in response to the custom menu item in the form of the table (such as Unit1.h)

At the end of the response message added to form a class definition table, a message processing WM_SYSCOMMAND acquired right BEGIN_MESSAGE_MAP MESSAGE_HANDLER (WM_SYSCOMMAND, TMessage, OnWMSysCommand) END_MESSAGE_MAP (TForm) private join message handler declaration area in the form class defined private: // User declarations void __fastcall OnWMSysCommand (TMessage & message); write a message response function void __fastcall TForm1 :: OnWMSysCommand (TMessage & message) {if (Message.WParam == ID_SysMenu_MyItem) {// Your Code Here in file form (e.g. Unit1.h), Do Something } TFORM :: Dispatch (& Message);} 3, full program sample

Example 2: Add OnStartTrack and OneendTrack events to the tracking bar

When the tracking column is used for progress control, OnStartTrack and OneendTrack are likely to be the event you need. For example, when controlling multimedia play progress, when the user moves the slider, you will need the onStartTrack event to stop playback, you need the OneendTrack event to locate the new play position. But Borland did not provide these two events, and I have such programming enthusiasts to self-reliance, and the idea of ​​intercepting Windows messages.

First, intercept the WM_HSCROLL message, add an onStartTrack and OneendTrack event to the trackball

In the table single header file (such as Unit.h)

Add a message response table at the end of the form class definition to give the WM_HSCROLL message processing to the ONWMHScroll function. Begin_MESSAGE_MAP Message_Handler (WM_HSCROLL, TMESSAGE, ONWMHSCROLL) End_MAP (TForm) Join OnWMHScroll function declaration within the Private area defined by the form class. Private: // user declarations void __fastcall onwmhscroll (TMESSAGE & Message); Join StartTrack and EndTrack Function Declaration in the Private area defined by the form class. private: // User declarations void __fastcall TrackBar1StartTrack (TObject * Sender); void __fastcall TrackBar1EndTrack (TObject * Sender); in the form file (e.g. Unit.cpp) OnWMHScroll write function, you can call based on the message parameters and EndTrack function StartTrack In the actual sense, there is an onStartTrack and OneendTrack event. Write the StartTrack and EndTrack functions. If it is a vertical tracking bar, change the above WM_HSCROLL to WM_VScroll. End Borland C Builder programming, intercepting Windows messages is an advanced programming technology that allows you to try to minimize Windows's potential, especially let programmers who have programmed with API have been comfortable. Intercepting the Windows message is the stage where the API is funny. When VCL can't do anything for you, please think of the bottom API.

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

New Post(0)