Block Windows Message with BCB

xiaoxiao2021-03-06  70

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 sealed. They always have more enthusiasm for achieving other ideas, modifying the system menu and adding StartTrack and Ndtrack events for tracking columns. Just of them. 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 ...

---- Intercepting Windows Messages requires the following steps:

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

---- 1. Create a message mapping table in the class declaration, and give the handling of a message to the custom message processing function.

Begin_MESSAGE_MAP

Message_handler (Windows Message Name, TMessage, Message Processing Function)

Message_handler (...)

END_MESSAGE_MAP (TFORM)

---- 2. Declaration Message Processing Functions in the Private Area of ​​Class Declaration.

PRIVATE: // user declarations

VOID __FASTCALL message processing function name (tMessage & Message);

In the form file (such as Unit1.cpp)

---- 3. Write the message handling function, here you implement the features you need. such as

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, defined as follows:

Struct tMessage

{

Unsigned int msg; // message

INT wparam; // word parameter

INT LPARAM; // long word parameters

Int result; // message result

}

---- 2. About TFORM :: Dispatch (& Message)

---- Custom message processing function is best added to 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 System Menu

---- There are some programs, the main window is small, the menu is not, if you want to join about or setup dialogs, 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 process Window messages, so the opportunity is coming!

First, modify the system menu with the Window API function

Assume that the form is MAINFORM, set the mainform :: oncreate () function:

1. Get the system menu handle with GetSystemMenu (Mainform-> Handle, False);

2. Modify the system menu with Appendmenu, DELETEMENU, and ModifyMenu function, assign 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, intercept the WM_SYSCOMMAND message to respond to custom menu items

In the form of a single header (such as Unit1.h)

1. Add a message response table at the end of the form class, obtain the processing of the WM_SYSCOMMAND message.

Begin_MESSAGE_MAP

Message_handler (WM_SYSCOMMAND, TMESSAGE, ONWMSYSCOMMAND)

END_MESSAGE_MAP (TFORM)

2. Add a message processing function declaration within the Private area defined by the form class.

PRIVATE: // user declarations

Void __fastcall onwmsysCommand (TMESSAGE & Message);

In the form file (such as Unit1.h)

3. Write a message response function

Void __fastcall tform1 :: onwmsyscommand (tMessage & Message)

{

IF (message.wparam == id_sysmenu_myitem)

{

// Your Code Here, Do Something

}

TFORM :: Dispatch (& Message);

}

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)

1. Add a message response table at the end of the form class definition to hand over the WM_HSCROLL message processing to the ONWMHScroll function.

Begin_MESSAGE_MAP

Message_Handler (WM_HSCROLL, TMESSAGE, ONWMHSCROLL)

END_MESSAGE_MAP (TFORM)

2. Add the ONWMHScroll function declaration within the Private area defined by the form class.

PRIVATE: // user declarations

Void __fastcall onWmhscroll (TMESSAGE & Message);

3. Join the StartTrack and EndTrack function declaration within the Private area defined by the form class. PRIVATE: // user declarations

Void __fastcall trackbar1starttrack (TOBJECT * Sender);

Void __fastcall trackbar1ndtrack (TOBJECT * Sender);

In the form file (such as Unit.cpp)

4. Write the OnWMHScroll function, so that it can call the StartTrack and EndTrack function according to the message parameter, generate an onStartTrack and OneendTrack event in the actual sense.

5. Write the StartTrack and EndTrack function.

If it is a vertical tracking bar, change the above WM_HSCROLL to WM_VScroll.

Borland C Builder programming, intercepting Windows messages is an advanced programming technology that allows you to try to explore 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-92091.html

New Post(0)