_EXAMINING SYMANTEX C++_ by Michael Yam Listing One //----- PTATOM.H - Declares class interface for Periodic Table --- #ifndef __PTATOM_H__ #define __PTATOM_H__ #include "ptdefs.h" class CATOM { private: char Name[PT_NAMELEN+1]; int NumberOfElectrons; int NumberOfShells; public: CATOM (char *szName); ~CATOM(); int DrawAtom (CWnd *Parent); }; class CATOMDialog : public CDialog { private: CATOM *Atom; public: CATOMDialog (char *AtomName); ~CATOMDialog (); //{{ AFX_MSG (CATOMDialog) afx_msg void OnPaint(); afx_msg void OnOK(); //}} AFX_MSG DECLARE_MESSAGE_MAP() }; #endif Listing Two //------ PTATOM.CPP - Periodic Table for Windows ------- #include #include #include #include "resource.h" #include "ptatom.h" //---------------------------------------------------------------------------- // CATOMDialog Constructor -- Creates a modeless dialog box to display an // atom. Also sets dialog caption to atom name and creates atom object. //---------------------------------------------------------------------------- CATOMDialog::CATOMDialog(char *AtomName) { if (stricmp (AtomName, "Hydrogen")) { MessageBox ("This version of PT can only draw Hydrogen", "SORRY", MB_OK | MB_ICONINFORMATION | MB_TASKMODAL); return; } Atom = new CATOM (AtomName); if (Create ("ATOM") == FALSE) MessageBox ("Cannot create modeless dialog box.", "ERROR", MB_OK); else SetWindowText (AtomName); } //--------------------------------------------------------------------------- // ~CATOMDialog Destructor -- Destroys atom object and modeless dialog box. //--------------------------------------------------------------------------- CATOMDialog::~CATOMDialog() { delete Atom; DestroyWindow(); } //-------------------------------------------------------------- // OnOK -- User pressed OK button. Destroy the dialog box. //-------------------------------------------------------------- void CATOMDialog::OnOK() { delete this; } //--------------------------------------------------------------------- // OnPaint -- Received a WM_PAINT message. Get the device context and // draw the atom inside the GroupBox. //--------------------------------------------------------------------- void CATOMDialog::OnPaint() { CPaintDC pdc(this); // paint device context on stack CWnd *GroupBox = GetDlgItem (IDD_GROUPBOX); if (GroupBox != NULL && Atom != NULL) Atom->DrawAtom (GroupBox); } //-------------------------------------------------------------- // CATOM Constructor //-------------------------------------------------------------- CATOM::CATOM (char *szName) { strcpy (Name, szName); } //----------------------------------------------------------------------- // CATOM Destructor -- No handling necessary. Included for completeness. //------------------------------------------------------------------------ CATOM::~CATOM () { } //---------------------------------------------------------------------------- // DrawAtom - This method only draws Hydrogen atom. Output goes to CWnd object. // Drawing is done from outside-in: electron shell, electron, then nucleus. //---------------------------------------------------------------------------- int CATOM::DrawAtom(CWnd *Parent) { RECT rc; int HorzSF, VertSF; // scale factors CClientDC pdc(Parent); // client device context on stack // scale down rectangle. Use ellipse() to describe // electron orbits.around nucleus. Parent->GetClientRect (&rc); HorzSF = rc.right/5; VertSF = rc.bottom/5; rc.left = HorzSF; rc.top = VertSF; rc.right -= rc.left; rc.bottom -= rc.top; pdc.SelectStockObject (HOLLOW_BRUSH); pdc.SelectStockObject (BLACK_PEN); pdc.Ellipse (&rc); // electron orbit // set up to draw electron. Easier if we position // at 12 o'clock. rc.left = rc.left + (rc.right - rc.left)/2 - 4; rc.top -= 4; rc.right = rc.left + 8; rc.bottom = rc.top + 8; pdc.SelectStockObject (BLACK_BRUSH); pdc.Ellipse (&rc); // electron // scale down rectangle to draw nucleus. Parent->GetClientRect (&rc); rc.left = HorzSF*2; rc.top = VertSF*2; rc.right -= rc.left; rc.bottom -= rc.top; pdc.SelectStockObject (BLACK_BRUSH); pdc.Ellipse (&rc); // nucleus return 0; } //-------------------------------------------------------------- // MESSAGE MAP //-------------------------------------------------------------- BEGIN_MESSAGE_MAP (CATOMDialog, CDialog) //{{ AFX_MSG_MAP (CPTATOMDialog) ON_WM_CLOSE () ON_COMMAND (IDOK, OnOK) ON_WM_PAINT () //}} AFX_MSG_MAP END_MESSAGE_MAP() Listing Three ORIGIN = Symantec C++ ORIGIN_VER = Version 6.0 VERSION = DEBUG PROJ = SCPT APPTYPE = WINDOWS EXE PROJTYPE = EXE CC = SC MAKE = MAKE RC = RCC HC = HC ASM = SC DISASM = OBJ2ASM LIBR = IMPLIB LNK = LINK CVPK = CVPACK DLLS = HEADERS = pt.h resource.h ..\..\..\sc\mfc\include\afx.h \ ..\..\..\sc\mfc\include\afxver_.h \ ..\..\..\sc\include\windows.h \ ..\..\..\sc\include\shellapi.h \ ..\..\..\sc\mfc\include\afxres.h \ ..\..\..\sc\mfc\include\afxcoll.h \ ..\..\..\sc\include\win16\print.h \ ..\..\..\sc\mfc\include\afxmsg_.h \ ..\..\..\sc\mfc\include\afxdd_.h \ \sc\mfc\include\afx.h \sc\mfc\include\afxver_.h \ \sc\include\windows.h \sc\include\shellapi.h \ \sc\mfc\include\afxres.h \sc\mfc\include\afxcoll.h \ \sc\mfc\include\afxmsg_.h \sc\mfc\include\afxdd_.h \ \sc\include\win16\windows.h \ ptatom.h ptdefs.h LIBS = ..\..\..\sc\lib\libw.lib \ ..\..\..\sc\mfc\lib\lafxcw.lib \ LIBW.LIB COMMDLG.LIB SHELL.LIB DEFFILE = pt.def CFLAGS = -Jm -ml -C -W1 -s -2 -c -g -gh -gf HFLAGS = $(CFLAGS) LFLAGS = /CO /LI /NOI /INF /RC -k :pt.RES MFLAGS = RESFLAGS = AFLAGS = -c HELPFLAGS = MODEL = L DEFINES = RCDEFINES = LIBDIRS = INCLUDES = -I\SC\INCLUDE -I\SC\MFC\INCLUDE OBJS = pt.OBJ ptatom.OBJ RCFILES = RESFILES = pt.RES SYMS = pt.SYM resource.SYM HELPFILES = BATS = .C.OBJ: $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -o$*.obj $*.c $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -o$*.obj $*.cpp .CXX.OBJ: $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -o$*.obj $*.cxx $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -o$*.obj $*.cp .H.SYM: $(CC) $(HFLAGS) $(DEFINES) $(INCLUDES) -HF -o$*.sym $*.h .HPP.SYM: $(CC) $(HFLAGS) $(DEFINES) $(INCLUDES) -HF -o$*.sym $*.hpp .HXX.SYM: $(CC) $(HFLAGS) $(DEFINES) $(INCLUDES) -HF -o$*.sym $*.hxx .C.EXP: $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -e $*.c -l$*.lst $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -e $*.cpp -l$*.lst .CXX.EXP: $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -e $*.cxx -l$*.lst $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -e $*.cp -l$*.lst .ASM.EXP: $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -e $*.asm -l$*.lst .OBJ.COD: $(DISASM) $*.OBJ >$*.cod .EXE.COD: $(DISASM) $*.EXE >$*.cod .COM.COD: $(DISASM) $*.COM >$*.cod .OBJ.EXE: $(LNK) $(LFLAGS) @$(PROJ).LNK .OBJ.COM: $(LNK) $(LFLAGS) @$(PROJ).LNK .DLL.LIB: $(LIBR) $*.LIB $*.DLL .DEF.LIB: $(LIBR) $*.LIB $*.DEF .RTF.HLP: $(HC) $(HELPFLAGS) $*.HPJ .ASM.OBJ: $(ASM) $(AFLAGS) $(DEFINES) $(INCLUDES) $*.ASM .RC.RES: $(RC) $(RCDEFINES) $(RESFLAGS) $(INCLUDES) $*.rc .DLG.RES: echo \#include "windows.h" >$$$*.rc echo \#include "$*.h" >>$$$*.rc echo \#include "$*.dlg" >>$$$*.rc $(RC) $(RCDEFINES) $(RESFLAGS) $$$*.rc -del $*.res -ren $$$*.res $*.res all: $(PROJ).$(PROJTYPE) done $(PROJ).$(PROJTYPE): $(PROJS) $(OBJS) $(RCFILES) \ $(RESFILES) $(HELPFILES) $(BATS) $(LNK) $(LFLAGS) @$(PROJ).LNK $(CVPK) $$SCW$$.$(PROJTYPE) -del $(PROJ).$(PROJTYPE) -ren $$SCW$$.$(PROJTYPE) $(PROJ).$(PROJTYPE) done: -echo $(PROJ).$(PROJTYPE) done buildall: clean all clean: -del $(PROJ).$(PROJTYPE) -del SCPH.SYM -del pt.OBJ -del ptatom.OBJ -del pt.SYM -del resource.SYM cleanres: res: cleanres $(RCFILES) link link: $(LNK) $(LFLAGS) @$(PROJ).LNK $(CVPK) $$SCW$$.$(PROJTYPE) -del $(PROJ).$(PROJTYPE) -ren $$SCW$$.$(PROJTYPE) $(PROJ).$(PROJTYPE) pt.OBJ: \ pt.cpp \ \sc\mfc\include\afx.h \ \sc\mfc\include\afxver_.h \ \sc\include\windows.h \ \sc\include\shellapi.h \ \sc\mfc\include\afxres.h \ \sc\mfc\include\afxcoll.h \ ..\..\..\sc\include\win16\print.h \ \sc\mfc\include\afxmsg_.h \ \sc\mfc\include\afxdd_.h \ resource.h \ pt.h \ ptdefs.h \ ptatom.h ptatom.OBJ: \ ptatom.cpp \ \sc\mfc\include\afx.h \ \sc\mfc\include\afxver_.h \ \sc\include\windows.h \ \sc\include\shellapi.h \ \sc\mfc\include\afxres.h \ \sc\mfc\include\afxcoll.h \ ..\..\..\sc\include\win16\print.h \ \sc\mfc\include\afxmsg_.h \ \sc\mfc\include\afxdd_.h \ resource.h \ ptatom.h \ ptdefs.h Figure 3: (a) MFC's CPaintDC method; (b) creating the CPaintDC object on the stack (a) CPaintDC *pdc = new CPaintDC(this); [painting routine here] delete pdc; (b) CPaintDC pdc(this); [painting routine here]