Message Mechanism in Windows and message mechanism in windows

Source: Internet
Author: User

Message Mechanism in Windows and message mechanism in windows

1) Add the window to the clipboard viewer chain.

Pass in the window handle through SetClipboardViewer (). All the window handles that monitor the clipboard form a linked list (later in front ). In this way, when the clipboard content changes, Windows will send messages to these Windows. Clipboard is a function of the system. Each system function has a message chain. If a user program wants to use a system function, it first registers its program window to the message chain of the specified function. If you understand a system function as a device, it is like using a processing function for files or devices in Windows. Each device in the system has its own functions. Every system action should be told to the program that needs this device. Who needs to know this behavior? The program container that wants to know the current behavior or status of the device first registers itself to the message linked list of the device.

Each device sends messages only to the program window that has been registered in its own message linked list, indicating the current status or behavior of the device. The message is sent only to the program in the message chain. This is easy to understand. It makes no sense to send a message in its own behavior status to a program window that does not care about the device. You can see from here. Windows messages must be sent to a specified object or to all devices. From the user's point of view, it is to send to all users or all users.

2) Remove the window from the clipboard viewer chain before itis destroyed.

First, register the user's program window to the message linked list of a function or device. The operation corresponding to registration is deregistration. When the program window does not need to know the behavior status of the device or function, you need to delete yourself from the device message chain. Changyunboardchain () is the cancellation process corresponding to SetClipboardViewer (). When you need to know a system function or device, you need to register your program window to the device's message chain. When you no longer need to know the behavior status of the device, you need to delete your program window from the device message chain.

3) Interaction

Why register? Because you want to know the behavior status of a function or device of the system. The reason for cancellation is that you do not care about the behavior status of the specified device or a system function. The process of registration and cancellation is a response. Registration is to grasp the status or behavior of a system device and reflect it in its own program window. That is to say, when the system device reaches a certain state, the user's program window must have some actions or actions to interact with it. In fact, sending and receiving messages are the interaction between the sender and receiver. First, establish connections with each other, communicate with each other at a specific time, behavior, or status, and finally restore the contact to the initial state. In this case, is the initial status unrelated? No. The initial status is not completely unrelated.

The so-called initial status is a contact mechanism, that is, message sending and receiving, registration and cancellation of message chains. These mechanisms, methods, and principles have been established. It is up to the user to decide when to use these mechanisms to implement specific functions as the user's program window. Different program development environments provide different methods for interacting with system messages. However, as a programming environment, there will be thousands of changes. As a system, there is only one rule.

4) message chain

When registering a user's program window to a message chain, you do not need to pay attention to the structure changes of the message chain. This process is completed by the system. The problem is that when the user's program window exits from the message chain. When a program window exits from the message chain, the message chain structure changes. At this time, who is required to maintain the message chain? On the one hand, the system maintains the increase or decrease of the message chain, and on the other hand, the exit behavior of the message chain is maintained by the user's program window. So is the maintenance of the message chain completed by the system or by the user's own program window? The specific system functions or devices provide different messages and services, which must be analyzed based on the actual situation.

 

// The following content is available in the C ++ Builder programming environment

1 class TForm1: public TForm 2 {3 public: // User declarations 5 HWND NextViewerHandle; 6 void _ fastcall OnWMDrawClipboard (TMessage & Msg ); 7 void _ fastcall handle (TMessage & Msg); 8 BEGIN_MESSAGE_MAP 9 handle (response, TMessage, OnWMDrawClipboard); 10 handle (WM_CHANGECBCHAIN, TMessage, response); 11 END_MESSAGE_MAP (TForm) 12} 13 14 // 1) register the message chain 15 void _ fastcall TForm1: FormCreate (TObject * Sender) 16 {17 NextViewerHandle = SetClipboardViewer (Handle); 18}
19 // 2) cancel the message chain 20 void _ fastcall TForm1: FormDestroy (TObject * Sender) 21 {22 chang?boardchain (Handle, NextViewerHandle); 23}
24 // 3) Message Processing 25 void _ fastcall TForm1: OnWMDrawClipboard (TMessage & Msg) 26 {27 if (NextViewerHandle! = NULL) 28 {29 SendMessage (NextViewerHandle, Msg. Msg,); 30} 31 // {write your own code} 32} 33
// 4) maintain the message chain structure 34 void _ fastcall TForm1: OnWMChagecbchain (TMessage & Msg) 35 {36 if (HWND) Msg. WParam = NextViewerHandle) 37 {38 NextViewerHandle = (HWND) Msg. LParam; 39} 40 else if (NextViewerHandle! = NULL) 41 {42 SendMessage (NextViewerHandle, Msg. Msg, Msg. WParam, Msg. LParam); 43} 44}

 

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.