Visual C Windows Programming CSplitter Wnd CScroll View

  • Slides: 36
Download presentation
Visual C++ Windows Programming 第六章 分裂視窗及多文件視窗

Visual C++ Windows Programming 第六章 分裂視窗及多文件視窗

CSplitter. Wnd CScroll. View

CSplitter. Wnd CScroll. View

#include <afxwin. h> <afxtempl. h> <afxext. h> "resource. h" class CGObject : public CObject

#include <afxwin. h> <afxtempl. h> <afxext. h> "resource. h" class CGObject : public CObject { public: int shapenum; BOOL fill; COLORREF Fill. Color, Line. Color; int width; CPoint Start. Pnt, End. Pnt; CGObject() { } CGObject(int shapenum, BOOL fill, COLORREF Fill. Color, COLORREF Line. Color, int width, CPoint Start. Pnt, CPoint End. Pnt) : shapenum(shapenum), fill(fill), Fill. Color(Fill. Color), Line. Color(Line. Color), width(width), Start. Pnt(Start. Pnt), End. Pnt(End. Pnt) { } CGObject(CGObject &g) : shapenum(g. shapenum), fill(g. fill), Fill. Color(g. Fill. Color), Line. Color(g. Line. Color), width(g. width), Start. Pnt(g. Start. Pnt), End. Pnt(g. End. Pnt) { } CGObject & operator = (CGObject &g) { shapenum = g. shapenum; fill = g. fill; Fill. Color = g. Fill. Color; Line. Color = g. Line. Color; width = g. width; Start. Pnt = g. Start. Pnt; End. Pnt = g. End. Pnt; return *this; } } ;

class Shape { protected: CPoint Start. Pnt, End. Pnt; int shapenum; friend class CMy.

class Shape { protected: CPoint Start. Pnt, End. Pnt; int shapenum; friend class CMy. View; public: Shape(CPoint Start. Pnt, CPoint End. Pnt, int shapenum) : Start. Pnt(Start. Pnt), End. Pnt(End. Pnt), shapenum(shapenum) { } Shape(Shape &s) : Start. Pnt(s. Start. Pnt), End. Pnt(s. End. Pnt), shapenum(s. shapenum) { } Shape() { } Shape & operator = (Shape &s) { Start. Pnt = s. Start. Pnt; End. Pnt = s. End. Pnt; return *this; } virtual void draw(CDC &a. DC, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) = 0; int Get. Shape. Num() { return shapenum; } void Set. Point(CPoint SPnt, CPoint EPnt) { Start. Pnt = SPnt; End. Pnt = EPnt; } } ; class Line : public Shape { public: friend class CMy. View; Line() { shapenum = 0; } Line(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 0) { } Line(Line &l) : Shape(l. Start. Pnt, l. End. Pnt, 0) { } Line & operator = (Line &l) { Start. Pnt = l. Start. Pnt; End. Pnt = l. End. Pnt; return *this; }

void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) {

void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Move. To(Start. Pnt); dc. Line. To(End. Pnt); dc. Select. Object(old. Pen); } } ; class ellipse : public Shape { public: friend class CMy. View; ellipse() { shapenum = 1; } ellipse(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 1) { } ellipse(ellipse &e) : Shape(e. Start. Pnt, e. End. Pnt, 1) { } ellipse & operator = (ellipse &e) { Start. Pnt = e. Start. Pnt; End. Pnt = e. End. Pnt; return *this; } void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CRect rect(Start. Pnt, End. Pnt); CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Select. Stock. Object(NULL_BRUSH ); dc. Ellipse(rect); dc. Select. Object(old. Pen); } } ;

