常用MFCマクロ

12218 ワード

最近私はMFCで1つのインテリジェントホーム監視プラットフォームのソフトウェア(MSCOMMシリアル通信コントロールを使用した)を開発して、私が1つのダイアログクラスAの中で別のダイアログクラスBのオブジェクトを定義してBのpublicメンバーにアクセスすることを通じて(通って)、ヒントはアクセスできません.
ActiveXコントロール
時には
DECLARE_EVENTSINK_MAP()マクロ
このマクロの後に定義された各メンバーは、新しいアクセスタイプを指定しないと元のプロパティを失います.
DECLARE_でEVENTSINK_MAP()マクロ(または他のマクロ)の後にメンバーを定義するには、新しいアクセスタイプを指定する必要があります.
.
どうぞご覧ください
DECLARE_EVENTSINK_MAP()マクロの定義(afxwin.h):
#ifdef _AFXDLL
#define DECLARE_EVENTSINK_MAP() \
private: \
    static const AFX_EVENTSINKMAP_ENTRY _eventsinkEntries[]; \
    static UINT _eventsinkEntryCount; \
protected: \
    static const AFX_EVENTSINKMAP eventsinkMap; \
    static const AFX_EVENTSINKMAP* PASCAL GetThisEventSinkMap(); \
    virtual const AFX_EVENTSINKMAP* GetEventSinkMap() const; \

#else
#define DECLARE_EVENTSINK_MAP() \
private: \
    static const AFX_EVENTSINKMAP_ENTRY _eventsinkEntries[]; \
    static UINT _eventsinkEntryCount; \
protected: \
    static const AFX_EVENTSINKMAP eventsinkMap; \
    virtual const AFX_EVENTSINKMAP* GetEventSinkMap() const; \

#endif

以下、MFC常用マクロについて説明する.
 
OLEコントロールイベントマクロ
DECLARE_EVENTSINK_MAP()
  
An OLE container can provide an event sink map to specify the events your container will 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, use the BEGIN_EVENTSINK_MAP macro, macro entries for each of the events to be notified of, and the END_EVENTSINK_MAP macro to declare the end of the event sink list.
 
メッセージマッピングマクロ
Message-Map Declaration and Demarcation Macros
DECLARE_MESSAGE_MAP
Declares that a message map will be used in a class to map messages to functions (must be used in the class declaration).
BEGIN_MESSAGE_MAP
Begins the definition of a message map (must be used in the class implementation).
END_MESSAGE_MAP
Ends the definition of a message map (must be used in the class implementation).
 
Message-Mapping Macros
ON_COMMAND
Indicates which function will handle a specified command message.
ON_CONTROL
Indicates which function will handle a specified control-notification message.
ON_MESSAGE
Indicates which function will handle a user-defined message.
ON_OLECMD
Indicates which function will handle a menu command from a DocObject or its container.
ON_REGISTERED_MESSAGE
Indicates which function will handle a registered user-defined message.
ON_REGISTERED_THREAD_MESSAGE
Indicates which function will handle a registered user-defined message when you have a 
CWinThread class.
ON_THREAD_MESSAGE
Indicates which function will handle a user-defined message when you have a 
CWinThread class.
ON_UPDATE_COMMAND_UI
Indicates which function will handle a specified user-interface update command message.
 
