Digital Custom Message (below)

zhaozj2021-02-16  51

In the front, we discussed the delivery method of the message in your own class and the group of messaging, the next question is how to deliver messages to different classes. In fact, in "Resolution of VC 6", we discussed how to get other classes of other classes in different classes, and we can easily want to be in various classes in various classes in different classes. Pass the message. First, the way the message is defined is the same as what we are mentioned above (for example, send a message to the view class in the frame class): (1) Define the message value in the view class .cpp: #define WM_MSG (WM_USER 101) (2) First, add a message declaration in the AFX_MSG block: In CMYVIEW.H, find the following section, and join the message declaration: protected: // {{AFX_MSG (CMYVIEW) ... AFX_MSG LRESULT OnMYMSG (WParam WPARAM, LPARAM lParam); file: //}} AFX_MSG (3) was added in MESSAGE_MAP ON_MESSAGE macro block: BEGIN_MESSAGE_MAP (CMyView, CView) file: // {{AFX_MSG_MAP (CMyView) ..... ON_MESSAGE (WM_MSG, OnMyMsg) File: //}} AFX_MSG_MAP END_MESSAGE_MAP () (4) Add Message Function: LpeSult CMYView :: OnMymsg (WPARAM WPARAM, LPARAM LPARAM) {AFXMessageBox ("Message has been received!"); return 0;} (5) Add Test Function in the Main Framework Void CMAINFRAME :: OnTestMSG () {cView * pView = getActiveView (); // Get the current viewer pointer IF (pView! = Null) PView-> Postmessage (WM_MSG, 0, 0);} Here We see that as long as we have a way to get the target class of the send message, we can send a message, refer to the method in "Resolving the pointer in VC 6", Let's go!

Message transfer we speak in front of other applications is based on the same application, but in some cases we may need to send messages to other applications, then we can use the sendMessage () function to the target application. The handle of a window sends a message. The skill is to get the handle of the window. At the same time, use the RegisterWindowMessage () function to create a unique message and two applications understand each other's meaning. At the same time, the brodcastsySystemMessage () function can be used, which can send a message to the main window of each application in the system. This avoids the problem of obtaining another application window handle. The BroadcastSystemMessage () function provides additional flag BSF_LPaRpoInter, which can convert pointers that write parameters LPARAM to be used by the target program to access the program space, but this flag may not have documentation. The method is as follows: First register your own window message. However, this time we don't have to use the technology of WM_USER 1, the benefits of registration window messages are not to consider WM_USER, and if the message identifier is excess the allowable range of the project. This example uses a text string in two engineering to register a message. Since this text string should be unique in the entire system, a COM technology called GUID will be used to name a message. The GUID Name Generator program can be found in the MFC / bin directory, and its executable file name is Guidgen.exe. The program will generate a text string that is known to be known in the application, which is of course the best for the application. 1) Register a unique window message to generate a GUID using guidgen.exe. Define GUID as window message text strings: #define hello_msg "{6047ccb1-e4e7-11d1-9b7e-00aa003d8695}" :: registerWindowsMessage () Register this window message text string: idhellomsg = :: registerWindowMessage Hello_MSG); save the message identifier IDHELLOMSG, which is easy to use. 2) Send a message to other applications Use the message identifier returned by: registerWindowsMessage (), you can use the following code :: SendMessG, WParam, LPARAM, or above, assume that you can get in advance The handle of a window of a target application. A pointer to the CWND class cannot work outside of the program. However, you can package the obtained window handle in the CWND class, and send a message as follows: CWND WND; WND.ATTACH (HWND); WND.sendMessage (IDHellomsg, WPARAM, LPARAM); 3) Receive registered window messages message reception window is registered, it is necessary in the receiving window class, is generally added to CMainFrame ON_REGISTERED_MESSAGE message to the message map macro manually: BEGIN_MESSAGE_MAP (CMainFrame, CMDIFrameWnd) // {{AFX_MSG_MAP (CMainFrame) //}} AFX_MSG_MAPON_REGISTERED_MESSAGE (idHelloMsg, OnHellomsg) end_MESSAGE_MAP () The code for message processing functions about the registered message is as follows: LRESULT CMAINFRAME :: OnHellomsg (WPARAM WPARAM, LPARAM LPARAM) {// Process Messagereturn 0;} This instance is now assumed to be in advance The handle of a window for a target application is obtained. But this is a difficult task. A simple way is to broadcast a message to each application and want the target program being listening.

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

New Post(0)