Common MFC macros

Source: Internet
Author: User
Tags ole

Recently I was using MFC to develop a smart home monitoring platform software (using the MSComm serial communication control), when I was in a dialog class A to define another dialog class B object access to the public members of B, the prompt is not accessible. Later, after many days to friends for help, and finally in a great God helped to find a solution. Originally in use ActiveX ControlsWill produce a Declare_eventsink_map () macro, each member defined after this macro loses its original property if it does not specify a new access type. If you define any member after the DECLARE_EVENTSINK_MAP () macro (or other macro), you must specify a new access type for it。 Here's a brief introduction to MFC common macros: OLE control event macros Declare_eventsink_map ()    An OLE container can provide a event sink map to specify the events your container would be notified of. Use the DECLARE_EVENTSINK_MAP macro at the end of your class declaration. Then, in the. CPP file that defines the member functions for the class with the BEGIN_EVENTSINK_MAP macro, macro entries for EA Ch of the events to is notified of, and the END_EVENTSINK_MAP macro to declare the END of the event sink list. message map Macros Message-map Declaration and demarcation Macros Declare_message_mapDeclares that a message map would be used in a class to map messages to functions (must is used in the class declaration). Begin_message_mapBegins the definition of a message map (must is used in the class implementation). End_message_mapEnds the definition of a message map (must is used in the class implementation). message-mapping Macros On_commandIndicates which function would handle a specified command message. On_controlIndicates which function would handle a specified control-notification message. On_messageIndicates which function would handle a user-defined message. On_olecmdIndicates which function would handle a menu command from a DocObject or its container. On_registered_messageIndicates which function would handle a registered user-defined message. On_registered_thread_messageIndicates which function would handle a registered user-defined message when you have a CWinThreadClass. On_thread_messageIndicates which function would handle a user-defined message when you have a CWinThreadClass. ON_UPDATE_COMMAND_UIIndicates which function would handle a specified user-interface update command message. message-map Range Macros On_command_rangeIndicates which function would handle the range of command IDs specified in the first and parameters to the macro. On_update_command_ui_rangeIndicates which update handler would handle the range of command IDs specified in the first and parameters to the macro. On_control_rangeIndicates which function would handle notifications from the range of control IDs specified in the second and third Paramet ERS to the macro. The first parameter is a control-notification message, such as bn_clicked.

MFC Debug Macros

Trace ()--Trace Debug macro

The parameters in TRACE (< output format >,< expression >) are made up of output formats and expressions in the same form as the parameters of the function printf (). The function of the trace macro is to export the value of an expression to the Output Debug window at debug run time . Trace macros work only in the debug run state of the MFC application Debug version, and you must ensure enable tracing settings in Developer Studio.

Note:VS2010, do not add _t with trace, otherwise it will prompt _crtdbgreport:string too long or IO Error.

Example code:

Char* szname="liming";   int nage=//vs2010, do not add _t with trace, otherwise you will be prompted _crtdbgreport:string too long or IO ErrorTRACE ("name=%s,age=%d\n"

ASSERT ()--Assert macro

ASSERT (< expression >): If the expression is true, the program resumes execution, otherwise pauses the program's Run and pops up a dialog box that tells the user that the program is paused and information about the file in which it is running. The user can choose to terminate the run, debug the program, or continue running.

Assert_valid ()--Assert a valid macro

Assert_valid (< pointer >) is used to check the validity of pointers and objects . For a generic pointer, check only if the pointer is empty. For MFC class object pointers, call the CObject class's member function AssertValid () to determine the object's rounding. The ASSERT_VALID macro prompts the pointer or object to be invalid in the same way that an Assert macro pops up an informational dialog box. The ASSERT_VALID macro also works only in the debug version .

VERIFY ()--Check macro

In the Debug version of MFC, evaluates its argument. If The result is 0, the macro prints a diagnostic message and halts. If the condition is nonzero, it does nothing. The diagnostic message has the form "Assertion failed in file <name>in line <num>" .

In the Release version of MFC, VERIFY evaluates the expression but does not print or interrupt the Progr Am. For example, if the expression was a function call, the call would be made.

Example code:

//VERIFY can used for things that should never fail, though//want to make sure you can provide better error recovery//if the error can actually cause a crash in a production system.//It _is_ possible that GetDC () could fail, but the out-of-memory//condition that causes it isn ' t likely. For a test application,//This use of VERIFY () is fine. For any production code, this//usage is dubious.//get the display device contextHDC hdc; VERIFY (HDC=:: GetDC (hwnd))! =NULL);//give the display context back:: ReleaseDC (hwnd, HDC);

Macros for run-time type recognition

  non-polymorphic languages do not require runtime type information, because each object's type is determined at compile time. (Example: We specify the type of the object when writing the program). But in languages that support polymorphism, such as C + +, there may be situations where you do not know the type information of an object at compile time, and only when the program is running can you get accurate information about the object. We already know that C + + is polymorphic through the hierarchy of classes, virtual functions, and base-class pointers. A base-class pointer can be used to point to an object of a base class or to an object of its derived class, that is, we are not always able to know in advance at any time the actual type of the object to which the base class pointer points . Therefore, you must use run-time type recognition in your program to identify the actual type of the object.

  Runtime type recognition (runtime Information,rtti) refers to the type of an object that can be determined when the program is run. MFC expands the functionality of run-time type recognition in general C + +, which allows the program to obtain information about the object (such as class name, amount of storage space and version number) and base class information when a class supports MFC's runtime type recognition feature (runtime class Informtation,rtci )。

Declare_dynamic ()--Dynamic support macros

  Adds the ability to access Run-time information on an object's class when deriving a class from cobject.

If you use the DECLARE_DYNAMIC and implement_dynamic macros as described. RUNTIME_CLASS Macro and the cobject::iskindof function to determine the CLASS of your objects at run Tim E.

Runtime_class (class_name)--run-time Base macro

Gets the Run-time class structure from the name of a C + + class.

runtime_class Returns a pointer to a CRUNTIMECLASS structure for the CLASS specified by class_name. Only CObject-derived classes declared with declare_dynamic,declare_dyncreate, or Declare_serial would return pointers to a CRuntimeClass structure.

Example code:

cruntimeclass* prt = runtime_class (CAge); ASSERT (strcmp (PRT"CAge"0);  

Color Macros

RGB ()

Syntax form:colorref RGB (byte byred, byte Bygreen, byte byblue)

The intensity for each argument are in the range 0 through 255. If all three intensities was zero, the result is black. If all three intensities was 255, the result is white.

To extract the individual values for the red, green, and blue components of a colorref color value, use theGe Trvalue, getgvalue, and getbvalue macros, respectively.

Common MFC macros

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.