Method and device for reusing MFC message mapping code based on QT cross-platform and medium
By defining new event types and message sending functions and combining the mapping mechanism of event processing functions, the code difference problem of MFC programs on the QT platform is solved, the code reuse and maintenance convenience is achieved, and the porting workload is reduced.
Patent Information
- Application Number
- CN202411168124.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2024-12-03
AI Technical Summary
When migrating the MFC-based human-machine interface program developed on Windows platform to Linux platform, the MFC program needs to be converted into a QT program, resulting in large code differences, difficulty in maintaining and updating, and the existing cross-platform communication method fails to provide effective code porting scheme.
By defining new event types WSMEvent and message sending function, encapsulate message content and send it on the QT platform, use event processing functions to map and respond, retain message mapping and responsive code in MFC program, and transform the macro BEGIN_MESSAGE_MAP in MFC to adapt QT platform, reduce code changes.
It realizes the reuse of the message mapping code of the MFC program on the QT platform, which reduces the amount of code modification, improves the code reuse, simplifies the maintenance and update process, and reduces the amount of porting work.
Smart Images

Figure CN119065712A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a train signal control system, and in particular to a method, a device and a medium for reusing MFC message mapping codes based on QT cross-platform. Background Art
[0002] Based on the needs of information innovation, existing systems in use, such as centralized dispatching systems, interlocking systems, ATS systems, and monitoring systems, should be gradually transferred to domestic software and hardware equipment. Domestic operating systems are based on Linux, which poses a great challenge to human-machine interface programs developed based on MFC on the Windows platform. It is necessary to convert existing MFC programs into QT programs, and the workload of migration is relatively large. In particular, the existing MFC program and the transplanted QT program will be deployed simultaneously in the project, and both will have the same functional maintenance and update requirements.
[0003] The difference between MFC and QT is huge. There will be huge differences in code between the existing MFC program and the transplanted QT program. This is very troublesome for the maintenance and update of both. If the difference between the existing MFC program and the transplanted QT program is not particularly large, it will be very beneficial for software maintenance.
[0004] Message mapping is the basic method of MFC program development. Message mapping is necessary for developing programs with MFC. ON_MESSAGE is generally used for program-defined messages. These messages are sent to the target window through the Windows API function SendMessage. The window procedure responds after receiving the message. When developing based on QT, these message mapping macros and SendMessage functions of MFC cannot be used. The basis of QT is signal and slot functions. Signals are similar to Windows messages, and slot functions are similar to MFC message response functions. The message mapping of MFC programs is either transformed into QT signal and slot functions, or directly uses C++ function calls. If the C++ function call method is used, a specific class is required when calling and the abstract feature is lost, or the abstract class defines virtual functions, which makes the abstract class particularly bloated. If the signal and slot function method is used, not only the signal must be designed, but also the signal and slot functions must be connected at the right time. These will bring relatively large code changes.
[0005] If the existing MFC program is no longer maintained, using QT's signal and slot function method is a feasible solution. However, MFC programs need to be continuously maintained because there are a large number of existing engineering applications, and newly transplanted QT programs also need to be continuously maintained. The greater the difference in the code of the two programs, the greater the difficulty and workload of maintenance.
[0006] CN115914330A discloses a heterogeneous inter-application communication method based on the NIO asynchronous thread model. Aiming at the characteristics of cross-language development and cross-operating system of application software in large-scale integrated information systems, and the need for real-time communication between applications according to different business scenario requirements, it combines existing communication protocols and technologies, provides an efficient inter-application communication model, and provides multiple communication modes such as point-to-point, subscription publishing, and shared memory for the application layer. However, it is only a cross-platform communication method, and does not provide a solution for cross-platform program transplantation. Summary of the invention
[0007] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and to provide a method, device and medium for reusing MFC message mapping code based on QT cross-platform, reuse the message mapping related code of the existing MFC program, reduce the amount of code modification during transplantation, maintain the existing program structure, and improve the same code segments of MFC programs and QT programs with the same function.
[0008] The purpose of the present invention can be achieved by the following technical solutions:
[0009] According to a first aspect of the present invention, a method for reusing MFC message mapping code based on QT cross-platform is provided. When the MFC program is ported to QT, the method retains the code related to message mapping, message response and message sending in the MFC program, and reduces the code changes in the porting process, comprising the following steps:
[0010] Define MFC message mapping related data types and function pointer types based on QT;
[0011] Define a new event type WSMEvent and its type value based on QT;
[0012] Define a message sending function based on QT, encapsulate the message content into the newly defined event type WSMEvent, and send it to the target through the message sending function;
[0013] Define an event processing function. When the event processing function receives QEvent type event data, it judges and converts it into WSMEvent event type according to the type value of the QEvent type event.
[0014] The event processing function takes the message ID and message parameter data from the WSMEvent event, searches for the matching information of the message ID according to the message mapping table of the target class, that is, the message mapping entry, and obtains the message response function in the message mapping entry; calls the message response function to process the message parameter data, returns the processing result, and records the processing result in the WSMEvent event;
[0015] Based on the message sending function, the processing result recorded in the WSMEvent event is returned to the caller;
[0016] Modify the macro BEGIN_MESSAGE_MAP in MFC to match the QT platform, so that the message map-related macros DECLARE_MESSAGAE_MAP, BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, ON_MESSAGE can still be used in QT;
[0017] Added macros WM_DECL_WNDMSG and WM_IMPL_WNDMSG to encapsulate the declaration and implementation of event handling related functions into the macro code, making the code of event handling functions patterned.
[0018] As a preferred technical solution, the type value of the event type WSMEvent does not repeat the type values of other event types, and according to QT development rules, the type value is between QEvent::User=1000 and QEvent::MaxUser=65535. It can generally be determined by adding a preset numerical value to QEvent::User, or calling QT's registerEventType function to return a type value.
[0019] As a preferred technical solution, the event type WSMEvent defines a message ID, message parameters wParam and lParam, and a member variable lresult for storing processing results, wherein the types of the message ID, wParam, and lParam are UINT, WPARAM, and LPARAM, respectively, and the type of lresult is LRESULT; the data types related to the MFC message mapping defined based on QT include but are not limited to UINT, WPARAM, LPARAM, and LRESULT.
[0020] As a preferred technical solution, the event processing function is a customEvent virtual function. The event processing function passes the message ID, wParam, and lParam data in WSMEvent to the message processing function, searches for the message response function based on the message mapping table, then calls the message response function and stores the return value of the message response function in the member variable lresult of WSMEvent.
[0021] As an optimal technical solution, the message processing function starts searching for the message response function from the message mapping table of the final class. If the corresponding message response function is not found in the message mapping table of the current level, it continues to search in the message mapping table of the previous level until the root level message mapping table.
[0022] As a preferred technical solution, the message mapping table is described by an AFX_MSGMAP structure, including an acquisition function pointer pfnGetBaseMap and a message mapping entry array of the previous level or parent class level message mapping table. AFX_MSGMAP retains the original form and semantics of MFC. The message mapping table AFX_MSGMAP data is declared by DECLARE_MESSAGE_MAP, and the data is set after being expanded by BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.
[0023] As a preferred technical solution, the message mapping table obtains the message mapping table of the upper level by calling the function pointer pfnGetBaseMap, and when pfnGetBaseMap is a null pointer, it indicates the root message mapping table. The pfnGetBaseMap data in the message mapping table AFX_MSGMAP is set after being expanded by BEGIN_MESSAGE_MAP.
[0024] As a preferred technical solution, the message processing function traverses the message mapping entry array to find the message response function through the AfxFindMessageEntry function, and AfxFindMessageEntry retains the original form and semantics of MFC.
[0025] As a preferred technical solution, the message mapping entry includes a message ID, a message response function and other necessary data, and is described by an AFX_MSGMAP_ENTRY structure, which is generated in a message mapping table after being expanded by an ON_MESSAGE macro. AFX_MSGMAP_ENTRY and ON_MESSAGE retain the original form and semantics of MFC.
[0026] As a preferred technical solution, the calling message response function specifically performs a class function call on the response function pointer in the message mapping entry when the message processing function finds the message mapping entry, and uses the obtained message parameters wParam and lParam data as parameters of the response function call.
[0027] As a preferred technical solution, the transformation of the macro BEGIN_MESSAGE_MAP in MFC is to make appropriate modifications to the macro definition in MFC so that the macro related to the message map matches the QT platform, specifically: the second parameter of BEGIN_MESSAGE_MAP is the base class of the first parameter. When the second parameter is a class in QT, the second parameter is changed to be the same as the first parameter, otherwise, the second parameter is kept consistent with the parameter in the MFC program.
[0028] As a preferred technical solution, the macro BEGIN_MESSAGE_MAP in the modified MFC sets the pfnGetBaseMap data in the message mapping table AFX_MSGMAP by whether the second parameter is the same as the first parameter. When the two parameters are the same, pfnGetBaseMap is set to an empty function pointer, otherwise pfnGetBaseMap points to the acquisition function of the message mapping table of the class declared by the second parameter, that is, the function of obtaining the parent class or the previous level message mapping table.
[0029] As a preferred technical solution, the newly added macro WM_DECL_WNDMSG is used to expand the declaration of the event processing function, and the newly added macro WM_IMPL_WNDMSG is used to expand the implementation of the event processing function.
[0030] As a preferred technical solution, the END_MESSAGE_MAP, DECLARE_MESSAGE_MAP, and ON_MESSAGE macros retain the original MFC form and semantics.
[0031] As an optimal technical solution, different processing is performed according to the target class type of the application, and the target class type includes the message mapping base class and the message mapping inheritance class. The message mapping base class refers to the very first class with a message mapping mechanism or function, and all base classes above this class do not have a message mapping mechanism or function; the message mapping inheritance class refers to a direct base class or indirect base class above this class that has a message mapping mechanism or function, and this class also has a message mapping mechanism or function. The message mapping inheritance class refers to all inheritance classes of the message mapping base class; the method for determining that a class is the message mapping base class is that the direct base class of this class is a class in QT, and in the MFC original program, the direct base class of this class is a class in MFC.
[0032] As a preferred technical solution, when the method is applied to a message mapping inheritance class, the header file and cpp file of an existing MFC program are transplanted, and the code related to the message mapping remains the same as the MFC program; when the method is applied to the most basic class of a message mapping, the header file of an existing MFC program is transplanted, and a WM_DECL_WNDMSG macro application is added to the class definition; the cpp file of an existing MFC program is transplanted, and a WM_IMPL_WNDMSG macro application is added, and the second parameter of BEGIN_MESSAGE_MAP is modified to be the same as the first parameter.
[0033] According to a second aspect of the present invention, there is provided an electronic device, comprising a memory and a processor, wherein a computer program is stored in the memory, and the method described above is implemented when the processor executes the program.
[0034] According to a third aspect of the present invention, there is provided a computer-readable storage medium having a computer program stored thereon, wherein the program implements the method described above when executed by a processor.
[0035] Compared with the prior art, the present invention has the following beneficial effects:
[0036] 1. The message mapping mechanism is the basic technology of MFC. There are a lot of message mapping codes in the software developed based on MFC. Converting these message mapping codes will bring a lot of transplantation workload. The present invention is based on QT technology, realizes the message mapping mechanism of MFC, so that the message mapping part of the existing code can be reused. When LayoutX (MFC program) is transplanted to QT, the code related to message mapping, message response and message sending in the LayoutX code can be retained, so that the LayoutX code changes less when transplanting.
[0037] 2. The QT-based message mapping mechanism of the present invention retains the DECLARE_MESSAGE_MAP macro of the original MFC program code, and all functions related to ON_MESSAGE in the original MFC program header file can be retained and reused.
[0038] 3. The QT-based message mapping mechanism of the present invention retains the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP macros of the original MFC program code, and only makes a minor modification to the second parameter of the BEGIN_MESSAGE_MAP macro: when this class is the most basic class of the message mapping, or the parent class of this class is the QT framework class, the second parameter is the same as the first parameter. Compared with the existing MFC program code, only one modification is made, and the code reuse is high.
[0039] 4. The QT-based message mapping mechanism of the present invention retains the ON_MESSAGE macro of the original MFC program code, so that a large amount of ON_MESSAGE-related codes between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP can be retained and reused.
[0040] 5. The QT-based message mapping mechanism of the present invention requires that the most basic class of the message mapping also uses the WM_DECL_WNDMSG macro while using DECALRE_MESSAGE_MAP, so the amount of newly added code is small and the code reuse is high.
[0041] 6. The QT-based message mapping mechanism of the present invention does not require the use of the WM_DECL_WNDMSG macro for the message mapping inheritance class, which reduces the header file differences of the message mapping inheritance class and increases code reuse.
[0042] 7. The QT-based message mapping mechanism of the present invention requires that the message mapping base class also uses the WM_IMPL_WNDMSG macro while using BEGIN_MESSAGE_MAP. The second parameter of WM_IMPL_WNDMSG is its C++ base class, that is, the class in the QT framework.
[0043] 8. The QT-based message mapping mechanism of the present invention does not require the use of the WM_IMPL_WNDMSG macro for the message mapping inheritance class, which makes the cpp file differences of the message mapping inheritance class small and the code reuse is high.
[0044] 9. The present invention avoids using QT's signal slot mechanism to reconstruct the ON_MESSAGE related functions of existing applications. QT's signal slot mechanism requires explicit connection of signals, slots, signal objects, and slot objects during runtime, and the timing of connection must be appropriate. The QT-based message mapping mechanism is hidden in the macro structure of the code. The message mapping structure is established before entering the main function. There is no need to worry about the timing of the connection, so the transplantation workload is small.
[0045] 10. The QT-based message mapping mechanism of the present invention makes the code of the existing program and the message mapping-related code very similar, which will be extremely convenient when maintaining two sets of codes based on MFC and QT and adding new functions. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 is a flow chart of the method of the present invention;
[0047] Figure 2 This is the data processing flow chart of the event processing function customEvent;
[0048] Figure 3 It is the data processing flow chart of the message processing function;
[0049] Figure 4 A comparison of message definitions between an MFC program and a QT program in one embodiment;
[0050] Figure 5 A comparison of message sending functions between an MFC program and a QT program in one embodiment;
[0051] Figure 6 A comparison of message sending and processing between an MFC program and a QT program in one embodiment;
[0052] Figure 7 A comparison of application header files of an MFC program and a QT program in an embodiment;
[0053] Figure 8A comparison of application cpp files of an MFC program and a QT program in an embodiment;
[0054] Fig. 9 A message mapping structure of an existing application based on MFC in an embodiment;
[0055] Fig.10 A QT-based message mapping structure in one embodiment. DETAILED DESCRIPTION
[0056] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0057] Unless otherwise defined, the technical terms or scientific terms involved in this application should be understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the" and the like involved in this application do not indicate a quantitative limitation, and may represent the singular or plural. The terms "include", "comprise", "have" and any of their variations involved in this application are intended to cover non-exclusive inclusions; for example, a process, method, system, product or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units that are not listed, or may also include other steps or units inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there may be three relationships, for example, "A and / or B" can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects before and after are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.
[0058] In the process of porting the site map operation and display control LayoutX to QT, in order to reduce the code difference before and after the porting, some contents in the LayoutX code that depend on the MFC library are redesigned based on QT. For example, in the site map display and operation control LayoutX, there is such code:
[0059] BEGIN_MESSAGE_MAP(CLayoutFrame,CFrameWnd)
[0060] / / {{AFX_MSG_MAP(CLayoutFrame)
[0061] ON_MESSAGE(UM_AUTOREPLAY_S,OnAutoReplay)
[0062] ON_MESSAGE(UM_BEGINREPLAY_P,OnBeginReplay)
[0063] ON_MESSAGE(UM_REPLAYICON_S,OnReplayIcon)
[0064] ON_MESSAGE(UM_ENDREPLAY_P,OnEndReplay)
[0065] ON_MESSAGE(UM_STOPRELAY_P,OnStopReplay)
[0066] ON_MESSAGE(UM_ISREPLAYING_S,OnIsReplaying)
[0067] ON_MESSAGE(UM_PLAYBACK_S,OnPlaybackStart)
[0068] ON_MESSAGE(UM_PLAYBACKICONCHANGE_S,OnPlaybackIconChange)
[0069] ON_MESSAGE(UM_STOPPLAYBACK_S,OnPlaybackStop)
[0070] ON_MESSAGE(UM_PLAYBACKLOGCHANGE_S,OnPlaybackLogChange)
[0071] ON_MESSAGE(UM_OUTERCOMMAND_ENABLE,OnChangeBtnEnable)
[0072] ON_MESSAGE(UM_SET_PRIORITY_S,OnSetPriority)
[0073] ON_MESSAGE(UM_SHOWTOOLBAR_P,OnShowToolbar)
[0074] ON_MESSAGE(UM_SETSTATIONCTRLMODE_S,OnSetStationCtrlMode)
[0075] ON_MESSAGE(UM_SETROUTECTRLMODE_S,OnSetRouteCtrlMode)
[0076] ON_MESSAGE(UM_SETSTATIONSTATUS_P,OnSetStationStatus)
[0077] ON_MESSAGE(UM_SETROUTECREATERESULT_P,
[0078] OnSetRouteCreateResult)
[0079] ON_MESSAGE(UM_ROUTECREATEFOLLOWINGBUTTON_S,
[0080] OnRouteCreateFollowingButton)
[0081] ON_MESSAGE(UM_PRINTINFO_S,OnPrintMsgInfo)
[0082] ON_MESSAGE(UM_SETROUTECREATDLG_P,OnSetRouteCreatDlg)
[0083] ON_MESSAGE(UM_SETROUTETRAINID_S,OnSetRouteTrainID)
[0084] ON_MESSAGE(UM_ICONCHANGE_S,OnIconChange)
[0085] ON_MESSAGE(UM_STATIONCM_REQUEST,OnStationCMRequest)
[0086] ON_MESSAGE(UM_SETCTCMDFLAG_S,OnSetCTCmdFlag)
[0087] ON_MESSAGE(UM_SETTCCSTATUS_P,OnSetTccStatus)
[0088] ON_MESSAGE(UM_ADDDEVICENOTE,OnAddDeviceNote)
[0089] / / }}AFX_MSG_MAP
[0090] END_MESSAGE_MAP()
[0091] The above code is the message map of the MFC program, in which BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, and ON_MESSAGE together constitute the data structure required for the MFC message map. Message mapping is the basic method of MFC program development. Message mapping must be used when developing programs with MFC. ON_MESSAGE is generally used for program-defined messages. These messages are sent to the target window through the Windows API function SendMessage. The window procedure responds after receiving the message. When developing based on QT, these MFC message mapping macros and SendMessage functions cannot be used.
[0092] Based on this, this embodiment provides a method for reusing MFC message mapping code based on QT cross-platform. When porting an MFC program to QT, this method retains the code related to message mapping, message response and message sending in the MFC program, thereby reducing code changes during the porting process. Figure 1 As shown, the method comprises the following steps:
[0093] S1, define MFC message mapping related data types and function pointer types based on QT.
[0094] The data types and function pointer types related to MFC message mapping defined based on QT include but are not limited to UINT, WPARAM, LPARAM, LRESULT, AFX_MSGMAP, AFX_MSGMAP_ENTRY, AFX_PMSG, etc.
[0095] S2, defines a new event type WSMEvent and its type value based on QT.
[0096] In this embodiment, the WSMEvent event type is defined as follows:
[0097] class WSMEvent:public QEvent
[0098] {
[0099] UINT message;
[0100] WPARAM wParam;
[0101] LPARAM lParam;
[0102] LRESULT lresult;
[0103] };
[0104] The event type WSMEvent inherits from the QEvent event, and its type value is determined according to the type value of the received QEvent type event.
[0105] In this embodiment, the message ID in the Windows system, the parameters wParam and lParam for storing the message, and the return value lresult of the message processing are stored in the WSMEvent type. The Type of the new type can adopt QEvent::User+1000, that is, 2000. It is necessary to ensure that the type value is not used, or call Qt's registerEventType function to return a usable type value.
[0106] S3, based on QT, defines a message sending function SendMessage, encapsulates the message content into a newly defined event type WSMEvent, and sends it to the target through the message sending function.
[0107] The SendMessage function uses QT's sendEvent function to send, and the message is received through QT's customEvent function.
[0108] In this embodiment, the SendMessage function is implemented as follows:
[0109] LRESULT SendMessage(QObject*receiver,UINT nMessage,
[0110] WPARAM wParam,LPARAM lParam)
[0111] {
[0112] WSMEvent event(nMessage,wParam,lParam);
[0113] QCoreApplication::sendEvent(receiver,&event);
[0114] return event.lresult;
[0115] }
[0116] Keep the same concept, form and parameters as SendMessage under Windows platform.
[0117] S4, define an event processing function customEvent. When the event processing function receives QEvent type event data, it judges and converts it into WSMEvent event type according to the type value of the QEvent type event.
[0118] The customEvent function is a virtual function that handles custom event types under the QT platform. In this function, the Type value of the QEvent event is used to determine whether to convert it to a WSMEvent event.
[0119] S5, the data processing process of the event processing function customEvent is as follows Figure 2 As shown, the event processing function takes out the message ID and message parameter data from the WSMEvent event and passes them to the message processing function CWnd_OnWndMsg. The message processing function searches for the matching information of the message ID according to the message mapping table of the target class, that is, the message mapping entry, and obtains the message response function in the message mapping entry; calls the message response function to process the message parameter data, returns the processing result, and stores the processing result in the member variable lresult of the WSMEvent event.
[0120] The message map, DECLARE_MESSAGE_MAP, BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, and ON_MESSAGE maintain the same concepts as MFC.
[0121] The message processing function CWnd_OnWndMsg starts looking for the message response function from the message mapping table of the final class. If the corresponding message response function is not found in the message mapping table of the current level, it continues to search in the message mapping table of the previous level until the root level message mapping table.
[0122] The message map is described by the AFX_MSGMAP structure, which is defined as follows:
[0123] struct AFX_MSGMAP
[0124] {
[0125] const AFX_MSGMAP*(PASCAL*pfnGetBaseMap)();
[0126] const AFX_MSGMAP_ENTRY*lpEntries;
[0127] };
[0128] It includes the message mapping table of the previous level or parent class level and the message mapping entry array. pfnGetBaseMap is a function pointer. Calling pfnGetBaseMap can obtain the message mapping table of the previous level. When pfnGetBaseMap is a null pointer, it indicates that this is the root message mapping table. lpEntries is the message mapping entry array.
[0129] The specific method of calling the message response function is that when the message processing function CWnd_OnWndMsg finds the message mapping entry, the response function pointer in the message mapping entry is called as a class function, and the obtained message parameters wParam and lParam data are used as parameters of the response function call.
[0130] The CWnd_OnWndMsg function is defined as follows:
[0131] BOOL CWnd_OnWndMsg(QWidget*pWnd,UINT message,
[0132] WPARAM wParam,LPARAM lParam,LRESULT*pResult,
[0133] const AFX_MSGMAP*pMessageMap)
[0134] {
[0135] …
[0136] }
[0137] The data processing process of the message processing function CWnd_OnWndMsg is as follows Figure 3 As shown in the figure, the function code starts to search for the message response function from the message map table of the final class until the root message map table. The AfxFindMessageEntry function is used to find the message response function in the message map table (i.e., array) by looping through the entries of the array. The message map entry is the AFX_MSGMAP_ENTRY structure, which is generated in the message map table after the ON_MESSAGE macro is expanded. The message map entry includes the message ID, message response function, and other necessary data.
[0138] S6, based on the message sending function, returns the processing result recorded in the WSMEvent event to the caller.
[0139] S7. Modify the macro BEGIN_MESSAGE_MAP in MFC. The second parameter of BEGIN_MESSAGE_MAP is the base class of the first parameter. When the second parameter is a class in QT, change the second parameter to be the same as the first parameter. Otherwise, keep the second parameter consistent with the parameter in the MFC program.
[0140] When the second parameter of BEGIN_MESSAGE_MAP is the same as the first parameter, pfnGetBaseMap in AFX_MSGMAP is automatically set to a null function pointer, otherwise pfnGetBaseMap points to the acquisition function of the message mapping table of the class declared by the second parameter (i.e., the previous level).
[0141] In this embodiment, for example:
[0142] class CLayoutFrame:public QFrame
[0143] {
[0144] …
[0145] };
[0146] The CLayoutFrame class inherits from the QFrame class in QT. The QFrame class does not have the MFC message map table, so BEGIN_MESSAGE_MAP needs to be modified. In its cpp file, BEGIN_MESSAGE_MAP is applied as follows:
[0147] BEGIN_MESSAGE_MAP(CLayoutFrame,CLayoutFrame)
[0148] When linking the parent class message map, BEGIN_MESSAGE_MAP sets the upper message map of CLayoutFrame to nullptr. For the subclass of CLayoutFrame, the second parameter of the BEGIN_MESSAGE_MAP macro is exactly the same as the code of LayoutX (existing MFC program).
[0149] The method of transforming BEGIN_MESSAGE_MAP solves the link of the message mapping table under the QT platform, so that the macros such as DECLARE_MESSAGE_MAP, BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, ON_MESSAGE in the existing code maintain the original form and semantics of MFC.
[0150] S8, added macros WM_DECL_WNDMSG and WM_IMPL_WNDMSG, encapsulate the declaration and implementation of event handling related functions into macro code, making the code of event handling functions patterned. Macro WM_DECL_WNDMSG is used to expand the declaration of event handling functions, and macro WM_IMPL_WNDMSG is used to expand the implementation of event handling functions. For example:
[0151] #define WM_DECL_WNDMSG()\
[0152] protected:virtual void customEvent(QEvent*event);
[0153] When applying the method, it is necessary to distinguish whether the target class of the application is the message mapping base class or the message mapping inheritance class. The message mapping base class is the first class that has the message mapping mechanism or function, and all base classes above this class do not have the message mapping mechanism or function; the message mapping inheritance class refers to a direct base class or indirect base class above this class that has the message mapping mechanism or function, so this class also has the message mapping mechanism or function. The message mapping inheritance class can generally refer to all inheritance classes of the message mapping base class. The simplest way to determine that a class is the message mapping base class is that the direct base class of this class is a class in QT, while in the original MFC program, the direct base class of this class is a class in MFC.
[0154] When the method is applied to the message map inheritance class, the header file and cpp file of the existing MFC program are transplanted, and the code related to the message map remains the same as the MFC program. When the method is applied to the message map base class, the header file of the existing MFC program is transplanted, and the WM_DECL_WNDMSG macro application needs to be added in the class definition; when transplanting the cpp file of the existing MFC program, the WM_IMPL_WNDMSG macro application needs to be added, and the second parameter of BEGIN_MESSAGE_MAP needs to be modified to be the same as the first parameter.
[0155] Transplant the header file of LayoutX (existing MFC program), such as CLayoutFrame class file. This class is the most basic class of message mapping. It is necessary to add WM_DECL_WNDMSG macro application in the class definition; the code form is as follows:
[0156] class CLayoutFrame:public QFrame
[0157] {
[0158] WM_DECL_WNDMSG() / / Newly added macro to adapt to QT
[0159] DECLARE_MESSAGE_MAP()
[0160] …
[0161] }
[0162] Transplant the cpp file of LayoutX (existing MFC program), such as the cpp file of CLayoutFrame. This class is the most basic class of message mapping, and needs to add WM_IMPL_WNDMSG macro application. The code form is as follows:
[0163] WM_IMPL_WNDMSG(CLayoutFrame,QFrame) / / Newly added macro to adapt to QT
[0164] BEGIN_MESSAGE_MAP(CLayoutFrame,CLayoutFrame)
[0165] ON_MESSAGE(…)
[0166] …
[0167] END_MESSAGE_MAP()
[0168] In the above code, all the codes that use ON_MESSAGE to respond to custom messages remain in the original code form.
[0169] Other message mapping related classes of LayoutX (existing MFC program) are modified with reference to the above transplantation method, which will not be described in detail in this embodiment. The message mapping inherited classes of LayoutX (existing MFC program) are transplanted while keeping the message mapping macro unchanged, which will not be described in detail in this embodiment.
[0170] Figure 4 Compare the message definitions of MFC programs and QT programs. The most important contents of the ported code are the message ID, parameters wParam, lParam and return value. Since the customEvent virtual function has no return value, the return value is placed in the WSMEvent structure.
[0171] Figure 5 Compare the message sending functions of MFC programs and QT programs. Figure 5 It can be seen that the SendMessage function has the same form, except that the first parameter is different in form. The code that uses the SendMessage function in existing programs does not need to be modified.
[0172] Figure 6 Compare the message sending and processing process between MFC program and QT program. Figure 6It can be seen that the message mapping method based on QT design has similar functions to the MFC message mapping method, and can effectively locate and call message functions.
[0173] Figure 7 Compare the application header files of MFC program and QT program. Figure 7 It can be seen that CLayoutFrame is the most basic class of message mapping, and only a few parts of the header file need to be modified; CLayoutFrame2 is the inherited class of message mapping, and the header file remains as it is.
[0174] Figure 8 Compare the application cpp files of MFC program and QT program. Figure 8 It can be seen that CLayoutFrame is the most basic class of message mapping, and the part of the cpp file related to message mapping only needs to be modified slightly; CLayoutFrame2 is the inherited class of message mapping, and the cpp file remains the same. All ON_MESSAGE macro codes can be kept as they are.
[0175] Fig. 9 and Fig.10 is a comparison of message map structures.
[0176] Fig. 9 It is the message mapping structure for existing applications based on MFC. Fig. 9 The structure is described in detail in "MFC in Simple Terms (2nd Edition)" (written by Hou Junjie). This embodiment intercepts the relevant parts according to the characteristics of existing applications. The message mapping structure linked list of all programs developed by MFC has a common root, that is, the message mapping structure defined by the CCmdTarget class.
[0177] Fig.10 It is a QT-based message mapping structure. Fig.10 It can be seen that the message mapping table designed based on QT has nothing to do with the QT framework class. The CLayoutFrame class of the application is the base class of the message mapping and the root of the message mapping linked list. The message mapping linked lists based on QT have no common root because they do not have a common message mapping base class.
[0178] The electronic device of the present invention includes a central processing unit (CPU), which can perform various appropriate actions and processes according to computer program instructions stored in a read-only memory (ROM) or loaded from a storage unit into a random access memory (RAM). In the RAM, various programs and data required for device operation can also be stored. The CPU, ROM and RAM are connected to each other via a bus. An input / output (I / O) interface is also connected to the bus.
[0179] Multiple components in the device are connected to the I / O interface, including: input units, such as keyboards, mice, etc.; output units, such as various types of displays, speakers, etc.; storage units, such as disks, optical disks, etc.; and communication units, such as network cards, modems, wireless communication transceivers, etc. The communication unit allows the device to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunication networks.
[0180] The processing unit performs the various methods and processes described above, such as methods S1 to S8. For example, in some embodiments, methods S1 to S8 may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit. In some embodiments, part or all of the computer program may be loaded and / or installed on the device via a ROM and / or a communication unit. When the computer program is loaded into the RAM and executed by the CPU, one or more steps of methods S1 to S8 described above may be performed. Alternatively, in other embodiments, the CPU may be configured to execute methods S1 to S8 in any other appropriate manner (e.g., by means of firmware).
[0181] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0182] The program code for implementing the method of the present invention can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code can be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.
[0183] In the context of the present invention, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0184] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed by the present invention, and these modifications or replacements should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A method of reusing MFC message mapping code based on QT cross-platform, It is characterized in that This method retains the code related to message mapping, message response and message sending in the MFC program when porting the MFC program to QT, reducing the code changes in the porting process, and includes the following steps: Define MFC message mapping related data types and function pointer types based on QT; Define a new event type WSMEvent and its type value based on QT; Define a message sending function based on QT, encapsulate the message content into the newly defined event type WSMEvent, and send it to the target through the message sending function; Define an event processing function. When the event processing function receives QEvent type event data, it judges and converts it into WSMEvent event type according to the type value of the QEvent type event. The event processing function takes the message ID and message parameter data from the WSMEvent event, searches for the matching information of the message ID according to the message mapping table of the target class, that is, the message mapping entry, and obtains the message response function in the message mapping entry; calls the message response function to process the message parameter data, returns the processing result, and records the processing result in the WSMEvent event; Based on the message sending function, the processing result recorded in the WSMEvent event is returned to the caller; Modify the macro BEGIN_MESSAGE_MAP in MFC to match the QT platform, so that the message map-related macros DECLARE_MESSAGAE_MAP, BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, ON_MESSAGE can still be used in QT; Added macros WM_DECL_WNDMSG and WM_IMPL_WNDMSG to encapsulate the declaration and implementation of event handling related functions into the macro code, making the code of event handling functions patterned.
2. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The type value of the event type WSMEvent is not repeated with the type values of other event types, and according to QT development rules, the type value is between QEvent::User=1000 and QEvent::MaxUser=65535. It is determined by adding a preset value to QEvent::User, or calling QT's registerEventType function to return a type value.
3. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The event type WSMEvent defines a message ID, message parameters wParam and lParam, and a member variable lresult for storing processing results, wherein the types of the message ID, wParam, and lParam are UINT, WPARAM, and LPARAM, respectively, and the type of lresult is LRESULT; the data types related to the MFC message mapping include but are not limited to UINT, WPARAM, LPARAM, and LRESULT.
4. A method for reusing MFC message mapping code based on QT cross-platform according to claim 3, It is characterized in that The event processing function is a customEvent virtual function. The event processing function passes the message ID, wParam, and lParam data in WSMEvent to the message processing function. The message processing function searches for the message response function based on the message mapping table to process the message and stores the return value of the message processing in the member variable lresult of WSMEvent.
5. A method for reusing MFC message mapping code based on QT cross-platform according to claim 4, It is characterized in that The message processing function starts to search for the message response function from the message mapping table of the final class. If the corresponding message response function is not found in the message mapping table of the current level, it continues to search in the message mapping table of the previous level until the root level message mapping table.
6. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The message mapping table is described by the AFX_MSGMAP structure, including the function pointer pfnGetBaseMap for obtaining the message mapping table of the previous level or parent class level and the message mapping entry array; AFX_MSGMAP retains the original form and semantics of MFC, the message mapping table AFX_MSGMAP data is declared by DECLARE_MESSAGE_MAP, and the data is set after being expanded by BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.
7. A method for reusing MFC message mapping code based on QT cross-platform according to claim 5 or 6, It is characterized in that The message mapping table obtains the message mapping table of the upper level by calling the function pointer pfnGetBaseMap. When pfnGetBaseMap is a null pointer, it indicates the root message mapping table. The pfnGetBaseMap data in the message mapping table AFX_MSGMAP is expanded and set by BEGIN_MESSAGE_MAP.
8. A method for reusing MFC message mapping code based on QT cross-platform according to claim 5 or 6, It is characterized in that The message processing function searches for the message response function in the message mapping entry array through the AfxFindMessageEntry function, and AfxFindMessageEntry retains the original form and semantics of MFC.
9. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, 5 or 6, It is characterized in that The message mapping entry includes a message ID, a message response function and other necessary data, and is described by an AFX_MSGMAP_ENTRY structure, which is generated in the message mapping table after the ON_MESSAGE macro is expanded. AFX_MSGMAP_ENTRY and ON_MESSAGE retain the original form and semantics of MFC.
10. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The calling of the message response function specifically involves performing a class function call on the response function pointer in the message mapping entry when the message processing function finds the message mapping entry, and using the obtained message parameters wParam and lParam data as parameters of the response function call.
11. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The described transformation of the macro BEGIN_MESSAGE_MAP in MFC is to appropriately modify the macro definition in MFC so that the macro related to the message map matches the QT platform, specifically: the second parameter of BEGIN_MESSAGE_MAP is the base class of the first parameter, when the second parameter is a class in QT, the second parameter is changed to be the same as the first parameter, otherwise, the second parameter is kept consistent with the parameter in the MFC program.
12. A method for reusing MFC message mapping code based on QT cross-platform according to claim 11, It is characterized in that The described transformation of the macro BEGIN_MESSAGE_MAP in MFC sets the pfnGetBaseMap data in the message mapping table AFX_MSGMAP by whether the second parameter is the same as the first parameter. When the two parameters are the same, pfnGetBaseMap is set to a null function pointer, otherwise pfnGetBaseMap points to the acquisition function of the message mapping table of the class declared by the second parameter, that is, the pointer to the acquisition function of the parent class or the previous level message mapping table.
13. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The newly added macro WM_DECL_WNDMSG is used to expand the declaration of the event processing function, and the newly added macro WM_IMPL_WNDMSG is used to expand the implementation of the event processing function.
14. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The END_MESSAGE_MAP, DECLARE_MESSAGE_MAP, and ON_MESSAGE macros retain the original MFC form and semantics.
15. A method for reusing MFC message mapping code based on QT cross-platform according to claim 1, It is characterized in that The code patternization of the event processing function refers to the method using the defined message mapping related macros to implement the message mechanism when applying, so as to achieve the purpose of reusing the MFC message mapping code; when applying the defined message mapping related macros, different processing is performed according to the target class type of the application, and the target class type includes the message mapping most base class and the message mapping inheritance class. The message mapping most base class refers to the very first class with the message mapping mechanism or function, and all base classes above this class do not have the message mapping mechanism or function; the message mapping inheritance class refers to a direct base class or indirect base class above this class that has the message mapping mechanism or function, and this class also has the message mapping mechanism or function. The message mapping inheritance class generally refers to all inheritance classes of the message mapping most base class; the method for determining that a class is the message mapping most base class is that the direct base class of this class is a class in QT, and the direct base class of this class in the MFC original program is a class in MFC.
16. A method for reusing MFC message mapping code based on QT cross-platform according to claim 15, It is characterized in that When the method is applied to a message mapping inheritance class, the header file and cpp file of an existing MFC program are transplanted, and the code related to the message mapping remains the same as the MFC program; when the method is applied to the most basic class of a message mapping, the header file of an existing MFC program is transplanted, and a WM_DECL_WNDMSG macro application is added to the class definition; the cpp file of an existing MFC program is transplanted, and a WM_IMPL_WNDMSG macro application is added, and the second parameter of BEGIN_MESSAGE_MAP is modified to be the same as the first parameter.
17. An electronic device comprising a memory and a processor, wherein a computer program is stored in the memory. It is characterized in that When the processor executes the program, the method according to any one of claims 1 to 16 is implemented.
18. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 16 is implemented.
Citation Information
Patent Citations
Communication method between heterogeneous applications based on NIO asynchronous thread model
CN115914330A
