Intercept Windows Message
C Builder is worthy of Borland's excellent products, use it to develop Windows programs very fast and efficient, 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, put a message Treatment is given to a 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 a message processing function within the Private area of the class declaration. Private: // user declarations void __fastcall message; TMESSAGE & Message; in the form file (such as Unit1.cpp) 3. Write the message handling function, here you need the features you need. For example, 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) The defined message processing function is best to add a TForm :: Dispatch (& Message), this sentence is to let the message 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 the table named Mainform, set the mainform :: oncreate () function: Get the system menu handle with getSystemMenu (Mainform-> Handle, False); use the appendmenu, deletemenu, ModifyMenu function to modify the system menu, Gring the new ID number to a 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, in response to the message interception WM_SYSCOMMAND discretion BEGIN_MESSAGE_MAP MESSAGE_HANDLER custom menu items in the form of header files (e.g. Unit1.h) was added at the end of the message in response to a form class definition table, a message to obtain WM_SYSCOMMAND (WM_SYSCOMMAND, TMessage, OnWMSysCommand) END_MESSAGE_MAP ( TFORM) Add message processing function in the Private area defined in the form class, private: // user declarations void __fastcall onwmsysCommand (TMESSAGE & Message); write message response function void __fastcall tform1 :: onwmsysCall TFORM1: ONWMSYSCOMMAND in the form file (such as Unit1.h) (TMessage & Message) {if (message.wparam == id_sysmenu_myitem) {// Your Code Here, Do Something} TForm :: Dispatch (& Message);} III: 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 the onStartTrack and OneendTrack event to the tracking bar (such as Unit.h) to add a message response table at the end of the form class definition, and 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.