VS2003模板的下載與使用和注意事項
本文講述VS2003模板的具體細節(jié),怎樣創(chuàng)建VS2003模板。這些內(nèi)容都是我這些天花了一周的時間查閱資料并像IT精英學習出來的,中間可能有不少的錯誤,歡迎大家指正。下面就說明下。
VS2003模板升級的基本過程如下:
1、首先備份現(xiàn)有的VC程序,備份之后啟動VS2005。
2、通過VS2005打開要升級的解決方案,系統(tǒng)提示自動轉換,如果VC程序已經(jīng)被嵌入到VSS中,則系統(tǒng)將提示登錄VSS,并且自動將解決方案文件和項目文件簽出。(注意選擇備份原來版本 的解決方案的選項。)
3、一般情況下系統(tǒng)提示成功,可能會有兩個警告,可以不予理會。因為升級過程中僅僅修改了解決方案文件和工程項目文件,所以速度會很快,C++頭文件和CPP文件都不作任何修改(這和VB6到VB2005的升級不同),因此速度很快。
4、在VS2005中重新編譯升級后的程序,很可能出現(xiàn)很多警告和錯誤提示,警告可以不予理會,錯誤提示必須修改。我所遇到的錯誤提示主要有兩種:一種提示是“某個變量沒有定義”,另外一種是模板類的消息映射的錯誤提示。
先說第一種錯誤提示,例如如下的兩個for循環(huán)語句
- for(int i=0;i<10;i++)
- {
- }
- for(i=0;i<100;i++)//此處將提示i沒有定義
上面的語句在VS2003中沒有問題,在2005中則是錯誤的,2005將i作為第一個for循環(huán)中的局部變量處理,因此編譯器認為第二個for循環(huán)中的i沒有定義。這類錯誤可能有很多,但是修改起來比較容易。
VS2003模板第二種錯誤是模板類的消息映射宏錯誤。我在程序中設計了一個控件模板:
- template <class B,class P>class CUniDataCtrl : public B,public CUniDataBaseCtrl #t#
- {
則直接實現(xiàn)了原來幾十行代碼實現(xiàn)的功能。
上述的模板類消息映射宏我是參考BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass)編寫的,該宏只支持一個模板參數(shù),而我定義的模板中需要兩個模板參數(shù),因此,我自己擴充了一下VS2003模板的內(nèi)容。關于BEGIN_TEMPLATE_MESSAGE_MAP的幫助在MSDN中好像沒有,在afxwin.h中定義了:
- #define BEGIN_TEMPLATE_MESSAGE_MAP_EX(theClass, type_name1,type_name2, baseClass)
- PTM_WARNING_DISABLE
- template < typename type_name1,typename type_name2 >
- const AFX_MSGMP* theClass< type_name1 ,type_name2 >::GetMessageMap() const
- { return GetThisMessageMap(); }
- template < typename type_name1 ,typename type_name2>
- const AFX_MSGMAP* PASCAL theClass< type_name1 ,type_name2 >::GetThisMessageMap()
- {
- typedef theClass< type_name1 ,type_name2 > ThisClass;
- typedef baseClass TheBaseClass;
- static const AFX_MSGMAP_ENTRY _messageEntries[] =
- {
- BEGIN_TEMPLATE_MESSAGE_MAP_EX(CUniDataCtrl,B,P ,B)
- ON_WM_LBUTTONDOWN()
- ON_WM_RBUTTO
- #define DECLARE_MESSAGE_MAP()
- protected:
- static const AFX_MSGMAP* PASCAL GetThisMessageMap();
- virtual const AFX_MSGMAP* GetMessageMap() const;
- #define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass)
- PTM_WARNING_DISABLE
- template < typename type_name >
- const AFX_MSGMAP* theClass< type_name >::GetMessageMap() const
- { return GetThisMessageMap(); }
- template < typename type_name >
- const AFX_MSGMAP* PASCAL theClass< type_name >::GetThisMessageMap()
- {
- typedef theClass< type_name > ThisClass;
- typedef baseClass TheBaseClass;
- static const AFX_MSGMAP_ENTRY _messageEntries[] =
- {
- #define BEGIN_MESSAGE_MAP(theClass, baseClass)
- PTM_WARNING_DISABLE
- const AFX_MSGMAP* theClass::GetMessageMap() const
- { return GetThisMessageMap(); }
- const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap()
- {
- typedef theClass ThisClass;
- typedef baseClass TheBaseClass;
- static const AFX_MSGMAP_ENTRY _messageEntries[] =
- {
- #define END_MESSAGE_MAP()
- {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
- };
- static const AFX_MSGMAP messageMap =
- { &TheBaseClass::GetThisMessageMap, &_messageEntries[0] };
- return &messageMap;
- }
- PTM_WARNING_RESTORE