可以先从CStatic派生,然后改动一下,以下是一个例子:
.h文件
(Msg_SRText.h)
#if !defined(AFX_MSG_SRTEXT_H__EF85DC22_7896_4741_B543_5D92350DD54F__INCLUDED_)
#define AFX_MSG_SRTEXT_H__EF85DC22_7896_4741_B543_5D92350DD54F__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Msg_SRText.h : header file
//
#include "UserDefineGram.h"
/////////////////////////////////////////////////////////////////////////////
// CMsg_SRText windowclass CMsg_SRText : public CObject
{
protected:    DECLARE_SERIAL(CMsg_SRText)
protected:
public:
    MSG_SRTEXT Msg_SRText;//MSG_SRTEXT为自定义的数据类型
public:

    CMsg_SRText();
    CMsg_SRText(CString Index,UINT message,CString SRText,int STextType=0,int MatchType=0,WPARAM wParam=NULL,LPARAM lParam=NULL); 
   ~CMsg_SRText();
    virtual void Serialize(CArchive& ar);
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MSG_SRTEXT_H__EF85DC22_7896_4741_B543_5D92350DD54F__INCLUDED_)
.cpp文件
// Msg_SRText.cpp : implementation file
//#include "stdafx.h"
#include "Msg_SRText.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMsg_SRText
IMPLEMENT_SERIAL(CMsg_SRText,CObject,1)
CMsg_SRText::CMsg_SRText()
{
Msg_SRText.Index="0";
Msg_SRText.message=WM_USER+0;
Msg_SRText.strSRText="";
Msg_SRText.STextType=0;
Msg_SRText.MatchType=0;
Msg_SRText.wParam=NULL;
Msg_SRText.lParam=NULL; 
}CMsg_SRText::~CMsg_SRText()
{
}
CMsg_SRText::CMsg_SRText(CString Index,UINT message,CString SRText,int STextType,int MatchType,WPARAM wParam,LPARAM lParam)
{
    Msg_SRText.Index=Index; 
    Msg_SRText.message=message;
    Msg_SRText.strSRText=SRText;
    Msg_SRText.STextType=STextType;
    Msg_SRText.MatchType=MatchType; 
    Msg_SRText.wParam=wParam;
    Msg_SRText.lParam=lParam; 
}void CMsg_SRText::Serialize(CArchive& ar)
{
if(ar.IsStoring())
{
ar<<(UINT)Msg_SRText.message;
ar<<(WPARAM)Msg_SRText.wParam;
ar<<(LPARAM)Msg_SRText.lParam;
ar<<(CString)Msg_SRText.strSRText; 
ar<<(int)Msg_SRText.STextType;
ar<<(int)Msg_SRText.MatchType; 
}
else
{
UINT message;
ar>>message;
                  Msg_SRText.message=message;
WPARAM wParam;
ar>>wParam;
Msg_SRText.wParam=wParam;
LPARAM lParam;
ar>>lParam;
Msg_SRText.lParam=lParam;
CString srText;
ar>>srText;
Msg_SRText.strSRText=srText; 
int STextType;
ar>>STextType;
Msg_SRText.STextType=STextType;
int MatchType;
ar>>MatchType;
Msg_SRText.MatchType=MatchType; 
}
}

解决方案 »

  1.   

    没看出来为什么要从CStatic改写。 这个和CStatic有什么关系吗?还有怎么改写?直接copy and paste吗?
      

  2.   

    完全可以自己添加新的.cpp和.h开始写,我说基于CStatic只是一种选择而已,主要是让MFC的ClasswIzard给自己生成一个类的框架,省力而已。
    我上面写的类就是用MFC的ClasswIzard生成的(基于CStatic),然后做了小
    的修改,用于序列化(Serialize)
      

  3.   

    然后你就直接把class CMsg_SRText : public CStatic 改为
    class CMsg_SRText : public CObject, 然后把那些函数全部改成自己的?
    还有没有更直接简单的方法?
    为什么不能直接从CObject派生呢?