EX - NO:1 Date:: VC++ Programminng
EX - NO:1 Date:: VC++ Programminng
EX - NO:1 Date:: VC++ Programminng
EX.NO:1 DATE :
WINDOW CREATION Aim: To write a program to create an empty window using MFC. Algorithm: Step-1 : Start the process. Step-2 : Create a project using Win 32 application. Step-3 : select c++ source file for coding. Step-4 : Derive a class from CWinApp base class. Step-5 : Declare the InitInstance() function in derived class. Step-6 : Derive a class from CFrameWnd base class. Step-7 : Declare a constructor in the class.
1
VC++ programminng
Step-8 : Create a global object for the derived class of CWinApp in the derived class. Step-9 : Create an empty window in the constructor of derived class by overriding the Create () function. Step-10: Create an object for the application class in the InitInstance method of the class Derived from the CWinApp class. Step-11:Stop the process.
VC++ programminng
Source code: #include<afxwin.h> class myapp : public CFrameWnd { public: myapp() { Create(0,"trial window"); } }; class mywin : public CWinApp { public: int InitInstance() { myapp *ob; ob = new myapp; ob->ShowWindow(10); m_pMainWnd = ob; return 1; }
3
VC++ programminng
}; mywin obb;
VC++ programminng
Output:
VC++ programminng
TEST CASES
AC ST TU AT AL US O/P Sa me as EO Sa me as EO
i) mywin i)Decla Vali i)InitI obb; ration d nstance of method object is should called Inva be in the Global lid class ii)if derived Declara from tion of the object CWinApp not be class. Global ii)the constru ctor
Suc ces s
VC++ programminng
for the class derived from the CFrameW nd is called inside the InitIns tance method. TC- Process 02 case: i)Create(i nt,string) i)Create() is invoked inside the constructo r of the class derived from the
i)two Vali Sa paramet d me ers as Should EO be used for Create( ) functio n. Inva
7
Suc ces s
VC++ programminng
CFrameW nd.
a)param lid eter of type integer . b)param eter of type string. ii) if the paramat ers are three in number
Sa me as EO
TC- Output 04 case: i) myapp checks *ob; for the ob = formatt ne ed w output my
Suc ces s
VC++ programminng
the Inva functio lid Sa ns me as ii)Exec EO ution of functio ns are not perform ed if the call does not taken place
Test cases:
9
VC++ programminng
EX.NO: 2 DATE : MENUS Aim: To write a program using MFC to perform arithmetic operations using menus. Algorithm: Step-1 :Start the process. Step-2 :Create an empty frame window inside the class (myframe) derived from the MFC class called CFrameWnd by using the following function, Create (0,"EDIT CONTROL");
10
VC++ programminng
Step-3 :Create a menu with the help of the CMenu class. Whose menu items are defined in the resource file. Step-4 :Load the menu to the myframe class by using the following function, CMenu *m m-> LoadMenu(10). Step-5 :Create two editboxes, labels, a button to the frame by using the following methods, st1.Create("Number1",WS_CHILD|WS_ VISIBLE|SS_RIGHT|SS_CENTERIMA GE, CRect(100,40,210,65),this,1); st2.Create("Number2",WS_CHILD|WS_ VISIBLE|SS_RIGHT|SS_CENTERIMA GE, CRect(100,75,210,105),this,1); e1.CreateEx(WS_EX_CLIENTEDGE," EDIT","",WS_CHILD|WS_VISIBLE|ES _AUTOHSCROLL,CRect(220,30,420,6 5),this,2);
11
VC++ programminng
e2.CreateEx(WS_EX_CLIENTEDGE," EDIT","",WS_CHILD|WS_VISIBLE|ES _AUTOHSCROLL,CRect(220,75,420,1 10),this,4); b.Create("OK",BS_PUSHBUTTON|WS _CHILD|WS_VISIBLE,CRect(290,150, 320,190),this,6); where e1,e2,st1,st2 ,b are the objects of the classes CEdit CStatic CButton respectively. Step-6 :Declare message maps to handle the messages of the menus inside the class myframe. Step-7 :Create an another class deriving CWinApp. It overrides InitInstance() function. call the constructor of the class myframe dynamically. Step-8 : Frame window is created with menu which includes the following menu items i)ADDITION ii) SUBTRACTION iii) MULTIPLICATION
12
VC++ programminng
iv) DIVISION Step-9 : Enter the values to the editboxes. Step-10:Result is displayed inside the message box corresponding the operation selected in the menuitem Step-11: stop the process.
SOURCE CODE: #include<afxwin.h> class myframe : public CFrameWnd { private: CEdit e1,e2; CStatic st1,st2; CButton b; char c; CMenu *m; public: myframe() {
13
VC++ programminng
Create(0,"EDIT CONTROL"); m = new CMenu; m->LoadMenu(1); SetMenu(m); } int OnCreate(LPCREATESTRUCT l) { st1.Create("Number1",WS_CHILD|WS_ VISIBLE|SS_RIGHT|SS_CENTERIMA GE, CRect(100,40,210,65),this,1); st2.Create("Number2",WS_CHILD|WS_ VISIBLE|SS_RIGHT|SS_CENTERIMA GE, CRect(100,75,210,105),this,1); e1.CreateEx(WS_EX_CLIENTEDGE," EDIT","",WS_CHILD|WS_VISIBLE|ES _AUTOHSCROLL,CRect(220,30,420,6 5),this,2); e2.CreateEx(WS_EX_CLIENTEDGE," EDIT","",WS_CHILD|WS_VISIBLE|ES _AUTOHSCROLL,CRect(220,75,420,1 10),this,4);
14
VC++ programminng
b.Create("OK",BS_PUSHBUTTON|WS _CHILD|WS_VISIBLE,CRect(290,150, 320,190),this,6); return 0; } void add() { c = 'a'; } void sub() { c = 's'; } void mul() { c = 'm'; }
void div() {
15
VC++ programminng
c = 'd'; } int ok() { CString s,s1; int dd,dd1,dd2; e1.GetWindowText(s); dd =atoi(s) ; e2.GetWindowText(s1); dd1 = atoi(s1); if(c=='a') dd2 = dd + dd1; if(c=='s') dd2 = dd - dd1; if(c=='m') dd2 = dd * dd1; if(c=='d') dd2 = dd / dd1; s1.Format("%d",dd2); MessageBox(s1,"text"); return 1; } DECLARE_MESSAGE_MAP() };
16
VC++ programminng
BEGIN_MESSAGE_MAP(myframe,CFram eWnd) ON_WM_CREATE() ON_COMMAND(6,ok) ON_COMMAND(101,add) ON_COMMAND(102,sub) ON_COMMAND(103,mul) ON_COMMAND(104,div) END_MESSAGE_MAP() class myapp : public CWinApp { public: int InitInstance() { myframe *p; p= new myframe; p->ShowWindow(3); m_pMainWnd = p; return 1; } }; myapp ob;
17
VC++ programminng
.rc file 1 MENU BEGIN MENUITEM "ARITHMETIC",101 MENUITEM "ADDITION",111 MENUITEM "SUBTRACTION",222 MENUITEM "MULTIPLICATION",333 MENUITEM "DIVISION",444 END
18
VC++ programminng
19
VC++ programminng
OUTPUT:
20
VC++ programminng
Test cases:
21
VC++ programminng
TE TEST TEST ST- DESCRI DATA ID PTION TC 01 INPUT CASE: i) enter the values for the edit boxes e1,e2 ii)writ e the values to the string variabl e s,and convert it into the
TEST EXP ACT STA CASES ECT UAL TUS ED O/P O/P Vali Sam d e as Suc EO ces s Inva lid Sam e as EO
i)CStri Input should ng s be an intege r value. ii)int If dd,dd1 input is not an intege r.
22
VC++ programminng
TC 02
integer variabl es dd,dd1 Process case: If(c== a) i)check dd2 = the dd + value dd1 of the If(c== variabl s) e c dd2 = dd dd1 If(c== m) dd2 = dd * dd1 If(c== d) dd2 = dd /
Input Vali Sam should d e be as intege EO Suc r ces s Inva lid Sam e If as input EO is not intege r
23
VC++ programminng
dd1
TC 03
Process case:
Value i)s1.Fo should i)conve rmat(% be an rt the d,dd2) integer values of dd2 If value into not be string an integer Output case: Checks for formatt ed
TC 03
VC++ programminng
output.
call s of the Inva functi lid Sam ons. e as Execut EO ion of functi ons are not perfor med if the call does not taken place
EX.NO:3
25
VC++ programminng
DATE : DRAWING CIRCLES AND RECTANGLES Aim: To write an mfc program to draw circles and rectangles. Algorithm: Algorithm: Step-1: Start the process. Step-2: i)Create an empty frame window inside the class (draw) derived from the MFC class called CFrameWnd by using the following function, Create (0,"EDIT CONTROL") in its constructor. ii) define an another overridable function called void OnPaint(). Step-3 :inside the function void OnPaint(), i)declare an object for the class device context Class CPaintDC as dc. ii)declare an object for the class CPen as pen.
26
VC++ programminng
iii)declare an object for the class CBrush as brush. Use the following methods to draw circle and rectangles, dc.TextOut(100,20,"RECTANGLE"); dc.Rectangle(200,100,350,20); dc.TextOut(100,220,"CIRCLE"); dc.Ellipse(200,200,280,280); Step-4 : Declare message map to handle the messages during on paint by using the macro ON_WM_PAINT() Step-5 : Derive a class from CWinApp base class. Step-6 : Declare the InitInstance() function in derived class. Step-7 :Create a global object for the derived class of CWinApp. Step-8 : Create an object for the application class in the InitInstance method of the class Derived from the CWinApp class. A Constructor of that class is automatically Invoked.
27
VC++ programminng
Step-9 : a circle and a rectangle has drawn inside the empty window Step-11:Stop the process.
28
VC++ programminng
Pseudocode: #include<afxwin.h> class draw : public CFrameWnd { public: draw() { Create(0,"draw circle and rectangle"); } void OnPaint() { CPaintDC dc(this); CPen pen; pen.CreatePen(PS_DOT,1,RGB(30,0,0)) ; dc.SelectObject(&pen); CBrush brush; brush.CreateSolidBrush(RGB(150,250,3 00)); dc.SelectObject(&brush);
29
VC++ programminng
dc.TextOut(100,20,"RECTANGLE"); dc.Rectangle(200,100,350,20); dc.TextOut(100,220,"CIRCLE"); dc.Ellipse(200,200,280,280); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(draw,CFrameW nd) ON_WM_PAINT() END_MESSAGE_MAP() class Dapp: public CWinApp { public: int InitInstance() { draw *dr; dr = new draw(); dr->ShowWindow(1); m_pMainWnd = dr; return 1; } };
30
VC++ programminng
Dapp da;
Output:
31
VC++ programminng
32
VC++ programminng
33
VC++ programminng
TE TEST TEST ST- DESCRIP DATA ID TION TC- Process 01 case: i) mywin i)InitI obb; nstance method is called in the class derived from the CWinApp class. ii)the constru ctor for the class
TEST EXP CASES ECT ED O/P i)Decl aratio n of object should be Global ii)if Declar ation of object not be Global
AC ST TU AT AL US O/P
34
VC++ programminng
derived from the CFrameW nd is called inside the InitIns tance method. TC- Process i)Create(int i)two Vali parame d 02 case: ,string) ters i)Create() Should is invoked be inside the used constructor for of the class Create derived () from the functi CFrameWn on. Inva d. a)para lid meter
Sam e as EO
Sam e as EO
35
VC++ programminng
of type intege r. b)para meter of type string . ii) if the parama ters are three in number TC- Process i)CPaint i) Vali Sam 03 case: i)OnPai DC Parame d e nt() dc(this) ters as functio ii)CPen should EO
36
VC++ programminng
pen overri iii)CBru de the sh virtua Inva brush; l lid Sam method e s. as EO ii)if parame ters should no overri de the virtua l method s. i)Exec Vali Sam ution d e is as perfor EO med by
37
TC- Output i) myapp 04 case: *ob; checks ob = new for the mya formatt pp;
VC++ programminng
the call Inva of the lid Sam functi e ons as EO ii)Exe cution of functi ons are not perfor med if the call does not taken place
38
VC++ programminng
Test cases: EX.NO:4 DATE : CREATION OF CHILD AND PARENT WINDOWS Aim: To write an mfc program to create child and parent windows. Algorithm: Step-1: Start the process. Step-2: Create an empty frame window inside the class (mywin) derived from the MFC class called CFrameWnd by using the following function, Create(0,"windows-forms "); Step-3: Create an another frame window inside the class (mywin1) derived from the MFC class called CFrameWnd by using the following function, Create(0,child window);
39
VC++ programminng
Step-4: Load the menu to the parent window created by mywin from the .rc file. Step-5: Define a function called neww() in the class mywin. Step-6. Inside the neww set the window of the mywin1 as its child by, mywin1 *o=new mywin1; o->SetParent(this); Step-7: Declare message map to handle the messages in the class mywin. Step-8: Derive a class from CWinApp base class. Step-9: Declare the InitInstance() function in derived class. Step-10: Create a global object for the derived class of CWinApp. Step-11: Create an object for the parent class(mywin) in the InitInstance method of the Class Derived from the CWinApp class. A Constructor of that class is automatically Invoked. Step-12: Child and parent windows are created and showed.
40
VC++ programminng
41
VC++ programminng
Source code: #include<afxwin.h> UINT c; CString s,s1; class mywin1 : public CFrameWnd { public: mywin1() { s.Format("child window %d",c); Create(0,s); c+=1; } }; class mywin : public CFrameWnd { public: mywin() { CMenu *m; Create(0,"windows-forms "); m = new CMenu; m->LoadMenu(11);
42
VC++ programminng
SetMenu(m); } void neew() { mywin1 *o=new mywin1; o->SetParent(this); o->ShowWindow(3); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(mywin,CFrame Wnd) ON_COMMAND(101,neew) END_MESSAGE_MAP() class myapp : public CWinApp { public: int InitInstance() { c=0; mywin *ob; ob = new mywin; ob->ShowWindow(3); m_pMainWnd = ob;
43
VC++ programminng
44
VC++ programminng
45
VC++ programminng
Test cases: TE TEST TEST ST- DESCRIPT DATA ID ION TC- Process i) mywin 01 case: obb; i) InitInsta nce method is called in the class derived from the CWinApp class. ii) The construct or for the class derived from the
AC TU AL O/ P i)Decl Vali Sa aratio d me n of as object EO should be Inva global lid Sa ii)if me Declar as ation EO of object not be Global
ST A T U S
46
VC++ programminng
CFrameWnd is called inside the InitInsta nce method. TC- Process i)Create(int i)two Vali Sa parame d me 02 case: ,string) ters as i)Create() is Should EO invoked be Inside the used constructor for of the parent Create class derived () Inva from the functi lid Sa CFrameWnd on. me named a)para as mywin. meter EO of type intege r.
47
VC++ programminng
b) parame ter of type string . ii) if the parama ters are three in number TC- Process i)Create(int i)two Vali Sa parame d me 03 case: ,string) ters as i)Create() is Should EO invoked be Inside the used constructor for of the child Create Sa class derived () me from the
48
VC++ programminng
functi as on. Inva EO a)para lid meter of type intege r. b)para meter of type string . ii) if the parama ters are three in number
49
VC++ programminng
TC- Output i) myapp 04 case: *ob; Checks ob = new for the mya formatted pp; output ie ii)oba parent >ShowWin window dow and then iii) mywin1 child *o=new window mywin1; iv)o>SetParent(t his); v)o>ShowWin dow(3);
i)Exec ution is perfor med by the call of the functi ons ii)Execu tion of function s are not perform ed if the call does not taken place
50
VC++ programminng
EX.NO:5 DATE : CREATION OF DIALOG BOX Aim: To write a program using MFC to create a Dialog box. Algorithm: Step-1: Start the process. Step-2: Create an empty frame window inside the class (myf) derived from the MFC class called CFrameWnd by using the following function, Create (0,"simple dialog box",WS_OVERLAPPEDWINDOW); Step-3: Create a menu with the help of the CMenu class. Whose menu items are defined in the resource file. Step-4: Load the menu to the myf class by using the following function,
51
VC++ programminng
CMenu *m M->LoadMenu (10). Step-5: Create a modal dialog box by selecting insert resource dialog new. And to the Current project. Step-6: Derive a class mydialog from the CDialog Step-7: Override the methods OnOk(),OnCancel() inside the class mydialog.it also includes an user defined method called about. Step-8: the function about handles the modals of the dialog named IDD_DIALOG1 Step-9: Declare the message maps inside the class myf to handle the messages. Step-11: stop the process.
52
VC++ programminng
Pseudocode: #include<afxwin.h> Class myd: public CDialog { Public: myd(int n):CDialog(n) { } void OnOK() { CDialog::OnOK(); MessageBox("u have pressed the ok button","ok button handler"); } void OnCancel() { CDialog::OnCancel(); MessageBox("u have pressed the ok button","cancel button handler");
53
VC++ programminng
} }; class myf : public CFrameWnd { public: myf() { CMenu *m; Create(0,"simple dialog box",WS_OVERLAPPEDWINDOW); m = new CMenu; m->LoadMenu(10); SetMenu(m); } void about() { myd d(11); d.DoModal(); } DECLARE_MESSAGE_MAP(); }; BEGIN_MESSAGE_MAP(myf,CFrameW nd)
54
VC++ programminng
ON_COMMAND(102,about) END_MESSAGE_MAP() class mmm : public CWinApp { public: int InitInstance() { myf *ob; ob = new myf; ob->ShowWindow(10); m_pMainWnd = ob; return 1; } }; mmm pb;
VC++ programminng
BEGIN MENUITEM "ABOUT",101 POPUP "&ABOUT US" BEGIN MENUITEM "ABOUT",102 END END
Output:
56
VC++ programminng
Test cases:
57
VC++ programminng
TE TEST TEST ST- DESCRIPT DATA ID ION TC- Process 01 case: i)InitIns tance method is called in the class derived from the CWinApp class. ii)The construct or for the class derived from the CFrameWnd is called i) mywin obb; ii)myd d(11); iii)myf *ob;
AC TU AL O/ P i)Decl Vali Sa aratio d me n of as object EO should be Inva global lid Sa ii)if me Declar as ation EO of object not be Global
ST A T U S
58
VC++ programminng
inside the InitInsta nce method. iii)The construct or for the class CDialog is called inside the myf class TC- Process i)Create(int,st i)two Vali Sa parame d me 02 case: ring) i)Create( ters as ) is Should EO invoked be Inside used the for construct Create or of () Inva the functi lid Sa
59
VC++ programminng
on. a)para meter of type intege r. b) parame ter of type string . ii) if the parama ters are three in number
me as EO
60
VC++ programminng
TC- Process 03 case: i) the function about is called by the handling of menu messages. ii) the call of about does the events of OK and CANCEL button
Sa Vali me d as EO
i)myf *ob;
Inva lid Sa me as ii)han EO dling doesno t happen s if there is no proper refere nces i)Exec Vali Sa ution d me
61
VC++ programminng
Checks ii)ob = for the new myf; formatted iii)oboutput ie >ShowWind a parent ow(10); window and then child window
is as perfor EO med by the call Inva of the lid Sa functi me ons as EO ii)Execu tion of function s are not perform ed if the call does not taken place
EX.NO:6 DATE :
62
VC++ programminng
SPLITTER WINDOW CREATION Aim: To write a program to create an empty splitter window using MFC for illustrating Document view programming. Algorithm: Step-1 : Start the process. Step-2 : Create a project using Win 32 application. Step-3 : select c++ source file for coding. Step-4 : Derive a class from CWinApp base class. Step-5 : Declare the InitInstance() function in derived class. Step-6 : Derive a class from CFrameWnd base class. Step-7 : Declare a constructor in the class.
63
VC++ programminng
Step-8 : Create an empty window in the constructor of derived class by overriding the Create () function. Step-9 : Override a function called OnCreateClient(CREATESTRUCT*l,CCrea teContext *c). Step-10: Inside the function OnCreateClient(),use the following to create splitter windows CRuntimeClass *w = RUNTIME_CLASS(CRichEditView); s.CreateStatic(this,1,2); s.CreateView(0,0,w,z,c); s.CreateView(0,1,w,z,c); Step-11: Create an object for the application class in the InitInstance method of the class Derived from the CWinApp class. Step-12: Stop the process.
64
VC++ programminng
Pseudocode: #include<afxwin.h> #include<afxext.h> #include<afxrich.h> class mywin : public CFrameWnd { public: CSplitterWnd s; mywin() { Create(0,"Splitter Window"); } int OnCreateClient(CREATESTRUCT *l,CCreateContext *c) { SIZE z; z.cx =300;z.cy = 100; CRuntimeClass *w; w= RUNTIME_CLASS(CRichEditView); s.CreateStatic(this,1,2); s.CreateView(0,0,w,z,c);
65
VC++ programminng
s.CreateView(0,1,w,z,c); return 1; } }; class myapp : public CWinApp { public: int InitInstance() { mywin *ob = new mywin; ob->ShowWindow(3); m_pMainWnd = ob; return 1; } }; myapp o;
66
VC++ programminng
output:
67
VC++ programminng
Test cases:
68
VC++ programminng
TEST CASES
E X P E C T E D O /P V a l i d
AC TU ST AL AT O/P US
TC- Process 01 case: i)InitI nstance method is called in the class derived from the
i)myapp obb;
Sa me as EO
Sa me I as n EO v a
69
VC++ programminng
CWinApp class. TC- Process 02 case: i)Creat e() is invoked Inside the constru ctor of the derived class from the CFrameW nd.
not be l Global i d i)two V i)Create(int, parame a ters l string) Should i be d used for Create () functi on. a)para I meter n of v type a intege l r. i b) d parame ter of type
Sa me as EO
Sa me as EO
70
VC++ programminng
string . ii) if the parama ters are three in number Process TC- case: 03 i)OnCre ateClie nt() is invoked after the constru ctor is invoked . i)Two OnCreateCli parame ters ent (CREATEST are requir RUCT *l,CCreateC ed for OnCrea ontext *c) teClie nt ii)if the parame ters V a l i d Sa me as EO
Sa me I as n EO v a l i
71
VC++ programminng
TC- Process 03 case: CRuntimeCl i)split ass *w; ting w= takes RUNTIME_ place CLASS(CRi accordi chEditView); ng s..CreateStati specifi c(this,1,2) ed s.CreateVie points. w(0,0,w,z,c) s..CreateVie w(0,1,w,z,c) TC- Output
i)if typing is possib le in the window ii)if typing is imposs ible in the window i)Exec
V a l i d
Sa me as EO
Sa me I as n EO v a l i d V Sa
72
VC++ programminng
04
case: Checks for the formatt ed output ie a parent window and then child window
ution is perfor med by the call of the functi ons ii)Executi on of functions are not performe d if the call does not taken place.
a me l as i EO d Sa me I as n EO v a l i d
EX.NO:7 DATE :
73
VC++ programminng
MULTIPLE DOCUMENT INTERFACE Aim: To write a program to a simple notepad application that demonstrates serialization on MULTIPLE DOCUMENT INTERFACE. Algorithm: Step-1:Start the process. Step-2:Create a project using Win 32 application. Step-3:Derive a class mydoc from CDocument. Step-4:Override the function Serialize with the argument type CArchive Step-5:Declare two macros in the class mydoc as, DECLARE_SERIAL() IMPLEMENT_SERIAL()
74
VC++ programminng
Step-6:Derive a class myview from CEditView. Step-7: Inside the class myview declare two macros as, DECLARE_DYNCREATE(myview) DECLARE_MESSAGE_MAP() Step-8: Add the .rc file to the current project. Step-8:Define a function aa(). inside the function aa set b as savemodified where b is the object of the class myview. Step-9 :Derive a class from CWinApp base class. Step-10:Declare the InitInstance() function in derived class. Step-11: inside the InitInstance function declare an object for the class CFrameWnd to create an Empty window. Step-12:Create a global object for the derived class of CWinApp in the derived class.
75
VC++ programminng
Step-13:Create an empty window in the constructor of derived class by overriding the Create () function. Step-14:Stop the process.
76
VC++ programminng
Pseudocode: #include<afxwin.h> #include<afxext.h> class mydoc : public CDocument { public: mydoc() { MessageBox(0,"Object Constructing","CDocument",0); } DECLARE_SERIAL(mydoc) void Serialize(CArchive &ar) { CEditView *e; e = (CEditView *)m_viewList.GetHead(); e->SerializeRaw(ar); } }; IMPLEMENT_SERIAL(mydoc,CDocument ,0) class myview : public CEditView {
77
VC++ programminng
DECLARE_DYNCREATE(myview) public: myview() { MessageBox("object constructing","CEditView"); } void OnDraw(CDC *) { } void aa() { mydoc *b; b = (mydoc *)m_pDocument; b->SetModifiedFlag(); b->SaveModified(); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(myview,CEditV iew) ON_COMMAND(ID_FILE_SAVE,aa) ON_COMMAND(ID_FILE_SAVE_AS,aa) END_MESSAGE_MAP()
78
VC++ programminng
IMPLEMENT_DYNCREATE(myview,CEd itView)
class myapp : public CWinApp { public: int InitInstance() { AfxMessageBox("In InitInstance()",0); LoadStdProfileSettings(); CSingleDocTemplate *psdoc; CRuntimeClass *d,*v,*f; d = RUNTIME_CLASS(mydoc); v = RUNTIME_CLASS(myview); f = RUNTIME_CLASS(CFrameWnd); psdoc = new CSingleDocTemplate(100,d,f,v); AddDocTemplate(psdoc);
79
VC++ programminng
CFrameWnd *fw; fw = new CFrameWnd; fw>Create(0,"notepad",WS_OVERLAPPEDWIN DOW,(CRect)0,0,"menu200"); fw->ShowWindow(3); m_pMainWnd = fw; return 1; } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(myapp,CWinAp p) ON_COMMAND(201,CWinApp::OnFileNe w) ON_COMMAND(202,CWinApp::OnFileO pen) END_MESSAGE_MAP() myapp o; .rc file: #include<afxres.h> menu200 MENU
80
VC++ programminng
BEGIN POPUP "FILES" BEGIN MENUITEM "NEW",201 MENUITEM "OPEN",202 END END 100 MENU BEGIN MENUITEM "&SAVE",ID_FILE_SAVE END
Output:
81
VC++ programminng
82
VC++ programminng
TEST CASES
E X P E C T E D O /P
AC TU ST AL AT O/P US
Test cases:
83
VC++ programminng
TC- Process 01 case: i)InitI nstance method is called in the class derived from the CWinApp class. TC- Process 02 case: i)Creat e() is invoked Inside the constru ctor of
i)myapp obb;
i)Decl aratio n of object should be global ii)if Declar ation of object not be Global
V a l i d
Sa me as EO
VC++ programminng
functi on. a)para meter of type intege r. b) parame ter of type string . ii) if the parama ters are three in number
Sa me I as n EO v a l i d
85
VC++ programminng
Process TC- case: 03 i)OnCre ateClie nt() is invoked after the constru ctor is invoked .
i)Two OnCreateCli parame ters ent (CREATEST are requir RUCT *l,CCreateC ed for OnCrea ontext *c) teClie nt ii)if the parame ters are not equal to three.
V a l i d
Sa me as EO
Sa me I as n EO v a l i d
86
VC++ programminng
TC- Process 03 case: CRuntimeCl i)split ass *w; ting w= takes RUNTIME_ place CLASS(CRi accordi chEditView); ng s..CreateStati specifi c(this,1,2) ed s.CreateVie points. w(0,0,w,z,c) s..CreateVie w(0,1,w,z,c)
i)if typing is possib le in the window ii)if typing is imposs ible in the window i)Exec TC- Output i)myf ution 04 case: Checks *ob; is for the ii)ob = perfor formatt new myf; med by ed iii) the output obcall ie a >ShowWind of the parent ow(10); functi
V a l i d
Sa me as EO
Sa me I as n EO v a l i d V Sa a me l as i EO d Sa me
87
VC++ programminng
ons ii)Executi on of functions are not performe d if the call does not taken place.
I as n EO v a l i d
EX.NO:8 DATE : SINGLE DOCUMENT INTERFACE AIM: To write a vc++ program to demonstrate multiple document interface. ALGORITHM:
88
VC++ programminng
89
VC++ programminng
PSEUDOCODE #include<afxwin.h> class mywin : public CFrameWnd { public: mywin() { Create(0,"cfile",WS_OVERLAPPEDWI NDOW,rectDefault,0,"mmmm");
90
VC++ programminng
} void AA() { CFile *f; f = new CFile(); f>Open("D:\\NEW\\JJ.txt",CFile::modeWrite ); f->Write ("jayam",0); f->Close (); MessageBox("data","filewrite"); } void bb() { CFile *f; char x[100]; f = new CFile(); f>Open("D:\\NEW\\JJ.txt",CFile::modeRead) ; DWORD size = f->GetLength(); f->Read(x,size); f->Close (); d.TextOut(50,50,x,size);
91
VC++ programminng
} DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(mywin,CFrame Wnd) ON_COMMAND(100,AA) ON_COMMAND(101,BB) END_MESSAGE_MAP() class myapp : public CWinApp { public: int InitInstance() { mywin *p = new mywin(); p->ShowWindow (3); m_pMainWnd = p; return 1; } }; myapp ob;
92
VC++ programminng
.rc file mmmm MENU BEGIN MENUITEM "WRITE",100 MENUITEM "READ",101 END
93
VC++ programminng
94
VC++ programminng
LISTBOX APPLICATION #include<afxwin.h> class MyFrame:public CFrameWnd { private: CListBox list1; CButton button1,button2,button3; CEdit area1; public: MyFrame() { Create(0,"ListBox"); } int OnCreate(LPCREATESTRUCT S) { CFrameWnd::OnCreate(S); button1.Create("CONFIRM",BS_PUSH BUTTON|WS_CHILD|WS_VISIBLE,CRec t(500,100,600,230),this,2); button2.Create("remove",BS_PUSHBU
95
VC++ programminng
TTON|WS_CHILD|WS_VISIBLE,CRect(5 00,300,600,430),this,3); button3.Create("add",BS_PUSHBUTTO N|WS_CHILD|WS_VISIBLE,CRect(500,50 0,600,630),this,4); area1.CreateEx(WS_EX_CLIENTEDG E,"EDIT",NULL,WS_CHILD|WS_VISIBL E|ES_MULTILINE,CRect(300,20,480,100), this,3); list1.CreateEx(WS_EX_CLIENTEDGE, "LISTBOX",NULL,WS_CHILD|WS_VISI BLE|LBS_MULTIPLESEL,CRect(100,50,2 00,250),this,1); list1.AddString("Madras"); list1.AddString("Coimbatore"); list1.AddString("Hyderabad"); list1.AddString("Pune"); list1.AddString("Bangalore"); return 0; } void confirm()
96
VC++ programminng
{ CString s1; int n[10]; int c=list1.GetCount(); int a=list1.GetSelItems(c,n); CString s=" "; for(int j=0;j<a;j++) { list1.GetText(n[j],s1); s=s+s1+"\r\n"; } MessageBox("Your Select is \n"+s,"AAAA",MB_ICONINFORMATION ); area1.SetWindowText(s); } void remove() { CString s1; int n[10]; int c=list1.GetCount(); list1.DeleteString(list1.GetCurSel()); } void add()
97
VC++ programminng
{ CString s; area1.GetWindowText(s); list1.AddString(s); area1.SetWindowText(""); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyFrame,CFra meWnd) ON_WM_CREATE() ON_COMMAND(2,confirm) ON_COMMAND(3,remove) ON_COMMAND(4,add) END_MESSAGE_MAP() class MyApp:public CWinApp { public: int InitInstance() { MyFrame *p=new MyFrame; m_pMainWnd=p; m_pMainWnd->ShowWindow(3);
98
VC++ programminng
return 1; } }; MyApp a;
99
VC++ programminng
EX.NO:10 DATE : PRINTING Aim: To write a program to demonstrate printing by specifying the printer device name and print preview using application wizard.. Algorithm: Step-1: Start the process. Step-2: Create a project using Win 32 application. Step-3: Derive a class mywin from CFrameWnd. Step-4: Override the function Serialize with the argument type CArchive Step-5: Declare an object for the class CPrintDialog . Step-6: Add the .rc file to the current project.
100
VC++ programminng
Step-7: Derive a class from CWinApp base class. Step-8: Declare the InitInstance() function in derived class. Step-9: Inside the InitInstance function declare an object for the class CFrameWnd to create an Empty window. Step-10:Declare Message map for the class mywin to handle commands. Step-11:Create a global object for the derived class of CWinApp in the derived class. Step-12:Stop the process. STEP-13:select the folloing settings for the project in the AppWizard, i)choose single document. ii) select the default settings. iii)turn off all features,except printing and print preview. iv)then select again the default settings. Step-14: Expand the CPrintDoc in Class View.
101
VC++ programminng
Step-15: In the CPrintDoc Constructor,initialize m_numRects=5. Step-16: In the CPrintDoc Constructor,initialize m_numRects=5. Step-17: In the OnDraw() function,add the following codes to it pDC->rectangle(20,20,220,220). Step-18: Run the program
102
VC++ programminng
Pseudocode: //printing #include<afxwin.h> #include<afxdlgs.h> class mywin : public CFrameWnd { public: mywin() { Create(0,"PrintDlg",WS_OVERLAPPE DWINDOW,rectDefault,0,MAKEINTRES OURCE(10)); } void aa() { CPrintDialog p(0); if(p.DoModal() == 1) { CString s; s = p.GetDeviceName(); MessageBox(s,"device"); } }
103
VC++ programminng
DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(mywin,CFrame Wnd) ON_COMMAND(100,aa) END_MESSAGE_MAP() class myapp : public CWinApp { public: int InitInstance() { mywin *w; w = new mywin; w->ShowWindow(3); m_pMainWnd = w; return 1; } }; myapp o; .rc file: 10 MENU BEGIN MENUITEM "PRINT",100
104
VC++ programminng
END
Output:
105
VC++ programminng
106
VC++ programminng
Pseudocode: // print preview #include "stdafx.h" #include "refer.h" #include "referDoc.h" #include "referView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //////////////////////////////////////////////////////////////// ///////////// // CReferView IMPLEMENT_DYNCREATE(CReferView , CView) BEGIN_MESSAGE_MAP(CReferView, CView)
107
VC++ programminng
//{{AFX_MSG_MAP(CReferView) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DI RECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PR EVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() //////////////////////////////////////////////////////////////// ///////////// // CReferView construction/destruction CReferView::CReferView() { // TODO: add construction code here } CReferView::~CReferView()
108
VC++ programminng
{ } BOOL CReferView::PreCreateWindow(CREATES TRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } //////////////////////////////////////////////////////////////// ///////////// // CReferView drawing
109
VC++ programminng
//////////////////////////////////////////////////////////////// ///////////// // CReferView printing BOOL CReferView::OnPreparePrinting(CPrintInfo * pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CReferView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CReferView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing }
110
VC++ programminng
//////////////////////////////////////////////////////////////// ///////////// // CReferView diagnostics #ifdef _DEBUG void CReferView::AssertValid() const { CView::AssertValid(); } void CReferView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CReferDoc* CReferView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument>IsKindOf(RUNTIME_CLASS(CReferDoc ))); return (CReferDoc*)m_pDocument;
111
VC++ programminng
} #endif //_DEBUG
112
VC++ programminng
113
VC++ programminng
Output:
114
VC++ programminng
115
VC++ programminng
Test cases:
116