class rectangle : public Shape { public: friend class CMy. View; rectangle() { shapenum

class rectangle : public Shape { public: friend class CMy. View; rectangle() { shapenum = 2; } rectangle(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 2) { } rectangle(rectangle &r) : Shape(r. Start. Pnt, r. End. Pnt, 2) { } rectangle & operator = (rectangle &r) { Start. Pnt = r. Start. Pnt; End. Pnt = r. End. Pnt; return *this; } void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CRect rect(Start. Pnt, End. Pnt); CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Select. Stock. Object(NULL_BRUSH ); dc. Rectangle(rect); dc. Select. Object(old. Pen); } } ; class CMy. Document : public CDocument { private: CArray<CGObject, CGObject> g. Array; public: void Add. Object(CGObject &g) { g. Array. Add(g); } CGObject & Get. Object(int i) { return g. Array[i]; } int Get. Size() { return g. Array. Get. Size(); } DECLARE_DYNCREATE(CMy. Document ) DECLARE_MESSAGE_MAP() } ;

IMPLEMENT_DYNCREATE(CMy. Document , CDocument) BEGIN_MESSAGE_MAP(CMy. Document , CDocument) END_MESSAGE_MAP() class CMy. Frame : public

IMPLEMENT_DYNCREATE(CMy. Document , CDocument) BEGIN_MESSAGE_MAP(CMy. Document , CDocument) END_MESSAGE_MAP() class CMy. Frame : public CFrame. Wnd { protected: CSplitter. Wnd Dyn. Split; CMenu *menu; public: CTool. Bar RGBBar, Shape. Bar; CStatus. Bar statusbar; CMy. Frame() { } ~CMy. Frame() { } BOOL On. Create. Client(LPCREATESTRUCT lpcs, CCreate. Context *p. Context) { return Dyn. Split. Create(this, 2, 2, CSize(1, 1), p. Context); } afx_msg int On. Create(LPCREATESTRUCT lp. Create. Struct) { if(CFrame. Wnd: : On. Create(lp. Create. Struct )) return -1; RGBBar. Create(this); RGBBar. Load. Tool. Bar(IDR_TBRGB ); RGBBar. Enable. Docking(CBRS_ALIGN_ANY ); RGBBar. Set. Bar. Style(RGBBar. Get. Bar. Style () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); Shape. Bar. Create(this); Shape. Bar. Load. Tool. Bar(IDR_TBSHAPE ); Shape. Bar. Enable. Docking(CBRS_ALIGN_ANY ); Shape. Bar. Set. Bar. Style(RGBBar. Get. Bar. Style () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); Enable. Docking(CBRS_ALIGN_ANY );

Dock. Control. Bar(&RGBBar); Dock. Control. Bar(&Shape. Bar ); static UINT indicators[] = { ID_SEPARATOR,

Dock. Control. Bar(&RGBBar); Dock. Control. Bar(&Shape. Bar ); static UINT indicators[] = { ID_SEPARATOR, IDS_RED, IDS_LINE } ; statusbar. Create(this); statusbar. Set. Indicators(indicators , sizeof(indicators) / sizeof(UINT)); return 0; } DECLARE_DYNCREATE(CMy. Frame ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. Frame , CFrame. Wnd) BEGIN_MESSAGE_MAP(CMy. Frame , CFrame. Wnd) ON_WM_CREATE() END_MESSAGE_MAP() class CMy. View : public CScroll. View { private: COLORREF lcolor, fcolor; Shape *a. Shape; Shape *rd. Shape; int width; HCURSOR hcursor; public: CMy. View() { lcolor = RGB(255, 0, 0); a. Shape = new Line; fcolor = RGB(0, 0, 0); width = 2; } ~CMy. View() { }

void Logical. Coor(CPoint *point) { CPoint Origin = Get. Scroll. Position(); point->x = Origin.

void Logical. Coor(CPoint *point) { CPoint Origin = Get. Scroll. Position(); point->x = Origin. x + point->x; point->y = Origin. y + point->y; } void Physical. Coor(CPoint *point) { CPoint Origin = Get. Scroll. Position(); point->x = point->x - Origin. x; point->y = point->y - Origin. y; } afx_msg int On. Create(LPCREATESTRUCT lp. Create. Struct) { if(CScroll. View: : On. Create(lp. Create. Struct ) == -1) return -1; CSize DCSize(800, 800); Set. Scroll. Sizes(MM_TEXT, DCSize); return 0; } void On. Update(CView *p. Sender, LPARAM l. Hint, CObject *p. Hint) { if(p. Hint != NULL) { CRect rect((CRect *)p. Hint); Physical. Coor(&rect. Top. Left ()); Physical. Coor(&rect. Bottom. Right ()); rect. Normalize. Rect(); rect. Inflate. Rect(rect. Width()/2+1, rect. Height()/2+1); Invalidate. Rect(&rect); } else CScroll. View: : On. Update(p. Sender , l. Hint, p. Hint); } afx_msg void On. Ellipse() { CString resstr;

a. Shape = new ellipse; resstr. Load. String(IDS_ELLIPSE ); ((CMy. Frame *) Get. Parent.

a. Shape = new ellipse; resstr. Load. String(IDS_ELLIPSE ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); } afx_msg void On. Rect() { CString resstr; a. Shape = new rectangle; resstr. Load. String(IDS_RECTANGLE ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); } afx_msg void On. Line() { CString resstr; a. Shape = new Line; resstr. Load. String(IDS_LINE ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); } afx_msg void On. Draw(CDC *a. DC) { CMy. Document *doc = (CMy. Document *) Get. Document(); int num = doc->Get. Size(); CView: : On. Draw(a. DC); int i; for(i = 0; i < num; i++) { CGObject *object = &(doc->Get. Object(i)); switch(object->shapenum) { case 0: rd. Shape = new Line; break;

case 1: rd. Shape = new ellipse; break; case 2: rd. Shape = new

case 1: rd. Shape = new ellipse; break; case 2: rd. Shape = new rectangle; break; } rd. Shape->Set. Point(object->Start. Pnt, object->End. Pnt); rd. Shape->draw((*a. DC), object->Line. Color, object->Fill. Color, object->width); delete rd. Shape; } } afx_msg void On. LButton. Down(UINT, CPoint point) { Set. Capture(); if(this == Get. Capture()) { Logical. Coor(&point); (*a. Shape). Start. Pnt = (*a. Shape). End. Pnt = point; switch((*a. Shape). shapenum) { case 0: hcursor = Afx. Get. App()->Load. Cursor(IDC_LINE); : : Set. Cursor(hcursor); break; case 1: hcursor = Afx. Get. App()->Load. Cursor(IDC_ELLIPSE); : : Set. Cursor(hcursor); break; case 2: hcursor = Afx. Get. App()->Load. Cursor(IDC_RECT); : : Set. Cursor(hcursor); break; } } }

afx_msg void On. Mouse. Move(UINT, CPoint point) { if(this == Get. Capture()) { CClient.

afx_msg void On. Mouse. Move(UINT, CPoint point) { if(this == Get. Capture()) { CClient. DC a. DC(this); a. DC. Set. ROP 2(R 2_NOT); On. Prepare. DC(&a. DC); Logical. Coor(&point); (*a. Shape). draw(a. DC, lcolor, fcolor, width); (*a. Shape). End. Pnt = point; (*a. Shape). draw(a. DC, lcolor, fcolor, width); } } afx_msg void On. LButton. Up(UINT, CPoint point) { if(this == Get. Capture()) { Logical. Coor(&point); CClient. DC a. DC(this); On. Prepare. DC(&a. DC); (*a. Shape). End. Pnt = point; (*a. Shape). draw(a. DC, lcolor, fcolor, width); CGObject object(a. Shape->Get. Shape. Num(), true, fcolor, lcolor, width, a. Shape->Start. Pnt, a. Shape->End. Pnt); CMy. Document *doc = (CMy. Document *) Get. Document(); doc->Add. Object(object); Release. Capture(); } } afx_msg void On. Red() { CString resstr; lcolor = RGB(255, 0, 0); resstr. Load. String(IDS_RED ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); }

afx_msg void On. Green() { CString resstr; lcolor = RGB(0, 255, 0); resstr. Load.

afx_msg void On. Green() { CString resstr; lcolor = RGB(0, 255, 0); resstr. Load. String(IDS_GREEN ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); } afx_msg void On. Blue() { CString resstr; lcolor = RGB(0, 0, 255); resstr. Load. String(IDS_BLUE ); ((CMy. Frame *) Get. Parent. Frame())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); } afx_msg void On. Update. Ellipse(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 1); } afx_msg void On. Update. Rect(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 2); } afx_msg void On. Update. Line(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 0); } afx_msg void On. Update. Red(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(255, 0, 0)); } afx_msg void On. Update. Green(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(0, 255, 0)); } afx_msg void On. Update. Blue(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(0, 0, 255)); }

DECLARE_DYNCREATE(CMy. View ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. View , CScroll. View) BEGIN_MESSAGE_MAP(CMy. View ,

DECLARE_DYNCREATE(CMy. View ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. View , CScroll. View) BEGIN_MESSAGE_MAP(CMy. View , CScroll. View) ON_WM_CREATE() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_COMMAND(IDM_RED, On. Red) ON_COMMAND(IDM_GREEN, On. Green) ON_COMMAND(IDM_BLUE, On. Blue) ON_COMMAND(IDM_LINE, On. Line) ON_COMMAND(IDM_ELLIPSE, On. Ellipse) ON_COMMAND(IDM_RECTANGLE, On. Rect) ON_UPDATE_COMMAND_UI(IDM_RED, On. Update. Red) ON_UPDATE_COMMAND_UI(IDM_GREEN, On. Update. Green) ON_UPDATE_COMMAND_UI(IDM_BLUE, On. Update. Blue) ON_UPDATE_COMMAND_UI(IDM_LINE, On. Update. Line) ON_UPDATE_COMMAND_UI(IDM_ELLIPSE, On. Update. Ellipse) ON_UPDATE_COMMAND_UI(IDM_RECTANGLE, On. Update. Rect) END_MESSAGE_MAP() class CMy. App : public CWin. App { public: BOOL Init. Instance() { CDocument *a. DOC; CSingle. Doc. Template *a. Doc. Template; a. Doc. Template = new CSingle. Doc. Template(IDR_MENU , RUNTIME_CLASS(CMy. Document ), RUNTIME_CLASS(CMy. Frame), RUNTIME_CLASS(CMy. View));

Add. Doc. Template(a. Doc. Template ); a. DOC = a. Doc. Template->Create. New. Document();

Add. Doc. Template(a. Doc. Template ); a. DOC = a. Doc. Template->Create. New. Document(); CFrame. Wnd *Frame = a. Doc. Template->Create. New. Frame(a. DOC, NULL); m_p. Main. Wnd = Frame; a. Doc. Template->Initial. Update. Frame(Frame , a. DOC); Frame->Show. Window(SW_SHOW); return true; } } ; CMy. App a_app;

#include <afxwin. h> <afxtempl. h> <afxext. h> "resource. h" class CMy. MDIChild : public

#include <afxwin. h> <afxtempl. h> <afxext. h> "resource. h" class CMy. MDIChild : public CMDIChild. Wnd { DECLARE_DYNCREATE(CMy. MDIChild ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. MDIChild , CMDIChild. Wnd) BEGIN_MESSAGE_MAP(CMy. MDIChild , CMDIChild. Wnd) END_MESSAGE_MAP() class CGObject : public CObject { public: int shapenum; BOOL fill; COLORREF Fill. Color, Line. Color; int width; CPoint Start. Pnt, End. Pnt; CGObject() { } CGObject(int shapenum, BOOL fill, COLORREF Fill. Color, COLORREF Line. Color, int width, CPoint Start. Pnt, CPoint End. Pnt) : shapenum(shapenum), fill(fill), Fill. Color(Fill. Color), Line. Color(Line. Color), width(width), Start. Pnt(Start. Pnt), End. Pnt(End. Pnt) { } CGObject(CGObject &g) : shapenum(g. shapenum), fill(g. fill), Fill. Color(g. Fill. Color), Line. Color(g. Line. Color), width(g. width), Start. Pnt(g. Start. Pnt), End. Pnt(g. End. Pnt) { }

CGObject & operator = (CGObject &g) { shapenum = g. shapenum; fill = g.

CGObject & operator = (CGObject &g) { shapenum = g. shapenum; fill = g. fill; Fill. Color = g. Fill. Color; Line. Color = g. Line. Color; width = g. width; Start. Pnt = g. Start. Pnt; End. Pnt = g. End. Pnt; return *this; } } ; class Shape { protected: CPoint Start. Pnt, End. Pnt; int shapenum; friend class CMy. View; public: Shape(CPoint Start. Pnt, CPoint End. Pnt, int shapenum) : Start. Pnt(Start. Pnt), End. Pnt(End. Pnt), shapenum(shapenum) { } Shape(Shape &s) : Start. Pnt(s. Start. Pnt), End. Pnt(s. End. Pnt), shapenum(s. shapenum) { } Shape() { } Shape & operator = (Shape &s) { Start. Pnt = s. Start. Pnt; End. Pnt = s. End. Pnt; //shapenum = s. shapenum; return *this; }

virtual void draw(CDC &a. DC, COLORREF color, COLORREF fcolor, int width, BOOL Filled =

virtual void draw(CDC &a. DC, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) = 0; int Get. Shape. Num() { return shapenum; } void Set. Point(CPoint SPnt, CPoint EPnt) { Start. Pnt = SPnt; End. Pnt = EPnt; } } ; class Line : public Shape { public: friend class CMy. View; Line() { shapenum = 0; } Line(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 0) { } Line(Line &l) : Shape(l. Start. Pnt, l. End. Pnt, 0) { } Line & operator = (Line &l) { Start. Pnt = l. Start. Pnt; End. Pnt = l. End. Pnt; return *this; } void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Move. To(Start. Pnt); dc. Line. To(End. Pnt); dc. Select. Object(old. Pen); } } ; class ellipse : public Shape { public: friend class CMy. View; ellipse() { shapenum = 1; } ellipse(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 1) { } ellipse(ellipse &e) : Shape(e. Start. Pnt, e. End. Pnt, 1) { }

ellipse & operator = (ellipse &e) { Start. Pnt = e. Start. Pnt; End.

ellipse & operator = (ellipse &e) { Start. Pnt = e. Start. Pnt; End. Pnt = e. End. Pnt; return *this; } void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CRect rect(Start. Pnt, End. Pnt); CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Select. Stock. Object(NULL_BRUSH ); dc. Ellipse(rect); dc. Select. Object(old. Pen); } } ; class rectangle : public Shape { public: friend class CMy. View; rectangle() { shapenum = 2; } rectangle(CPoint Start. Pnt, CPoint End. Pnt) : Shape(Start. Pnt, End. Pnt, 2) { } rectangle(rectangle &r) : Shape(r. Start. Pnt, r. End. Pnt, 2) { } rectangle & operator = (rectangle &r) { Start. Pnt = r. Start. Pnt; End. Pnt = r. End. Pnt; return *this; }

void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) {

void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width, BOOL Filled = false) { CRect rect(Start. Pnt, End. Pnt); CPen pen(PS_SOLID, width, color); CPen *old. Pen = dc. Select. Object(&pen); dc. Select. Stock. Object(NULL_BRUSH ); dc. Rectangle(rect); dc. Select. Object(old. Pen); } } ; class CMy. Document : public CDocument { private: CArray<CGObject, CGObject> g. Array; public: void Add. Object(CGObject &g) { g. Array. Add(g); } CGObject & Get. Object(int i) { return g. Array[i]; } int Get. Size() { return g. Array. Get. Size(); } DECLARE_DYNCREATE(CMy. Document ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. Document , CDocument) BEGIN_MESSAGE_MAP(CMy. Document , CDocument) END_MESSAGE_MAP() class CMy. Frame : public CMDIFrame. Wnd { protected: CMenu *menu; public: CTool. Bar RGBBar, Shape. Bar; CStatus. Bar statusbar;

CMy. Frame() { } ~CMy. Frame() { } afx_msg int On. Create(LPCREATESTRUCT lp. Create.

CMy. Frame() { } ~CMy. Frame() { } afx_msg int On. Create(LPCREATESTRUCT lp. Create. Struct) { if(CMDIFrame. Wnd: : On. Create(lp. Create. Struct )) return -1; RGBBar. Create(this); RGBBar. Load. Tool. Bar(IDR_TBRGB ); RGBBar. Enable. Docking(CBRS_ALIGN_ANY ); RGBBar. Set. Bar. Style(RGBBar. Get. Bar. Style () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); Shape. Bar. Create(this); Shape. Bar. Load. Tool. Bar(IDR_TBSHAPE ); Shape. Bar. Enable. Docking(CBRS_ALIGN_ANY ); Shape. Bar. Set. Bar. Style(RGBBar. Get. Bar. Style () | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); Enable. Docking(CBRS_ALIGN_ANY ); Dock. Control. Bar(&RGBBar); Dock. Control. Bar(&Shape. Bar ); static UINT indicators[] = { ID_SEPARATOR, IDS_RED, IDS_LINE } ; statusbar. Create(this); statusbar. Set. Indicators(indicators , sizeof(indicators) / sizeof(UINT)); return 0; } afx_msg void On. Exit() { Destroy. Window(); } DECLARE_DYNCREATE(CMy. Frame ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. Frame , CMDIFrame. Wnd) BEGIN_MESSAGE_MAP(CMy. Frame , CMDIFrame. Wnd) ON_WM_CREATE() ON_COMMAND(IDM_EXIT, On. Exit) END_MESSAGE_MAP()

class CMy. View : public CScroll. View { private: COLORREF lcolor, fcolor; Shape *a.

class CMy. View : public CScroll. View { private: COLORREF lcolor, fcolor; Shape *a. Shape; Shape *rd. Shape; int width; HCURSOR hcursor; public: CMy. View() { lcolor = RGB(255, 0, 0); a. Shape = new Line; fcolor = RGB(0, 0, 0); width = 2; } ~CMy. View() { } afx_msg int On. Create(LPCREATESTRUCT lp. Create. Struct) { if (CScroll. View: : On. Create(lp. Create. Struct )==-1) return -1; CSize DCSize(800, 800); Set. Scroll. Sizes(MM_TEXT, DCSize ); return 0; } void Logical. Coor(CPoint* point) { CPoint Origin=Get. Scroll. Position(); point->x=Origin. x+point->x; point->y=Origin. y+point->y; }

void Physical. Coor(CPoint* point) { CPoint Origin=Get. Scroll. Position(); point->x=point->x-Origin. x; point->y=point->y-Origin. y; }

void Physical. Coor(CPoint* point) { CPoint Origin=Get. Scroll. Position(); point->x=point->x-Origin. x; point->y=point->y-Origin. y; } afx_msg void On. Ellipse() { CString resstr; a. Shape = new ellipse; resstr. Load. String(IDS_ELLIPSE ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); } afx_msg void On. Rect() { CString resstr; a. Shape = new rectangle; resstr. Load. String(IDS_RECTANGLE ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); } afx_msg void On. Line() { CString resstr; a. Shape = new Line; resstr. Load. String(IDS_LINE ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(2, resstr. Get. Buffer(80)); }

afx_msg void On. Draw(CDC *a. DC) { CMy. Document *doc = (CMy. Document *)

afx_msg void On. Draw(CDC *a. DC) { CMy. Document *doc = (CMy. Document *) Get. Document(); int num = doc->Get. Size(); CView: : On. Draw(a. DC); int i; for(i = 0; i < num; i++) { CGObject *object = &(doc->Get. Object(i)); switch(object->shapenum) { case 0: rd. Shape = new Line; break; case 1: rd. Shape = new ellipse; break; case 2: rd. Shape = new rectangle; break; } rd. Shape->Set. Point(object->Start. Pnt, object->End. Pnt); rd. Shape->draw((*a. DC), object->Line. Color, object->Fill. Color, object->width); delete rd. Shape; } }

afx_msg void On. LButton. Down(UINT, CPoint point) { Set. Capture(); if(this == Get. Capture())

afx_msg void On. LButton. Down(UINT, CPoint point) { Set. Capture(); if(this == Get. Capture()) { Logical. Coor(&point); (*a. Shape). Start. Pnt = (*a. Shape). End. Pnt = point; switch((*a. Shape). shapenum) { case 0: hcursor = Afx. Get. App()->Load. Cursor(IDC_LINE); : : Set. Cursor(hcursor); break; case 1: hcursor = Afx. Get. App()->Load. Cursor(IDC_ELLIPSE); : : Set. Cursor(hcursor); break; case 2: hcursor = Afx. Get. App()->Load. Cursor(IDC_RECT); : : Set. Cursor(hcursor); break; } } } afx_msg void On. Mouse. Move(UINT, CPoint point) { if(this == Get. Capture()) { CClient. DC a. DC(this); a. DC. Set. ROP 2(R 2_NOT); On. Prepare. DC(&a. DC); Logical. Coor(&point); (*a. Shape). draw(a. DC, lcolor, fcolor, width); (*a. Shape). End. Pnt = point; (*a. Shape). draw(a. DC, lcolor, fcolor, width); } }

// afx_msg void On. LButton. Up(UINT, CPoint point) { if(this == Get. Capture()) {

// afx_msg void On. LButton. Up(UINT, CPoint point) { if(this == Get. Capture()) { Logical. Coor(&point); CClient. DC a. DC(this); (*a. Shape). End. Pnt = point; (*a. Shape). draw(a. DC, lcolor, fcolor, width); CGObject object(a. Shape->Get. Shape. Num(), true, fcolor, lcolor, width, a. Shape->Start. Pnt, a. Shape->End. Pnt); CMy. Document *doc = (CMy. Document *) Get. Document(); doc->Add. Object(object); Physical. Coor(&a. Shape->Start. Pnt); Physical. Coor(&a. Shape->End. Pnt); CRect rect(a. Shape->Start. Pnt, a. Shape->End. Pnt); rect. Normalize. Rect(); rect. Inflate. Rect(5, 5); Invalidate. Rect(&rect); Get. Document()->Update. All. Views(this); Release. Capture(); } } afx_msg void On. Red() { CString resstr; lcolor = RGB(255, 0, 0); resstr. Load. String(IDS_RED ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); }

afx_msg void On. Green() { CString resstr; lcolor = RGB(0, 255, 0); resstr. Load.

afx_msg void On. Green() { CString resstr; lcolor = RGB(0, 255, 0); resstr. Load. String(IDS_GREEN ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); } afx_msg void On. Blue() { CString resstr; lcolor = RGB(0, 0, 255); resstr. Load. String(IDS_BLUE ); ((CMy. Frame *) Afx. Get. Main. Wnd())->statusbar. Set. Pane. Text(1, resstr. Get. Buffer(80)); } afx_msg void On. Update. Ellipse(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 1); } afx_msg void On. Update. Rect(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 2); } afx_msg void On. Update. Line(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check((*a. Shape). shapenum == 0); } afx_msg void On. Update. Red(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(255, 0, 0)); } afx_msg void On. Update. Green(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(0, 255, 0)); } afx_msg void On. Update. Blue(CCmd. UI *a. Cmd. UI) { a. Cmd. UI->Set. Check(lcolor == RGB(0, 0, 255)); }

DECLARE_DYNCREATE(CMy. View ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. View , CScroll. View) BEGIN_MESSAGE_MAP(CMy. View ,

DECLARE_DYNCREATE(CMy. View ) DECLARE_MESSAGE_MAP() } ; IMPLEMENT_DYNCREATE(CMy. View , CScroll. View) BEGIN_MESSAGE_MAP(CMy. View , CScroll. View) ON_WM_CREATE() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_COMMAND(IDM_RED, On. Red) ON_COMMAND(IDM_GREEN, On. Green) ON_COMMAND(IDM_BLUE, On. Blue) ON_COMMAND(IDM_LINE, On. Line) ON_COMMAND(IDM_ELLIPSE, On. Ellipse) ON_COMMAND(IDM_RECTANGLE, On. Rect) ON_UPDATE_COMMAND_UI(IDM_RED, On. Update. Red) ON_UPDATE_COMMAND_UI(IDM_GREEN, On. Update. Green) ON_UPDATE_COMMAND_UI(IDM_BLUE, On. Update. Blue) ON_UPDATE_COMMAND_UI(IDM_LINE, On. Update. Line) ON_UPDATE_COMMAND_UI(IDM_ELLIPSE, On. Update. Ellipse) ON_UPDATE_COMMAND_UI(IDM_RECTANGLE, On. Update. Rect) END_MESSAGE_MAP()

class CMy. App : public CWin. App { public: BOOL Init. Instance() { CMulti.

class CMy. App : public CWin. App { public: BOOL Init. Instance() { CMulti. Doc. Template *a. Doc. Template; a. Doc. Template = new CMulti. Doc. Template(IDR_Child. MENU, RUNTIME_CLASS(CMy. Document ), RUNTIME_CLASS(CMy. MDIChild), RUNTIME_CLASS(CMy. View)); Add. Doc. Template(a. Doc. Template ); CMy. Frame *Frame = new CMy. Frame; m_p. Main. Wnd = Frame; Frame->Load. Frame(IDR_MENU); Frame->Show. Window(SW_SHOW); if (m_lp. Cmd. Line[0]=='') On. File. New(); return true; } DECLARE_MESSAGE_MAP() } ; BEGIN_MESSAGE_MAP(CMy. App , CWin. App) ON_COMMAND(ID_FILE_NEW, CWin. App: : On. File. New) END_MESSAGE_MAP() CMy. App a_app;