Message-Map Range Macros
ON_COMMAND_RANGE
Indicates which function will handle the range of command IDs specified in the first two parameters to the macro.
ON_UPDATE_COMMAND_UI_RANGE
Indicates which update handler will handle the range of command IDs specified in the first two parameters to the macro.
ON_CONTROL_RANGE
Indicates which function will handle notifications from the range of control IDs specified in the second and third parameters to the macro. The first parameter is a control-notification message, such as 
BN_CLICKED.
MFCデバッグマクロ
TRACE()-トレースデバッグマクロ
TRACE(,)のパラメータは、関数printf()のパラメータと同じ形式の出力フォーマットと式で構成されます.TRACEマクロの機能は、デバッグ実行時に式の値をOutputデバッグウィンドウに出力することです.TRACEマクロは、MFCアプリケーションDebug版のデバッグ実行状態でのみ機能し、Developer StudioのEnable tracing設定を保証する必要があります.
注:VS 2010ではTRACEで_を付けないT、さもないとヒントになりますCrtDbgReport: String too long or IO Error.
サンプルコード:
char* szName="LiMing";  
int nAge=18; 
//vs2010  , TRACE    _T ,     _CrtDbgReport: String too long or IO Error
TRACE("Name=%s,Age=%d
",szName,nAge);

ASSERT()——断言マクロ
ASSERT():式が真の場合、プログラムは実行を続行します.そうでなければ、プログラムの実行を一時停止し、実行を一時停止した行とファイルの情報をユーザーに伝えるダイアログボックスがポップアップされます.ユーザーは、実行の終了、デバッグ、または実行の継続を選択できます.
ASSERT_VALID()——有効マクロと断言
  ASSERT_VALID()は、ポインタとオブジェクトの有効性をチェックするために使用されます.一般ポインタの場合、ポインタが空であるかどうかのみがチェックされます.MFCクラスオブジェクトポインタについては,CObjectクラスのメンバ関数AssertValid()を呼び出すことでオブジェクトの捨て方性を判断する.ASSERT_VALIDマクロプロンプトポインタまたはオブジェクトが無効な方法でASSERTマクロと同じように、メッセージダイアログボックスが表示されます.ASSERT_VALIDマクロもDebugバージョンでしか機能しません.
VERIFY()——マクロの検証
  In the Debug version of MFC, evaluates its argument. If the result is 0, the macro prints a diagnostic message and halts the program. If the condition is nonzero, it does nothing. The diagnostic message has the form "assertion failed in file in line ".
  In the Release version of MFC, VERIFY evaluates the expression but does not print or interrupt the program. For example, if the expression is a function call, the call will be made.
サンプルコード:
// VERIFY can be used for things that should never fail, though 
// you may 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() may 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 context
HDC hdc;
VERIFY((hdc = ::GetDC(hwnd)) != NULL);

// give the display context back
::ReleaseDC(hwnd, hdc);

 
ランタイム・タイプ識別に関するマクロ
非マルチステート言語では、各オブジェクトのタイプがコンパイル時に決定されるため、実行時のタイプ情報は必要ありません(たとえば、プログラムを書くときにオブジェクトのタイプを指定します).ただし、C++などのマルチステートをサポートする言語では、コンパイル時にオブジェクトのタイプ情報を知らず、プログラムの実行時にのみオブジェクトの正確な情報を取得できる場合があります.C++はクラスの階層,虚関数,およびベースクラスポインタによって多状態を実現することが知られている.ベースクラスポインタは、ベースクラスのオブジェクトまたはその派生クラスのオブジェクトを指すために使用することができ、すなわち、ベースクラスポインタがオブジェクトを指す実際のタイプを常に事前に知ることはできない.したがって、プログラムで「ランタイムタイプ識別」を使用してオブジェクトの実際のタイプを識別する必要があります.  
ランタイムタイプ識別(runtime type information,RTTI)とは、プログラムの実行時にオブジェクトを特定できるタイプをいう.MFCは、一般的なC++におけるランタイムタイプ識別機能を拡張し、クラスがMFCのランタイムタイプ識別機能をサポートする場合、プログラムがオブジェクトの情報(クラス名、占有記憶空間サイズ、バージョン番号など)とベースクラス情報(runtime class informtation,RTCI)を取得することを可能にする.
DECLARE_DYNAMIC():動的サポートマクロ
  Adds the ability to access run-time information about an object's class when deriving a class from CObject.
  If you use the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros as described, you can then use the RUNTIME_CLASS macro and the CObject::IsKindOf function to determine the class of your objects at run time.
RUNTIME_CLASS(class_name)-ランタイムベースマクロ
  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 will return pointers to a CRuntimeClass structure.
サンプルコード:
CRuntimeClass* prt = RUNTIME_CLASS(CAge);
ASSERT(strcmp(prt->m_lpszClassName, "CAge") == 0);  

 
Color Macros
RGB()
構文:COLORREF RGB(BYTE byRed,BYTE byGreen,BYTE byBlue)
  The intensity for each argument is in the range 0 through 255. If all three intensities are zero, the result is black. If all three intensities are 255, the result is white.
  To extract the individual values for the red, green, and blue components of a COLORREF color value, use theGetRValue, GetGValue, and GetBValue macros, respectively.
 
文字コードマクロ
_T、_TEXT、TEXT、_L、L
  _T("")は、tcharに定義するマクロである.hの下で、あなたのプログラムにUnicode符号化をサポートさせる役割を果たします.
#define  __T(x)      L ## x

#define  _T(x)       __T(x)
#define  _TEXT(x)    __T(x)

プログラムをANSI方式でコンパイルすると、Tは実際には何の役にも立たない.一方、プログラムをUNICode方式でコンパイルすると、コンパイラは「Hello」文字列をUNICode方式で保存します.
#ifdef   UNICODE  
    #define __T(x)      L ## x  
#else  
    #define __T(x)      x  
#endif 

  
_Tと_Lの違いは、_Lどんな方法でコンパイルしてもUNICODで保存します.
文字列の前にLの役割を追加します.たとえば、Lの「マイ文字列」は、ANSI文字列をunicodeに変換する文字列を表し、各文字に2バイトを占有します.
UNICODを定義したらTマクロは文字列の前にLを付けます.このとき_T(「ABCD」)はL「ABCD」に相当し、これは広い文字列である.定義がなければ_Tマクロは文字列の前にそのLを付けません.T(「ABCD」)は「ABCD」に等しい.
 
CHAR、WCHAR、TCHAR
typedef   unsigned   char   CHAR;  
typedef   unsigned   wchar_t   WCHAR;

CHARは実際にはunsigned charであり、WHARはwchar_である.一方、TCHARは、UNICODをサポートするか否かによって異なる.
#ifdef   UNICODE  
    typedef   wchar_t   TCHAR;   
#else  
    typedef   unsigned   char   TCHAR;  
#endif 

UNICodeマクロを定義すると、コンパイラがUNICodeバージョンを採用する準備をしていることを示すことになります.このときTCHARはunsigned charからwchar_になりますt.
転載先:https://www.cnblogs.com/gaohongchen01/p/4084657.html