手写MFC的深入浅出

时间:2022-12-14 19:26:53

前言MFC#definetypedefC++弄得面目全非,下面的程序是一个单文档的程序,其功能是创建一个单文档的窗体,当鼠标左键点击窗体时,则在该文档窗体的点击处出现字符串“Welcome to SDI.

#include<afxwin.h>

#include<afxext.h>

#include"resource.h"

 

class mydoc:public CDocument

{

    DECLARE_DYNCREATE(mydoc)  //不知所云,C++没有这个东东,第一讲中介绍

public :

        CString s;

        CPoint point;

        mydoc()

        {

                 s="Welcome to SDI";

                 point.x=250;

                 point.y=100;

        }

};

 

IMPLEMENT_DYNCREATE (mydoc,CDocument) //不知所云,C++没有这个东东,第二讲中介绍

 

class myview:public CView

{

                 DECLARE_DYNCREATE(myview)

public:

        void OnDraw(CDC *d)

        {

                  mydoc *b;

                 b=(mydoc *)m_pDocument;

                 d->SetTextColor(RGB(0,0,0)) ;

                 d->TextOut(b->point.x,b->point.y ,b->s) ;

                  

        }

 

        void OnLButtonDown(UINT nFlags,CPoint p)

        {

                  mydoc *b;

                 b=(mydoc*)m_pDocument;

                 b->point.x=p.x;

                 b->point.y=p.y;

                 Invalidate();

        }

 

        DECLARE_MESSAGE_MAP()

 

};

        IMPLEMENT_DYNCREATE(myview,CView)

 

        BEGIN_MESSAGE_MAP(myview,CView) //不知所云,C++没有这个东东,第三讲中介绍

        ON_WM_LBUTTONDOWN()//不知所云,C++没有这个东东,第三讲中介绍

        END_MESSAGE_MAP()//不知所云,C++没有这个东东,第三讲中介绍

 

class zzz:public CWinApp

{

public:

        int InitInstance()

        {

                 CSingleDocTemplate *y;

                 CRuntimeClass *w,*d,*v;

                 d=RUNTIME_CLASS (mydoc);

                 w=RUNTIME_CLASS(CFrameWnd);

                 v=RUNTIME_CLASS(myview);

                  y=new CSingleDocTemplate(IDR_MENU1,d,w,v);

                 AddDocTemplate(y);

                 OnFileNew();

                  return 1;

        }

};

 

        zzz a;