• Tidak ada hasil yang ditemukan

Special Edtion using Visual C++

N/A
N/A
Protected

Academic year: 2019

Membagikan "Special Edtion using Visual C++"

Copied!
719
0
0

Teks penuh

(1)

Special Edition Using Visual C++ 6

Table of Contents:

● In t r odu ct ion

Part I - Getting Started with Visual C++

● Ch a pt er 1 - Bu il din g You r Fir st Win dows Appl ica t ion ● Ch a pt er 2 - Dia l ogs a n d Con t r ol s

● Ch a pt er 3 - Messa ges a n d Comma n ds

Part II - Getting Information from Your Applications

● Ch a pt er 4 - Docu men t s a n d Views ● Ch a pt er 5 - Dr a win g on t h e Scr een ● Ch a pt er 6 - Pr in t in g a n d Pr in t Pr eview ● Ch a pt er 7 - Per sist en ce a n d Fil e I/O

● Ch a pt er 8 - Bu il din g a Compl et e Appl ica t ion : Sh owSt r in g

Part III - Improving Your User Interface

● Ch a pt er 9 - St a t u s Ba r s a n d Tool ba r s ● Ch a pt er 10 - Common Con t r ol s

● Ch a pt er 11 - Hel p

● Ch a pt er 12 - Pr oper t y Pa ges a n d Sh eet s

Part IV - ActiveX Applications and ActiveX Controls

● Ch a pt er 13 - Act iveX Con cept s

(2)

● Ch a pt er 15 - Bu il din g a n Act iveX Ser ver Appl ica t ion ● Ch a pt er 16 - Bu il din g a n Au t oma t ion Ser ver

● Ch a pt er 17 - Bu il din g a n Act iveX Con t r ol

Part V - Internet Programming

● Ch a pt er 18 - Socket s, MAPI, a n d t h e In t er n et

● Ch a pt er 19 - In t er n et Pr ogr a mmin g wit h t h e Win In et Cl a sses ● Ch a pt er 20 - Bu il din g a n In t er n et Act iveX Con t r ol

● Ch a pt er 21 - Th e Act ive Templ a t e Libr a r y

Part VI - Advanced Programming Techniques

● Ch a pt er 22 - Da t a ba se Access

● Ch a pt er 23 - SQL a n d t h e En t er pr ise Edit ion

● Ch a pt er 24 - Impr ovin g You r Appl ica t ion 's Per f or ma n ce

● Ch a pt er 25 - Ach ievin g Reu se wit h t h e Ga l l er y a n d You r Own AppWiz a r ds ● Ch a pt er 26 - Except ion s a n d Templ a t es

● Ch a pt er 27 - Mu l t it a skin g wit h Win dows Th r ea ds ● Ch a pt er 28 - Fu t u r e Expl or a t ion s

Part VII - Appendixes

● Appen dix A - C++ Review a n d Object -Or ien t ed Con cept s

● Appen dix B - Win dows Pr ogr a mmin g Review a n d a Look In side CWnd ● Appen dix C - Th e Devel oper St u dio User In t er f a ce, Men u s, a n d Tool ba r s ● Appen dix D - Debu ggin g

● Appen dix E - MFC Ma cr os a n d Gl oba l s ● Appen dix F - Usef u l Cl a sses

(3)

Introduction

● Abou t t h e Au t h or ● Dedica t ion

● Ackn owl edgmen t s

● Wh o Sh ou l d Rea d Th is Book? ● Bef or e You St a r t Rea din g ● Wh a t Th is Book Cover s

❍ Dia l ogs a n d Con t r ol s ❍ Messa ges a n d Comma n ds

❍ Th e View/Docu men t Pa r a digm ❍ Dr a win g On scr een

❍ Pr in t in g on Pa per

❍ Per sist en ce a n d Fil e I/O ❍ Act iveX Pr ogr a mmin g ❍ Th e In t er n et

❍ Da t a ba se Access ❍ Adva n ced Ma t er ia l ● Con ven t ion s Used in Th is Book ● Time t o Get St a r t ed

About the Author

Kat e Gr e g o r y is a f ou n din g pa r t n er of Gr egor y Con su l t in g Limit ed

(www.g r e g c o ns.c o m), wh ich h a s been pr ovidin g con su l t in g a n d devel opmen t ser vices t h r ou gh ou t Nor t h Amer ica sin ce 1986. Her exper ien ce wit h C++ st r et ch es ba ck t o bef or e Visu a l C++ exist ed - sh e en t h u sia st ica l l y con ver t ed u pon seein g t h e f ir st r el ea se.

(4)

qu a l it y cu st om a n d of f -t h e-sh el f sof t wa r e compon en t s f or Web pa ges a n d ot h er a ppl ica t ion s.

Dedication

To my children, Beth and Kevin, who keep me connected to the world away from the keyboard, and remind me every day how good it feels to learn new things.

Acknowledgments

Wr it n g a book is h a r d, h a r d wor k. Wh a t ma kes it possibl e is t h e su ppor t I get f r om t h ose a r ou n d me. Fir st , a s a l wa ys, my f a mil y, Br ia n , Bet h , a n d Kevin , wh o kn ow it 's on l y

t empor a r y. Br ia n does dou bl e du t y a s bot h su ppor t ive h u sba n d a n d wor l d's best

t ech n ica l edit or . Th is t ime a r ou n d I wa s l u cky en ou gh t o h a ve Br ya n Ol iver h el pin g, sh oot in g f igu r es, t est in g code, f in din g bu gs, a n d gen er a l l y pit ch in g in . Th a n ks, Br ya n .

Th er e is a n a r my of edit or s, pr oof er s, in dexer s, il l u st r a t or s, a n d gen er a l sa in t s wh o t u r n my Wor d docu men t s in t o t h e book you h ol d in you r h a n d. Ma n y of t h e t ea m member s t h is t ime h a ve been in vol ved in ot h er Qu e pr oject s wit h me, a n d I kn ow t h a t I l a n ded t h e "good on es" f or t h is book. Specia l men t ion h a s t o go t o Ol a f Medin g, wh o pr ovided a t er r if ic t ech edit ba sed on a f a st -ch a n gin g pr odu ct . Joe Ma sson i a n d Mike Bl a sz cz a k a t Micr osof t h a ve a l so ea r n ed my gr a t it u de du r in g t h is r el ea se cycl e.

Wh il e I ch eer f u l l y sh a r e t h e cr edit f or t h e a ccu r a t e a n d edu ca t ion a l a spect s of t h is book, t h e mist a kes a n d omission s I h a ve t o cl a im a s min e a l on e. Pl ea se br in g t h em t o my a t t en t ion so t h a t t h ey ca n be cor r ect ed in su bsequ en t pr in t in gs a n d edit ion s. I a m a s gr a t ef u l a s ever t o r ea der s wh o h a ve don e so in t h e pa st , a n d impr oved t h is book in t h e pr ocess.

Int r o duc t io n

Visu a l C++ is a power f u l a n d compl ex t ool f or bu il din g 32-bit a ppl ica t ion s f or Win dow 95 a n d Win dows NT. Th ese a ppl ica t ion s a r e mu ch l a r ger a n d mor e compl ex t h a n t h eir pr edecessor s f or 16-bit Win dows or ol der pr ogr a ms t h a t didn 't u se a gr a ph ica l u ser in t er f a ce. Yet , a s pr ogr a m siz e a n d compl exit y h a s in cr ea sed, pr ogr a mmer ef f or t h a s decr ea sed, a t l ea st f or pr ogr a mmer s wh o a r e u sin g t h e r igh t t ool s.

(5)

Who Should Read This Book?

Th is book t ea ch es you h ow t o u se Visu a l C++ t o bu il d 32-bit Win dows a ppl ica t ion s, in cl u din g da t a ba se a ppl ica t ion s, In t er n et a ppl ica t ion s, a n d a ppl ica t ion s t h a t t a p t h e power of t h e Act iveX t ech n ol ogy. Th a t 's a t a l l or der , a n d t o f it a l l t h a t in l ess t h a n a t h ou sa n d pa ges, some t h in gs h a ve t o go. Th is book does n ot t ea ch you t h e f ol l owin g:

● The C++ programming language: You sh ou l d a l r ea dy be f a mil ia r wit h C++. Appen dix

A, "C++ Review a n d Object -Or ien t ed Con cept s," is a r eview f or t h ose wh ose C++ skil l s n eed a boost .

● How to use Windows applications: You sh ou l d be a pr of icien t Win dows u ser , a bl e t o

r esiz e a n d move win dows, dou bl e-cl ick, a n d r ecogn iz e f a mil ia r t ool ba r bu t t on s, f or exa mpl e.

● How to use Visual C++ as a C compiler: If you a l r ea dy wor k in C, you ca n u se Visu a l

C++ a s you r compil er , bu t n ew devel oper s sh ou l d t a ke t h e pl u n ge in t o C++.

● Windows programming without MFC: Th is, t oo, is oka y f or t h ose wh o kn ow it , bu t n ot

somet h in g t o l ea r n n ow t h a t MFC exist s.

● The internals of ActiveX programming: Th is is r ef er r ed t o in t h e Act iveX ch a pt er s,

wh ich t el l you on l y wh a t you n eed t o kn ow t o ma ke it wor k.

You sh ou l d r ea d t h is book if you f it on e of t h ese ca t egor ies:

● You kn ow some C++ a n d some Win dows pr ogr a mmin g t ech n iqu esa n d a r e n ew t o

Visu a l C++. You wil l l ea r n t h e pr odu ct mu ch mor e qu ickl y t h a n you wou l d if you ju st t r ied wr it in g pr ogr a ms.

● You 've been wor kin g wit h pr eviou s ver sion s of Visu a l C++. Ma n y t imes u ser s l ea r n

on e wa y t o do t h in gs a n d en d u p over l ookin g some of t h e n ewer pr odu ct ivit y f ea t u r es.

● You 've been wor kin g wit h Visu a l C++ 6 f or a wh il e a n d a r e begin n in g t o su spect

you 'r e doin g t h in gs t h e h a r d wa y. Ma ybe you a r e.

● You wor k in Visu a l C++ 6 r egu l a r l y, a n d you n eed t o a dd a f ea t u r e t o you r

pr odu ct . For t a sks l ike Hel p, pr in t in g, a n d t h r ea din g, you 'l l f in d a "h a n d u p" t o get st a r t ed.

(6)

You n eed a copy of Visu a l C++ 6 a n d mu st h a ve it in st a l l ed. Th e in st a l l a t ion pr ocess is simpl e a n d ea sy t o f ol l ow, so it 's n ot cover ed in t h is book.

Bef or e you bu y Visu a l C++ 6, you n eed a 32-bit Win dows oper a t in g syst em: Win dows 95, Win dows 98, or Win dows NT Ser ver or Wor kst a t ion . Th a t mea n s you r ma ch in e mu st be r ea son a bl y power f u l a n d moder n - sa y, a 486 or bet t er f or you r pr ocessor , a t l ea st 16MB of RAM a n d 500MB of disk spa ce, a n d a scr een t h a t ca n do 800 * 600 pixel displ a ys or even f in er r esol u t ion s. Th e il l u st r a t ion s in t h is book wer e a l l pr epa r ed a t a r esol u t ion of 800 * 600 a n d, a s you wil l see, a t t imes t h in gs become a l it t l e cr owded. Th e sa mpl e code is a l l a va il a bl e on t h e Web, so f ol l owin g a l on g wil l be simpl er if you a l so h a ve a modem a n d a ccess t o t h e Web.

Fin a l l y, you n eed t o ma ke a pr omise t o you r sel f - t h a t you wil l f ol l ow a l on g in Visu a l C++ a s you r ea d t h is book, cl ickin g a n d t ypin g a n d t r yin g t h in gs ou t . You don 't n eed t o t ype a l l t h e code if you don 't wa n t t o: It 's a l l on t h e Web sit e f or you t o l ook a t .

However , you sh ou l d be r ea dy t o open t h e f il es a n d l ook a t t h e code a s you go.

What This Book Covers

A t opic su ch a s Win dows pr ogr a mmin g in Visu a l C++ cover s a l ot of gr ou n d. Th is book con t a in s 28 ch a pt er s a n d 6 r ef er en ce a ppen dixes (A t o F). Be su r e t o l ook over t h e t it l es of t h e a ppen dixes n ow a n d t u r n t o t h em wh en ever you a r e u n su r e h ow t o do somet h in g. Th ey pr ovide va l u a bl e r ef er en ces f or t h e f ol l owin g:

● Appen dix A, "C++ Review a n d Object -Or ien t ed Con cept s," r emin ds you of t h e ba sics

of t h e C++ l a n gu a ge a n d t h e pr in cipl es a n d ben ef it s of object -or ien t ed pr ogr a mmin g.

● Appen dix B, "Win dows Pr ogr a mmin g Review a n d a Look In side CWn d," cover s t h e

specif ics of Win dows pr ogr a mmin g t h a t a r e n ow h idden f r om you by MFC cl a sses su ch a s CWn d.

● Appen dix C, "Th e Visu a l St u dio User In t er f a ce, Men u s, a n d Tool ba r s," expl a in s a l l

t h e men u s, t ool ba r s, edit in g a r ea s on t h e scr een s, sh or t cu t s, a n d so on , t h a t ma ke u p t h e h igh l y compl ica t ed a n d r ich l y power f u l in t er f a ce bet ween you a n d Visu a l St u dio.

● Appen dix D, "Debu ggin g," expl a in s t h e ext r a men u s, win dows, t ool ba r s, a n d

comma n ds in vol ved in debu ggin g a r u n n in g a ppl ica t ion .

● Appen dix E, "MFC Ma cr os a n d Gl oba l s," su mma r iz es t h e ma n y pr epr ocessor ma cr os

(7)

● Appen dix F, "Usef u l Cl a sses," descr ibes t h e cl a sses u sed t h r ou gh ou t t h e book t o

ma n ipu l a t e da t es, st r in gs, a n d col l ect ion s of object s.

Depen din g on you r ba ckgr ou n d a n d wil l in gn ess t o poke a r ou n d in men u s a n d t h e on l in e h el p, you migh t ju st skim t h ese a ppen dixes on ce a n d n ever r et u r n , or you migh t f il l t h em f u l l of bookma r ks a n d yel l ow st ickies. Al t h ou gh t h ey don 't l ea d you t h r ou gh t h e

sa mpl e a ppl ica t ion s, t h ey wil l t ea ch you a l ot .

Th e ma in st r ea m of t h e book is in Ch a pt er s 1 t h r ou gh 28. Ea ch ch a pt er t ea ch es you a n impor t a n t pr ogr a mmin g t a sk or somet imes t wo cl osel y r el a t ed t a sks, su ch a s bu il din g a t a skba r or a ddin g Hel p t o a n a ppl ica t ion . Det a il ed in st r u ct ion s sh ow you h ow t o bu il d a wor kin g a ppl ica t ion , or sever a l wor kin g a ppl ica t ion s, in ea ch ch a pt er .

Th e f ir st n in e ch a pt er s cover con cept s f ou n d in a l most ever y Win dows a ppl ica t ion ; a f t er t h a t , t h e t a sks become l ess gen er a l . Her e's a br ief over view of some of t h e wor k t h a t is cover ed.

Dialogs and Controls

Wh a t Win dows pr ogr a m does n ot h a ve a dia l og box? a n edit box? a bu t t on ? Dia l og boxes a n d con t r ol s a r e vit a l t o Win dows u ser in t er f a ces, a n d a l l of t h em, even t h e simpl e bu t t on or piece of st a t ic t ext , a r e win dows. Th e common con t r ol s en a bl e you t o t a ke a dva n t a ge of t h e l ea r n in g t ime u ser s h a ve devot ed t o ot h er pr ogr a ms a n d t h e

pr ogr a mmin g t ime devel oper s h a ve pu t in on t h e oper a t in g syst em in or der t o u se t h e sa me Fil e Open dia l og box a s ever ybody el se, t h e sa me h ier a r ch ica l t r ee con t r ol , a n d so on . Lea r n mor e a bou t a l l t h ese con t r ol s in Ch a pt er s 2, "Dia l ogs a n d Con t r ol s," a n d 10, "Win dows 95 Common Con t r ol s."

Messages and Commands

Messa ges f or m t h e h ea r t of Win dows pr ogr a mmin g. Wh en ever a n yt h in g h a ppen s on a Win dows ma ch in e, su ch a s a u ser cl ickin g t h e mou se or pr essin g a key, a messa ge is t r igger ed a n d sen t t o on e or mor e win dows, wh ich do somet h in g a bou t it . Visu a l C++ ma kes it ea sy f or you t o wr it e code t h a t ca t ch es t h ese messa ges a n d a ct s on t h em.

Ch a pt er 3, "Messa ges a n d Comma n ds," expl a in s t h e con cept of messa ges a n d h ow MFC a n d ot h er a spect s of Visu a l C++ en a bl e you t o dea l wit h t h em.

The View/Document Paradigm

(8)

cl a sses f r om wh ich t o in h er it you r docu men t cl a ss a n d you r view cl a ss, so t h a t common pr ogr a mmin g t a sks su ch a s impl emen t in g scr ol l ba r s a r e n o l on ger you r pr obl em.

Drawing Onscreen

No ma t t er h ow sma r t you r Win dows pr ogr a m is, if you ca n 't t el l t h e u ser wh a t 's goin g on by pu t t in g some wor ds or pict u r es on scr een , n o on e wil l kn ow wh a t t h e pr ogr a m h a s don e. A r ema r ka bl e a mou n t of t h e wor k is a u t oma t ica l l y don e by you r view cl a sses (on e of t h e a dva n t a ges of a dopt in g t h e docu men t /view pa r a digm), bu t a t t imes you h a ve t o do t h e dr a win g you r sel f . You l ea r n a bou t device con t ext s, scr ol l in g, a n d mor e in Ch a pt er 5, "Dr a win g on t h e Scr een ."

Printing on Paper

Addin g pr in t in g ca pa bil it ies t o you r pr ogr a m is somet imes t h e simpl est t h in g in t h e wor l d beca u se t h e code you u se t o dr a w on scr een ca n be r eu sed t o dr a w on pa per . If mor e t h a n on e pa ge of in f or ma t ion is in vol ved, t h ou gh , t h in gs become t r icky. Ch a pt er 6, "Pr in t in g a n d Pr in t Pr eview," expl a in s a l l t h is, a s wel l a s ma ppin g modes, h ea der s a n d f oot er s, a n d mor e.

Persistence and File I/O

Some good t h in gs a r e mea n t t o be on l y t empor a r y, su ch a s t h e displ a y of a ca l cu l a t or or a n on l in e ch a t win dow. However , most pr ogr a ms ca n sa ve t h eir docu men t s t o a f il e a n d open a n d l oa d t h a t f il e t o r e-cr ea t e a docu men t t h a t h a s been st or ed. MFC simpl if ies t h is by u sin g a r ch ives a n d ext en din g t h e u se of t h e st r ea m I/O oper a t or s >> a n d <<. You l ea r n a l l a bou t r ea din g a n d wr it in g t o f il es in Ch a pt er 7, "Per sist en ce a n d Fil e I/O."

ActiveX Programming

Act iveX is t h e su ccessor t o OLE, a n d it 's t h e t ech n ol ogy t h a t f a cil it a t es commu n ica t ion bet ween a ppl ica t ion s a t t h e object l evel , en a bl in g you t o embed a Wor d docu men t in a n Excel spr ea dsh eet or t o embed a n y of h u n dr eds of kin ds of object s in a n y Act iveX

a ppl ica t ion . Act iveX ch a pt er s in cl u de Ch a pt er s 13, "Act iveX Con cept s," 14, "Bu il din g a n Act iveX Con t a in er Appl ica t ion ," 15, "Bu il din g a n Act iveX Ser ver Appl ica t ion ," 16,

"Bu il din g a n Au t oma t ion Ser ver ," a n d 17, "Bu il din g a n Act iveX Con t r ol ."

The Internet

(9)

book a r e Ch a pt er 18, "Socket s, MAPI, a n d t h e In t er n et ," Ch a pt er 19, "In t er n et

Pr ogr a mmin g wit h t h e Win In et Cl a sses," Ch a pt er 20, "Bu il din g a n In t er n et Act iveX Con t r ol ," a n d Ch a pt er 21, "Th e Act ive Templ a t e Libr a r y."

Database Access

Da t a ba se pr ogr a mmin g keeps get t in g ea sier . ODBC, Micr osof t 's Open Da t a Ba se Con n ect ivit y pa cka ge, en a bl es you r code t o ca l l API f u n ct ion s t h a t a ccess a h u ge va r iet y of da t a ba se f il es - Or a cl e, DBa se, a n Excel spr ea dsh eet , a pl a in t ext f il e, ol d l ega cy ma in f r a me syst ems u sin g SQL, wh a t ever ! You ca l l a st a n da r d n a me f u n ct ion , a n d t h e API pr ovided by t h e da t a ba se ven dor or a t h ir d pa r t y h a n dl es t h e t r a n sl a t ion . Th e det a il s a r e in Ch a pt er s 22, "Da t a ba se Access," a n d 23, "SQL a n d t h e En t er pr ise Edit ion ."

Advanced Material

For devel oper s wh o h a ve ma st er ed t h e ba sics, t h is book f ea t u r es some a dva n ced ch a pt er s t o move you r pr ogr a mmin g skil l s f or wa r d. You wil l l ea r n h ow t o pr even t memor y l ea ks, f in d bot t l en ecks, a n d l oca t e bu gs in you r code wit h t h e t ech n iqu es discu ssed in Ch a pt er 24, "Impr ovin g You r Appl ica t ion 's Per f or ma n ce."

Reu se is a h u gel y popu l a r con cept in sof t wa r e devel opmen t a t t h e momen t , especia l l y wit h ma n a ger s wh o see a ch a n ce t o l ower t h eir devel opmen t bu dget . If you 'd l ike t o wr it e r eu sa bl e code a n d compon en t s, Ch a pt er 25, "Ach ievin g Reu se wit h t h e Ga l l er y a n d You r Own AppWiz a r ds," wil l t a ke you t h er e.

Of t en C++ pr ogr a mmer s a r e so bu sy l ea r n in g t h e ba sics of h ow t o ma ke pr ogr a ms wor k t h a t t h ey miss t h e f ea t u r es t h a t ma ke C++ t r u l y power f u l . You wil l l ea r n in Ch a pt er 26, "Except ion s a n d Templ a t es," h ow t o ca t ch er r or s ef f icien t l y a n d h ow t o u se on e set of code in ma n y dif f er en t sit u a t ion s.

As u ser dema n ds f or h igh -per f or ma n ce sof t wa r e con t in u e t o mu l t ipl y, devel oper s mu st l ea r n en t ir el y n ew t ech n iqu es t o pr odu ce power f u l a ppl ica t ion s t h a t pr ovide f a st r espon se t imes. For ma n y devel oper s, wr it in g mu l t it h r ea ded a ppl ica t ion s is a vit a l

t ech n iqu e. Lea r n a bou t t h r ea din g in Ch a pt er 27, "Mu l t it a skin g wit h Win dows Th r ea ds."

Ch a pt er 28, "Fu t u r e Expl or a t ion s," in t r odu ces you t o t opics t h a t a r e def in it el y n ot f or begin n er s. Lea r n h ow t o cr ea t e con sol e a ppl ica t ion s, u se a n d bu il d you r own DLLs, a n d wor k wit h Un icode.

Conventions Used in This Book

(10)

int SomeFunction( int x, int y); {

return x+y; }

You ca n t el l t h e dif f er en ce bet ween code a n d r egu l a r t ext by t h e f on t s u sed f or ea ch . Somet imes, you 'l l see a piece of code t h a t 's t oo l a r ge t o mix in wit h t h e t ext : You wil l f in d a n exa mpl e in List in g 0.1.

Listing 0.1

CHostDialog dialog(m_pMainWnd); if (dialog.DoModal() == IDOK) {

AppSocket = new CSocket();

if (AppSocket->Connect(dialog.m_hostname,119)) {

while (AppSocket->GetStatus() == CONNECTING) {

YieldControl(); }

if (AppSocket->GetStatus() == CONNECTED) {

CString response = AppSocket->GetLine(); SocketAvailable = TRUE;

} }

}

if (!SocketAvailable) {

AfxMessageBox("Can't connect to server. Please ¬ quit.",MB_OK|MB_ICONSTOP);

}

Th e ch a r a ct er on t h e n ext -t o-l a st l in e (¬) is ca l l ed t h e code continuation character. It in dica t es a pl a ce wh er e a l in e of code h a d t o be br oken t o f it it on t h e pa ge, bu t in r ea l it y t h e l in e does n ot br ea k t h er e. If you 'r e t ypin g code f r om t h e book, don 't br ea k t h e l in e t h er e - keep goin g. If you 'r e r ea din g a l on g in code t h a t wa s gen er a t ed f or you by Visu a l C++, don 't be con f u sed wh en t h e l in e does n ot br ea k t h er e.

Remember , t h e code is in t h e book so t h a t you ca n u n der st a n d wh a t 's goin g on , n ot f or you t o t ype it . Al l t h e code is on t h e compa n ion Web sit e a s wel l . Somet imes you wil l wor k you r wa y t h r ou gh t h e devel opmen t of a n a ppl ica t ion a n d see sever a l ver sion s of a bl ock of code a s you go - t h e f in a l ver sion is on t h e Web sit e. You 'l l f in d t h e sit e by goin g t o www.mc p.c o m/inf o or www.g r e g c o ns.c o m/uvc 6.ht m.

(11)

kn ow a bou t .

NOTE: Th is is a Not e: It expl a in s a su bt l e bu t impor t a n t poin t . Don 't skip Not es, even if you 'r e t h e kin d wh o skips Tips. n

CAUTION: Th is is a Ca u t ion , a n d it 's ser iou s. It wa r n s you of t h e h or r ibl e con sequ en ces if you ma ke a f a l se st ep, so be su r e t o r ea d a l l of t h ese t h a t you come a cr oss.

Wh en a wor d is bein g def in ed or emph a siz ed, it 's in italic. Th e n a mes of va r ia bl es,

f u n ct ion s, a n d C++ cl a sses a r e a l l in mon ospa ced f on t . In t er n et URLS a n d t h in gs you sh ou l d t ype a r e in bo l d. Remember , a n URL n ever en ds wit h pu n ct u a t ion , so ign or e a n y comma or per iod a f t er t h e URL.

Time to Get Started

Th a t a bou t wr a ps u p t h in gs f or t h e in t r odu ct ion . You 've l ea r n ed wh a t you n eed t o get st a r t ed, in cl u din g some a dva n ced wa r n in g a bou t t h e n ot a t ion s u sed t h r ou gh ou t t h e book. Ju mp r igh t in , l ea r n a l l a bou t wr it in g Win dows a ppl ica t ion s wit h MFC, a n d t h en get st a r t ed on some devel opmen t of you r own ! Good l u ck a n d h a ve f u n .

(12)

Special Edition Using Visual C++ 6

1

-Building Your First Windows Application

● Cr ea t in g a Win dows Appl ica t ion

❍ Decidin g How Ma n y Docu men t s t h e Appl ica t ion Su ppor t s ❍ Da t a ba ses

❍ Compou n d Docu men t Su ppor t ❍ Appea r a n ce a n d Ot h er Opt ion s ❍ Ot h er Opt ion s

❍ Fil en a mes a n d Cl a ssn a mes ❍ Cr ea t in g t h e Appl ica t ion ❍ Tr y It You r sel f

● Cr ea t in g a Dia l og-Ba sed Appl ica t ion

● Cr ea t in g DLLs, Con sol e Appl ica t ion s, a n d Mor e ❍ ATL COM AppWiz a r d

❍ Cu st om AppWiz a r d ❍ Da t a ba se Pr oject

❍ DevSt u dio Add-In Wiz a r d ❍ ISAPI Ext en sion Wiz a r d ❍ Ma kef il e

❍ MFC Act iveX Con t r ol Wiz a r d ❍ MFC AppWiz a r d (DLL)

❍ Win 32 Appl ica t ion

❍ Win 32 Con sol e Appl ica t ion ❍ Win 32 Dyn a mic Lin k Libr a r y ❍ Win 32 St a t ic Libr a r y

(13)

● Un der st a n din g AppWiz a r d's Code

❍ A Sin gl e Docu men t In t er f a ce Appl ica t ion ❍ Ot h er Fil es

● Un der st a n din g a Mu l t ipl e Docu men t In t er f a ce Appl ica t ion ● Un der st a n din g t h e Compon en t s of a Dia l og-Ba sed Appl ica t ion ● Reviewin g AppWiz a r d Decision s a n d Th is Ch a pt er

Creating a Windows Application

Visu a l C++ does n ot ju st compil e code; it gen er a t es code. You ca n cr ea t e a Win dows a ppl ica t ion in min u t es wit h a t ool ca l l ed AppWiz a r d. In t h is ch a pt er you 'l l l ea r n h ow t o t el l AppWiz a r d t o ma ke you a st a r t er a pp wit h a l l t h e Win dows boil er pl a t e code you wa n t . AppWiz a r d is a ver y ef f ect ive t ool . It copies in t o you r a ppl ica t ion t h e code t h a t a l most a l l Win dows a ppl ica t ion s r equ ir e. Af t er a l l , you a r en 't t h e f ir st pr ogr a mmer t o n eed a n a ppl ica t ion wit h r esiz a bl e edges, min imiz e a n d ma ximiz e bu t t on s, a Fil e men u wit h Open , Cl ose, Pr in t Set u p, Pr in t , a n d Exit opt ion s, a r e you ?

AppWiz a r d ca n ma ke ma n y kin ds of a ppl ica t ion s, bu t wh a t most peopl e wa n t , a t l ea st a t f ir st , is a n execu t a bl e (.exe) pr ogr a m. Most peopl e a l so wa n t AppWiz a r d t o pr odu ce boil er pl a t e code - t h e cl a sses, object s, a n d f u n ct ion s t h a t h a ve t o be in ever y pr ogr a m. To cr ea t e a pr ogr a m l ike t h is, Ch oose Fil e, New a n d cl ick t h e Pr oject s t a b in t h e New dia l og box, a s sh own in Figu r e 1.1.

FIG. 1.1The Projects tab of the New dialog box is where you choose the kind of application you want to build.

Ch oose MFC AppWiz a r d (EXE) f r om t h e l ist box on t h e l ef t , f il l in a pr oject n a me, a n d cl ick OK. AppWiz a r d wil l wor k t h r ou gh a n u mber of st eps. At ea ch st ep, you ma ke a decision a bou t wh a t kin d of a ppl ica t ion you wa n t a n d t h en cl ick Next . At a n y t ime, you ca n cl ick Ba ck t o r et u r n t o a pr eviou s decision , Ca n cel t o a ba n don t h e wh ol e pr ocess, Hel p f or mor e det a il s, or Fin ish t o skip t o t h e en d a n d cr ea t e t h e a ppl ica t ion wit h ou t a n swer in g a n y mor e qu est ion s (n ot r ecommen ded bef or e t h e l a st st ep). Th e f ol l owin g sect ion s expl a in ea ch st ep.

NOTE: An MFC a ppl ica t ion u ses MFC, t h e Micr osof t Fou n da t ion Cl a sses. You wil l l ea r n mor e a bou t MFC t h r ou gh ou t t h is book.

(14)

Th e f ir st decision t o commu n ica t e t o AppWiz a r d, a s sh own in Figu r e 1.2, is wh et h er you r a ppl ica t ion sh ou l d be MDI, SDI, or dia l og ba sed. AppWiz a r d gen er a t es dif f er en t code a n d cl a sses f or ea ch of t h ese a ppl ica t ion t ypes.

FIG. 1.2The first step in building a typical application with AppWizard is choosing the interface.

Th e t h r ee a ppl ica t ion t ypes t o ch oose f r om a r e a s f ol l ows:

● A single document interface (SDI) a ppl ica t ion , su ch a s Not epa d, h a s on l y on e

docu men t open a t a t ime. Wh en you ch oose Fil e, Open , t h e cu r r en t l y open f il e is cl osed bef or e t h e n ew on e is open ed.

● A multiple document interface (MDI) a ppl ica t ion , su ch a s Excel or Wor d, ca n open

ma n y docu men t s (t ypica l l y f il es) a t on ce. Th er e is a Win dow men u a n d a Cl ose it em on t h e Fil e men u . It 's a qu ir k of MFC t h a t if you l ike mu l t ipl e views on a sin gl e docu men t , you mu st bu il d a n MDI a ppl ica t ion .

● A dialog-based a ppl ica t ion , su ch a s t h e Ch a r a ct er Ma p u t il it y t h a t comes wit h

Win dows a n d is sh own in Figu r e 1.3, does n ot h a ve a docu men t a t a l l . Th er e a r e n o men u s. (If you 'd l ike t o see Ch a r a ct er Ma p in a ct ion , it 's u su a l l y in t h e Accessor ies f ol der , r ea ch ed by cl ickin g St a r t . You ma y n eed t o in st a l l it by u sin g Add/Remove pr ogr a ms u n der Con t r ol Pa n el .)

FIG. 1.3Character Map is a dialog-based application.

As you ch a n ge t h e r a dio bu t t on sel ect ion , t h e pict u r e on t h e l ef t of t h e scr een ch a n ges t o demon st r a t e h ow t h e a ppl ica t ion a ppea r s if you ch oose t h is t ype of a ppl ica t ion .

NOTE:: Dia l og-ba sed a ppl ica t ion s a r e qu it e dif f er en t f r om MDI or SDI a ppl ica t ion s. Th e AppWiz a r d dia l ogs a r e dif f er en t wh en you 'r e cr ea t in g a dia l og-ba sed a ppl ica t ion . Th ey a r e pr esen t ed l a t er in t h e sect ion "Cr ea t in g a Dia l og-Ba sed Appl ica t ion ."

Ben ea t h t h ese ch oices is a ch eckbox f or you t o in dica t e wh et h er you wa n t su ppor t f or t h e Docu men t /View a r ch it ect u r e. Th is f r a mewor k f or you r a ppl ica t ion s is expl a in ed in Ch a pt er 4, "Docu men t s a n d Views." Exper ien ced Visu a l C++ devel oper s, especia l l y t h ose wh o a r e por t in g a n a ppl ica t ion f r om a n ot h er devel opmen t syst em, migh t ch oose t o t u r n of f t h is su ppor t . You sh ou l d l ea ve t h e opt ion sel ect ed.

(15)

en cou n t er u n expect ed beh a vior f r om Cl a ssWiz a r d l a t er . (Of cou r se, if you r a ppl ica t ion is f or u ser s wh o wil l h a ve t h eir l a n gu a ge set t o U.S. En gl ish , you migh t n ot h a ve a ch oice. In t h a t ca se, ch a n ge you r syst em l a n gu a ge u n der Con t r ol Pa n el .) Cl ick Next a f t er you ma ke you r ch oices.

Databases

Th e secon d st ep in cr ea t in g a n execu t a bl e Win dows pr ogr a m wit h AppWiz a r d is t o ch oose t h e l evel of da t a ba se su ppor t , a s sh own in Figu r e 1.4.

FIG. 1.4The second step to building a typical application with AppWizard is to set the database options you will use.

Th er e a r e f ou r ch oices f or da t a ba se su ppor t :

● If you a r en 't wr it in g a da t a ba se a ppl ica t ion , ch oose Non e.

● If you wa n t t o h a ve a ccess t o a da t a ba se bu t don 't wa n t t o der ive you r view f r om

CFor mView or h a ve a Recor d men u , ch oose Hea der Fil es On l y.

● If you wa n t t o der ive you r view f r om CFor mView a n d h a ve a Recor d men u bu t

don 't n eed t o ser ia l iz e a docu men t , ch oose Da t a ba se View Wit h ou t Fil e Su ppor t . You ca n u pda t e da t a ba se r ecor ds wit h CRecor dset , a n MFC cl a ss discu ssed in mor e det a il in Ch a pt er 22, "Da t a ba se Access."

● If you wa n t t o su ppor t da t a ba ses a s in t h e pr eviou s opt ion bu t a l so n eed t o sa ve a

docu men t on disk (per h a ps some u ser opt ion s), ch oose Da t a ba se View Wit h Fil e Su ppor t .

Ch a pt er 22 cl a r if ies t h ese ch oices a n d demon st r a t es da t a ba se pr ogr a mmin g wit h MFC. If you ch oose t o h a ve a da t a ba se view, you mu st specif y a da t a sou r ce n ow. Cl ick t h e Da t a Sou r ce bu t t on t o set t h is u p.

As you sel ect dif f er en t r a dio bu t t on s, t h e pict u r e on t h e l ef t ch a n ges t o sh ow you t h e r esu l t s of you r ch oice. Cl ick Next t o move t o t h e n ext st ep.

Compound Document Support

(16)

FIG. 1.5The third step of building a typical application with AppWizard is to set the compound document support you will need.

Th er e a r e f ive ch oices f or compou n d docu men t su ppor t :

● If you a r e n ot wr it in g a n Act iveX a ppl ica t ion , ch oose Non e.

● If you wa n t you r a ppl ica t ion t o con t a in embedded or l in ked Act iveX object s, su ch

a s Wor d docu men t s or Excel wor ksh eet s, ch oose Con t a in er . You l ea r n t o bu il d a n Act iveX con t a in er in Ch a pt er 14, "Bu il din g a n Act iveX Con t a in er Appl ica t ion ."

● If you wa n t you r a ppl ica t ion t o ser ve object s t h a t ca n be embedded in ot h er

a ppl ica t ion s, bu t it n ever n eeds t o r u n a s a st a n da l on e a ppl ica t ion , ch oose Min i Ser ver .

● If you r a ppl ica t ion ser ves docu men t s a n d a l so f u n ct ion s a s a st a n da l on e

a ppl ica t ion , ch oose Fu l l Ser ver . In Ch a pt er 15, "Bu il din g a n Act iveX Ser ver Appl ica t ion ," you l ea r n t o bu il d a n Act iveX f u l l ser ver .

● If you wa n t you r a ppl ica t ion t o h a ve t h e ca pa bil it y t o con t a in object s f r om ot h er

a ppl ica t ion s a n d a l so t o ser ve it s object s t o ot h er a ppl ica t ion s, ch oose Bot h Con t a in er a n d Ser ver .

If you ch oose t o su ppor t compou n d docu men t s, you ca n a l so su ppor t compound files. Compou n d f il es con t a in on e or mor e Act iveX object s a n d a r e sa ved in a specia l wa y so t h a t on e of t h e object s ca n be ch a n ged wit h ou t r ewr it in g t h e wh ol e f il e. Th is spa r es you a gr ea t dea l of t ime. Use t h e r a dio bu t t on s in t h e middl e of t h is St ep 3 dia l og box t o sa y Yes, Pl ea se, or No, Th a n k You t o compou n d f il es.

If you wa n t you r a ppl ica t ion t o su r r en der con t r ol t o ot h er a ppl ica t ion s t h r ou gh

a u t oma t ion , ch eck t h e Au t oma t ion ch eck box. (Au t oma t ion is t h e su bject of Ch a pt er 16, "Bu il din g a n Au t oma t ion Ser ver .") If you wa n t you r a ppl ica t ion t o u se Act iveX

con t r ol s, sel ect t h e Act iveX Con t r ol s ch eck box. Cl ick Next t o move t o t h e n ext st ep.

NOTE: If you wa n t you r a ppl ica t ion t o be a n Act iveX con t r ol , you don 't cr ea t e a t ypica l .exe a ppl ica t ion a s descr ibed in t h is sect ion . Cr ea t in g

Act iveX con t r ol s wit h t h e Act iveX Con t r ol Wiz a r d is cover ed in Ch a pt er 17, "Bu il din g a n Act iveX Con t r ol ."

(17)

Th e f ou r t h st ep in r u n n in g AppWiz a r d t o cr ea t e a n execu t a bl e Win dows pr ogr a m (see Figu r e 1.6) is t o det er min e some of t h e in t er f a ce a ppea r a n ce opt ion s f or you r a ppl ica t ion . Th is St ep 4 dia l og box con t a in s a n u mber of in depen den t ch eck boxes. Ch eck t h em if you wa n t a f ea t u r e; l ea ve t h em u n ch ecked if you don 't .

FIG. 1.6The fourth step of building a typical application with AppWizard is to set some interface options.

Th e f ol l owin g a r e t h e opt ion s t h a t a f f ect you r in t er f a ce's a ppea r a n ce:

● Docking Toolbar. AppWiz a r d set s u p a t ool ba r f or you . You ca n edit it t o r emove

u n wa n t ed bu t t on s or t o a dd n ew on es l in ked t o you r own men u it ems. Th is is descr ibed in Ch a pt er 9, "St a t u s Ba r s a n d Tool ba r s."

● Initial Status Bar. AppWiz a r d cr ea t es a st a t u s ba r t o displ a y men u pr ompt s a n d ot h er

messa ges. La t er , you ca n wr it e code t o a dd in dica t or s a n d ot h er el emen t s t o t h is ba r , a s descr ibed in Ch a pt er 9.

● Printing and Print Preview. You r a ppl ica t ion wil l h a ve Pr in t a n d Pr in t Pr eview

opt ion s on t h e Fil e men u , a n d mu ch of t h e code you n eed in or der t o impl emen t pr in t in g wil l be gen er a t ed by AppWiz a r d. Ch a pt er 6, "Pr in t in g a n d Pr in t Pr eview," discu sses t h e r est .

● Context-Sensitive Help. You r Hel p men u wil l ga in In dex a n d Usin g Hel p opt ion s, a n d

some of t h e code n eeded t o impl emen t Hel p wil l be pr ovided by AppWiz a r d. Th is decision is h a r d t o ch a n ge l a t er beca u se qu it e a l ot of code is a dded in dif f er en t pl a ces wh en impl emen t in g Con t ext -Sen sit ive Hel p. Ch a pt er 11, "Hel p," descr ibes Hel p impl emen t a t ion .

● 3D Controls. You r a ppl ica t ion wil l l ook l ike a t ypica l Win dows 95 a ppl ica t ion . If

you don 't sel ect t h is opt ion , you r dia l og boxes wil l h a ve a wh it e ba ckgr ou n d, a n d t h er e wil l be n o sh a dows a r ou n d t h e edges of edit boxes, ch eck boxes, a n d ot h er con t r ol s.

● MAPI(Messaging API). You r a ppl ica t ion wil l be a bl e t o u se t h e Messa gin g API t o

sen d f a x, ema il , or ot h er messa ges. Ch a pt er 18, "Socket s, MAPI, a n d t h e In t er n et ," discu sses t h e Messa gin g API.

● WindowsSockets. You r a ppl ica t ion ca n a ccess t h e In t er n et dir ect l y, u sin g pr ot ocol s

l ike FTP a n d HTTP (t h e Wor l d Wide Web pr ot ocol ). Ch a pt er 18 discu sses socket s. You ca n pr odu ce In t er n et pr ogr a ms wit h ou t en a bl in g socket su ppor t if you u se t h e n ew Win In et cl a sses, discu ssed in Ch a pt er 19, "In t er n et Pr ogr a mmin g wit h t h e Win In et Cl a sses."

(18)

Wor d or Visu a l C++ it sel f , or wit h t ool ba r s l ike t h ose in In t er n et Expl or er . You ca n r ea d mor e a bou t t h is in Ch a pt er 9.

You ca n a l so set h ow ma n y f il es you wa n t t o a ppea r on t h e r ecen t f il e l ist f or t h is a ppl ica t ion . Fou r is t h e st a n da r d n u mber ; ch a n ge it on l y if you h a ve good r ea son t o do so.

Cl ickin g t h e Adva n ced bu t t on a t t h e bot t om of t h is St ep 4 dia l og box br in gs u p t h e Adva n ced Opt ion s dia l og box, wh ich h a s t wo t a bs. Th e Docu men t Templ a t e St r in gs t a b is sh own in Figu r e 1.7. AppWiz a r d bu il ds ma n y n a mes a n d pr ompt s f r om t h e n a me of you r a ppl ica t ion , a n d somet imes it n eeds t o a bbr evia t e you r a ppl ica t ion n a me. Un t il you a r e f a mil ia r wit h t h e n a mes AppWiz a r d bu il ds, you sh ou l d ch eck t h em on t h is Docu men t Templ a t e St r in gs dia l og box a n d a dju st t h em, if n ecessa r y. You ca n a l so ch a n ge t h e

ma in f r a me ca pt ion , wh ich a ppea r s in t h e t it l e ba r of you r a ppl ica t ion . Th e f il e ext en sion , if you ch oose on e, wil l be in cor por a t ed in t o f il en a mes sa ved by you r a ppl ica t ion a n d wil l r est r ict t h e f il es in it ia l l y displ a yed wh en t h e u ser ch ooses Fil e, Open .

Th e Win dow St yl es t a b is sh own in Figu r e 1.8. Her e you ca n ch a n ge t h e a ppea r a n ce of you r a ppl ica t ion qu it e dr a ma t ica l l y. Th e f ir st ch eck box, Use Spl it Win dow, a dds a l l t h e code n eeded t o impl emen t spl it t er win dows l ike t h ose in t h e code edit or of Devel oper St u dio. Th e r ema in der of t h e Win dow St yl es dia l og box set s t h e a ppea r a n ce of you r main frame a n d, f or a n MDI a ppl ica t ion , of you r MDI child frames. Fr a mes h ol d win dows; t h e syst em men u , t it l e ba r , min imiz e a n d ma ximiz e boxes, a n d win dow edges a r e a l l f r a me pr oper t ies. Th e ma in f r a me h ol ds you r en t ir e a ppl ica t ion . An MDI a ppl ica t ion h a s a n u mber of MDI ch il d f r a mes - on e f or ea ch docu men t win dow, in side t h e ma in f r a me.

FIG. 1.7The Document Template Strings tab of the Advanced Options dialog box lets you adjust the way names are abbreviated.

FIG. 1.8The Window Styles tab of the Advanced Options dialog box lets you adjust the appearance of your windows.

Her e a r e t h e pr oper t ies you ca n set f or f r a mes:

● Thick Frame. Th e f r a me h a s a visibl y t h ick edge a n d ca n be r esiz ed in t h e u su a l

Win dows wa y. Un ch eck t h is t o pr even t r esiz in g.

● Minimize Box. Th e f r a me h a s a min imiz e box in t h e t op-r igh t cor n er .

● Maximize Box. Th e f r a me h a s a ma ximiz e box in t h e t op-r igh t cor n er .

● System Menu. Th e f r a me h a s a syst em men u in t h e t op-l ef t cor n er .

(19)

t h is opt ion wil l be ign or ed wh en t h e a ppl ica t ion is r u n n in g u n der Win dows 95.

● Maximized. Th e f r a me is ma ximiz ed wh en t h e a ppl ica t ion st a r t s. For SDI

a ppl ica t ion s, t h is opt ion wil l be ign or ed wh en t h e a ppl ica t ion is r u n n in g u n der Win dows 95.

Wh en you h a ve ma de you r sel ect ion s, cl ick Cl ose t o r et u r n t o st ep 4 a n d cl ick Next t o move on t o t h e n ext st ep.

Other Options

Th e f if t h st ep in r u n n in g AppWiz a r d t o cr ea t e a n execu t a bl e Win dows pr ogr a m (see Figu r e 1.9) a sks t h e l ef t over qu est ion s t h a t a r e u n r el a t ed t o men u s, OLE, da t a ba se a ccess, or a ppea r a n ce. Do you wa n t commen t s in ser t ed in you r code? You cer t a in l y do. Th a t on e is ea sy.

FIG. 1.9The fifth step of building an application with AppWizard is to decide on comments and the MFC library.

Th e n ext qu est ion isn 't a s st r a igh t f or wa r d. Do you wa n t t h e MFC l ibr a r y a s a sh a r ed DLL or st a t ica l l y l in ked? A DLL (dyn a mic l in k l ibr a r y) is a col l ect ion of f u n ct ion s u sed by ma n y dif f er en t a ppl ica t ion s. Usin g a DLL ma kes you r pr ogr a ms sma l l er bu t ma kes t h e in st a l l a t ion a l it t l e mor e compl ex. Ha ve you ever moved a n execu t a bl e t o a n ot h er dir ect or y, or a n ot h er compu t er , on l y t o f in d it won 't r u n a n ymor e beca u se it 's missin g DLLs? If you st a t ica l l y l in k t h e MFC l ibr a r y in t o you r a ppl ica t ion , it is l a r ger , bu t it is ea sier t o move a n d copy a r ou n d.

If you r u ser s a r e l ikel y t o be devel oper s t h emsel ves a n d own a t l ea st on e ot h er

a ppl ica t ion t h a t u ses t h e MFC DLL or a r en 't in t imida t ed by t h e n eed t o in st a l l DLLs a s wel l a s t h e pr ogr a m it sel f , ch oose t h e sh a r ed DLL opt ion . Th e sma l l er execu t a bl e is con ven ien t f or a l l . If you r u ser s a r e n ot devel oper s, ch oose t h e st a t ica l l y l in ked

opt ion . It r edu ces t h e t ech n ica l su ppor t issu es you h a ve t o f a ce wit h in exper ien ced u ser s. If you wr it e a good in st a l l pr ogr a m, you ca n f eel mor e con f iden t a bou t u sin g sh a r ed DLLs.

Af t er you 've ma de you r St ep 5 ch oices, cl ick Next t o move t o St ep 6.

Filenames and Classnames

(20)

def a u l t is CView, bu t ma n y devel oper s pr ef er t o u se a n ot h er view, su ch a s CScr ol l View or CEdit View. Th e view cl a sses a r e discu ssed in Ch a pt er 4. Cl ick Fin ish wh en t h is St ep 6 dia l og box is compl et e.

TIP: Object s, cl a sses, a n d in h er it a n ce a r e r eviewed in Appen dix A, "C++ Review a n d Object -Or ien t ed Con cept s."

FIG. 1.10The final step of building a typical application with AppWizard is to confirm filenames and classnames.

Creating the Application

Af t er you cl ick Fin ish , AppWiz a r d sh ows you wh a t is goin g t o be cr ea t ed in a dia l og box, simil a r t o Figu r e 1.11. If a n yt h in g h er e is wr on g, cl ick Ca n cel a n d wor k you r wa y ba ck t h r ou gh AppWiz a r d wit h t h e Ba ck bu t t on s u n t il you r ea ch t h e dia l og box you n eed t o ch a n ge. Move f or wa r d wit h Next , Fin ish ; r eview t h is dia l og box a ga in ; a n d cl ick OK t o a ct u a l l y cr ea t e t h e a ppl ica t ion . Th is t a kes a f ew min u t es, wh ich is h a r dl y su r pr isin g beca u se h u n dr eds of code l in es, men u s, dia l og boxes, h el p t ext , a n d bit ma ps a r e bein g gen er a t ed f or you in a s ma n y a s 20 f il es. Let it wor k.

FIG. 1.11When AppWizard is ready to build your application, you get one more chance to confirm everything.

Try It Yourself

If you h a ven 't st a r t ed Devel oper St u dio a l r ea dy, do so n ow. If you 've n ever u sed it bef or e, you ma y f in d t h e in t er f a ce in t imida t in g. Th er e is a f u l l expl a n a t ion of a l l t h e a r ea s, t ool ba r s, men u s, a n d sh or t cu t s in Appen dix C, "Th e Visu a l St u dio User In t er f a ce, Men u s, a n d Tool ba r s."

Br in g u p AppWiz a r d by ch oosin g Fil e, New a n d cl ickin g t h e Pr oject s t a b. On t h e Pr oject s t a b, f il l in a f ol der n a me wh er e you wou l d l ike t o keep you r a ppl ica t ion s; AppWiz a r d wil l ma ke a n ew f ol der f or ea ch pr oject . Fil l in Fir st SDI f or t h e pr oject n a me; t h en move t h r ou gh t h e six AppWiz a r d st eps. Ch oose a n SDI a ppl ica t ion a t St ep 1, a n d on a l l t h e ot h er st eps simpl y l ea ve t h e sel ect ion s a s t h ey a r e a n d cl ick Next . Wh en AppWiz a r d h a s cr ea t ed t h e pr oject , ch oose Bu il d, Bu il d f r om t h e Devel oper St u dio men u t o compil e a n d l in k t h e code.

(21)

FIG. 1.12Your first application looks like any full-fledged Windows application.

Tr y ou t t h e Fil e men u by ch oosin g Fil e, Open ; br in g u p t h e f a mil ia r Win dows Fil e Open dia l og (t h ou gh n o ma t t er wh a t f il e you ch oose, n ot h in g seems t o h a ppen ); a n d t h en ch oose Fil e, Exit t o cl ose t h e a ppl ica t ion . Execu t e t h e pr ogr a m a ga in t o con t in u e expl or in g t h e ca pa bil it ies t h a t h a ve been a u t oma t ica l l y gen er a t ed f or you . Move t h e mou se cu r sor over on e of t h e t ool ba r bu t t on s a n d pa u se; a Tool Tip wil l a ppea r ,

r emin din g you of t h e t ool ba r bu t t on 's pu r pose. Cl ick t h e Open bu t t on t o con f ir m t h a t it is con n ect ed t o t h e Fil e Open comma n d you ch ose ea r l ier . Open t h e View men u a n d cl ick Tool ba r t o h ide t h e t ool ba r ; t h en ch oose View Tool ba r a ga in t o r est or e it . Do t h e sa me t h in g wit h t h e st a t u s ba r . Ch oose Hel p, Abou t , a n d you 'l l see it even h a s a n Abou t box wit h it s own n a me a n d t h e cu r r en t yea r in t h e copyr igh t da t e (see Figu r e 1.13).

Repea t t h ese st eps t o cr ea t e a n MDI a ppl ica t ion ca l l ed FirstMDI. Th e cr ea t ion pr ocess wil l dif f er on l y on St ep 0, wh er e you specif y t h e pr oject n a me, a n d St ep 1, wh er e you ch oose a n MDI a ppl ica t ion . Accept t h e def a u l t s on a l l t h e ot h er st eps, cr ea t e t h e a ppl ica t ion , bu il d it , a n d execu t e it . You 'l l see somet h in g simil a r t o Figu r e 1.14, a n MDI a ppl ica t ion wit h a sin gl e docu men t open . Tr y ou t t h e sa me oper a t ion s you t r ied wit h Fir st SDI.

FIG. 1.13You even get an About box in this start application.

FIG. 1.14An MDI application can display a number of documents at once.

Ch oose Fil e, New, a n d a secon d win dow, Fir st M2, a ppea r s. Tr y min imiz in g, ma ximiz in g, a n d r est or in g t h ese win dows. Swit ch a mon g t h em u sin g t h e Win dow men u . Al l t h is

f u n ct ion a l it y is you r s f r om AppWiz a r d, a n d you don 't h a ve t o wr it e a sin gl e l in e of code t o get it .

Creating a Dialog-Based Application

A dia l og-ba sed a ppl ica t ion h a s n o men u s ot h er t h a n t h e syst em men u , a n d it ca n n ot sa ve or open a f il e. Th is ma kes it good f or simpl e u t il it ies l ike t h e Win dows Ch a r a ct er Ma p. Th e AppWiz a r d pr ocess is a l it t l e dif f er en t f or a dia l og-ba sed a ppl ica t ion , pr ima r il y beca u se su ch a ppl ica t ion s ca n 't h a ve a docu men t a n d t h er ef or e ca n 't su ppor t da t a ba se a ccess or compou n d docu men t s. To cr ea t e a dia l og-ba sed a ppl ica t ion , st a r t AppWiz a r d a s you did f or t h e SDI or MDI a ppl ica t ion , bu t in St ep 1 ch oose a dia l og-ba sed a ppl ica t ion , a s sh own in Figu r e 1.15. Ca l l t h is a ppl ica t ion Fir st Dial o g.

FIG. 1.15To create a dialog-based application, specify your preference in Step 1 of the AppWizard process.

(22)

FIG. 1.16Step 2 of the AppWizard process for a dialog-based application involves choosing Help, Automation, ActiveX, and Sockets settings.

If you wou l d l ike a n Abou t it em on t h e syst em men u , sel ect t h e Abou t Box it em. To h a ve AppWiz a r d l a y t h e f r a mewor k f or Hel p, sel ect t h e Con t ext -Sen sit ive Hel p opt ion . Th e t h ir d ch eck box, 3D Con t r ol s, sh ou l d be sel ect ed f or most Win dows 95 a n d Win dows NT a ppl ica t ion s. If you wa n t you r a ppl ica t ion t o su r r en der con t r ol t o ot h er a ppl ica t ion s t h r ou gh a u t oma t ion , a s discu ssed in Ch a pt er 16, sel ect t h e Au t oma t ion ch eck box. If you wa n t you r a ppl ica t ion t o con t a in Act iveX con t r ol s, sel ect t h e Act iveX Con t r ol s ch eck box. If you a r e pl a n n in g t o h a ve t h is a ppl ica t ion wor k over t h e In t er n et wit h socket s, ch eck t h e Win dows Socket s box. (Dia l og-ba sed a pps ca n 't u se MAPI beca u se t h ey h a ve n o docu men t .) Cl ick Next t o move t o t h e t h ir d st ep, sh own in Figu r e 1.17.

As wit h t h e SDI a n d MDI a ppl ica t ion s cr ea t ed ea r l ier , you wa n t commen t s in you r code. Th e decision bet ween st a t ic l in kin g a n d a sh a r ed DLL is a l so t h e sa me a s f or t h e SDI a n d MDI a ppl ica t ion s. If you r u ser s a r e l ikel y t o a l r ea dy h a ve t h e MFC DLLs (beca u se t h ey a r e devel oper s or beca u se t h ey h a ve a n ot h er pr odu ct t h a t u ses t h e DLL) or if t h ey won 't min d in st a l l in g t h e DLLs a s wel l a s you r execu t a bl e, go wit h t h e sh a r ed DLL t o ma ke a sma l l er execu t a bl e f il e a n d a f a st er l in k. Ot h er wise, ch oose As A St a t ica l l y Lin ked Libr a r y. Cl ick Next t o move t o t h e f in a l st ep, sh own in Figu r e 1.18.

FIG. 1.17Step 3 of the AppWizard process for a dialog-based application deals with comments and the MFC library.

FIG. 1.18Step 4 of the AppWizard process for a dialog-based application gives you a chance to adjust filenames and classnames.

In t h is st ep you ca n ch a n ge t h e n a mes AppWiz a r d ch ooses f or f il es a n d cl a sses. Th is is r a r el y a good idea beca u se it wil l con f u se peopl e wh o ma in t a in you r code if t h e f il en a mes ca n 't be ea sil y dist in gu ish ed f r om t h e cl a ssn a mes, a n d vice ver sa . If you r ea l iz e a f t er l ookin g a t t h is dia l og t h a t you ma de a poor ch oice of pr oject n a me, u se Ba ck t o move a l l t h e wa y ba ck t o t h e New Pr oject Wor kspa ce dia l og, ch a n ge t h e n a me, cl ick Cr ea t e, a n d t h en u se Next t o r et u r n t o t h is dia l og. Cl ick Fin ish t o see t h e su mma r y of t h e f il es a n d cl a sses t o be cr ea t ed, simil a r t o t h a t in Figu r e 1.19.

If a n y in f or ma t ion on t h is dia l og isn 't wh a t you wa n t ed, cl ick Ca n cel a n d t h en u se Ba ck t o move t o t h e a ppr opr ia t e st ep a n d ch a n ge you r ch oices. Wh en t h e in f or ma t ion is r igh t , cl ick OK a n d wa t ch a s t h e a ppl ica t ion is cr ea t ed.

(23)

FIG. 1.19AppWizard confirms the files and classes before creating them.

FIG. 1.20A starter dialog application includes a reminder of the work ahead of you.

Cl ickin g t h e OK or Ca n cel bu t t on , or t h e X in t h e t op-r igh t cor n er , ma kes t h e dia l og disa ppea r . Cl ickin g t h e syst em men u in t h e t op-l ef t cor n er gives you a ch oice of Move, Cl ose, or Abou t . Figu r e 1.21 sh ows t h e Abou t box t h a t wa s gen er a t ed f or you .

FIG. 1.21The same About box is generated for SDI, MDI, and dialog-based applications.

Creating DLLs, Console Applications, and More

Al t h ou gh most peopl e u se AppWiz a r d t o cr ea t e a n execu t a bl e pr ogr a m, it ca n ma ke ma n y ot h er kin ds of pr oject s. You ch oose Fil e, New a n d t h en t h e Pr oject s t a b, a s discu ssed a t t h e st a r t of t h is ch a pt er , bu t ch oose a dif f er en t wiz a r d f r om t h e l ist on t h e l ef t of t h e New dia l og box, sh own in Figu r e 1.1. Th e f ol l owin g a r e some of t h e ot h er pr oject s

AppWiz a r d ca n cr ea t e:

● ATL COM AppWiz a r d

● Cu st om AppWiz a r d

● Da t a ba se Pr oject

● DevSt u dio Add-In Wiz a r d

● Ext en ded St or ed Pr ocedu r e AppWiz a r d

● ISAPI Ext en sion Wiz a r d

● Ma kef il e

● MFC Act iveX Con t r ol Wiz a r d

● MFC AppWiz a r d (dl l )

● Ut il it y Pr oject

● Win 32 Appl ica t ion

● Win 32 Con sol e Appl ica t ion

(24)

● Win 32 St a t ic Libr a r y

Th ese pr oject s a r e expl a in ed in t h e f ol l owin g sect ion s.

ATL COM AppWizard

ATL is t h e Act ive Templ a t e Libr a r y, a n d it 's u sed t o wr it e sma l l Act iveX con t r ol s. It 's gen er a l l y u sed by devel oper s wh o h a ve a l r ea dy ma st er ed wr it in g MFC Act iveX

con t r ol s, t h ou gh a n MFC ba ckgr ou n d is n ot r equ ir ed t o l ea r n ATL. Ch a pt er 17 in t r odu ces impor t a n t con t r ol con cept s wh il e demon st r a t in g h ow t o bu il d a n MFC con t r ol ; Ch a pt er 21, "Th e Act ive Templ a t e Libr a r y," t ea ch es you ATL.

Custom AppWizard

Per h a ps you wor k in a l a r ge pr ogr a mmin g sh op t h a t bu il ds ma n y a ppl ica t ion s. Al t h ou gh AppWiz a r d sa ves a l ot of t ime, you r pr ogr a mmer s ma y spen d a da y or t wo a t t h e st a r t of ea ch pr oject pa st in g in you r own boilerplate, wh ich is ma t er ia l t h a t is t h e sa me in ever y on e of you r pr oject s. You ma y f in d it wel l wor t h you r t ime t o bu il d a Cu st om

AppWiz a r d, a wiz a r d of you r ver y own t h a t pu t s in you r boil er pl a t e a s wel l a s t h e st a n da r d MFC ma t er ia l . Af t er you h a ve don e t h is, you r a ppl ica t ion t ype is a dded t o t h e l ist box on t h e l ef t of t h e Pr oject s t a b of t h e New dia l og box sh own in Figu r e 1.1.

Cr ea t in g a n d u sin g Cu st om AppWiz a r ds is discu ssed in Ch a pt er 25, "Ach ievin g Reu se wit h t h e Ga l l er y a n d You r Own AppWiz a r ds."

Database Project

If you h a ve in st a l l ed t h e En t er pr ise Edit ion of Visu a l C++, you ca n cr ea t e a da t a ba se pr oject . Th is is discu ssed in Ch a pt er 23, "SQL a n d t h e En t er pr ise Edit ion ."

DevStudio Add-In Wizard

Add-in s a r e l ike ma cr os t h a t a u t oma t e Devel oper St u dio, bu t t h ey a r e wr it t en in C++ or a n ot h er pr ogr a mmin g l a n gu a ge; ma cr os a r e wr it t en in VBScr ipt . Th ey u se a u t oma t ion t o ma n ipu l a t e Devel oper St u dio.

ISAPI Extension Wizard

(25)

Makefile

If you wa n t t o cr ea t e a pr oject t h a t is u sed wit h a dif f er en t ma ke u t il it y t h a n

Devel oper St u dio, ch oose t h is wiz a r d f r om t h e l ef t l ist in t h e New Pr oject Wor kspa ce dia l og box. No code is gen er a t ed. If you don 't kn ow wh a t a ma ke u t il it y is, don 't wor r y - t h is wiz a r d is f or t h ose wh o pr ef er t o u se a st a n da l on e t ool t o r epl a ce on e por t ion of Devel oper St u dio.

MFC ActiveX ControlWizard

ActiveX controls a r e con t r ol s you wr it e t h a t ca n be u sed on a Visu a l C++ dia l og, a Visu a l Ba sic f or m, or even a Web pa ge. Th ese con t r ol s a r e t h e 32-bit r epl a cemen t f or t h e VBX con t r ol s ma n y devel oper s wer e u sin g t o a ch ieve in t u it ive in t er f a ces or t o a void

r ein ven t in g t h e wh eel on ever y pr oject . Ch a pt er 17 gu ides you t h r ou gh bu il din g a con t r ol wit h t h is wiz a r d.

MFC AppWizard (DLL)

If you wa n t t o col l ect a n u mber of f u n ct ion s in t o a DLL, a n d t h ese f u n ct ion s u se MFC cl a sses, ch oose t h is wiz a r d. (If t h e f u n ct ion s don 't u se MFC, ch oose Win 32 Dyn a mic Lin k Libr a r y, discu ssed a l it t l e l a t er in t h is sect ion .) Bu il din g a DLL is cover ed in Ch a pt er 28, "Fu t u r e Expl or a t ion s." AppWiz a r d gen er a t es code f or you so you ca n get st a r t ed.

Win32 Application

Th er e a r e t imes wh en you wa n t t o cr ea t e a Win dows a ppl ica t ion in Visu a l C++ t h a t does n ot u se MFC a n d does n ot st a r t wit h t h e boil er pl a t e code t h a t AppWiz a r d pr odu ces f or you . To cr ea t e su ch a n a ppl ica t ion , ch oose t h e Win 32 Appl ica t ion wiz a r d f r om t h e l ef t l ist in t h e Pr oject s t a b, f il l in t h e n a me a n d f ol der f or you r pr oject , a n d cl ick OK. You a r e n ot a sked a n y qu est ion s; AppWiz a r d simpl y cr ea t es a pr oject f il e f or you a n d open s it . You h a ve t o cr ea t e a l l you r code f r om scr a t ch a n d in ser t t h e f il es in t o t h e pr oject .

Win32 Console Application

A console application l ooks ver y mu ch l ike a DOS a ppl ica t ion , t h ou gh it r u n s in a r esiz a bl e win dow. (Con sol e a ppl ica t ion s a r e 32-bit a ppl ica t ion s t h a t won 't r u n u n der DOS,

h owever .) It h a s a st r ict l y ch a r a ct er -ba sed in t er f a ce wit h cu r sor keys in st ea d of mou se movemen t . You u se t h e Con sol e API a n d ch a r a ct er -ba sed I/O f u n ct ion s su ch a s pr in t f () a n d sca n f () t o in t er a ct wit h t h e u ser . Some ver y r u dimen t a r y boil er pl a t e code ca n be gen er a t ed f or you , or you ca n h a ve ju st a n empt y pr oject . Ch a pt er 28 discu sses bu il din g a n d u sin g con sol e a ppl ica t ion s.

(26)

If you pl a n t o bu il d a DLL t h a t does n ot u se MFC a n d does n ot n eed a n y boil er pl a t e, ch oose t h e Win 32 Dyn a mic Lin k Libr a r y opt ion in st ea d of MFC AppWiz a r d (dl l ). You get a n empt y pr oject cr ea t ed r igh t a wa y wit h n o qu est ion s.

Win32 Static Library

Al t h ou gh most code you r eu se is ga t h er ed in t o a DLL, you ma y pr ef er t o u se a st a t ic l ibr a r y beca u se t h a t mea n s you don 't h a ve t o dist r ibu t e t h e DLL wit h you r a ppl ica t ion . Ch oose t h is wiz a r d f r om t h e l ef t l ist in t h e New Pr oject Wor kspa ce dia l og box t o cr ea t e a pr oject f il e in t o wh ich you ca n a dd object f il es t o be l in ked in t o a st a t ic l ibr a r y,

wh ich is t h en l in ked in t o you r a ppl ica t ion s.

Changing Your AppWizard Decisions

Ru n n in g AppWiz a r d is a on e-t ime t a sk. Assu min g you a r e ma kin g a t ypica l a ppl ica t ion , you ch oose Fil e, New; cl ick t h e Pr oject s t a b; en t er a n a me a n d f ol der ; ch oose MFC

Appl ica t ion (exe); go t h r ou gh t h e six st eps; cr ea t e t h e a ppl ica t ion st a r t er f il es; a n d t h en n ever t ou ch AppWiz a r d a ga in . However , wh a t if you ch oose n ot t o h a ve on l in e Hel p a n d l a t er r ea l iz e you sh ou l d h a ve in cl u ded it ?

AppWiz a r d, despit e t h e n a me, isn 't r ea l l y ma gic. It pa st es in bit s a n d pieces of code you n eed, a n d you ca n pa st e in t h ose ver y sa me bit s you r sel f . Her e's h ow t o f in d ou t wh a t you n eed t o pa st e in .

Fir st , cr ea t e a pr oject wit h t h e sa me opt ion s you u sed in cr ea t in g t h e pr oject wh ose set t in gs you wa n t t o ch a n ge, a n d don 't a dd a n y code t o it . Secon d, in a dif f er en t f ol der cr ea t e a pr oject wit h t h e sa me n a me a n d a l l t h e sa me set t in gs, except t h e on e t h in g you wa n t t o ch a n ge (Con t ext -Sen sit ive Hel p in t h is exa mpl e). Compa r e t h e f il es, u sin g

Win Dif f , wh ich comes wit h Visu a l C++. Now you kn ow wh a t bit s a n d pieces you n eed t o a dd t o you r f u l l -of -code pr oject t o impl emen t t h e f ea t u r e you f or got t o a sk AppWiz a r d f or .

Some devel oper s, if t h ey discover t h eir mist a ke soon en ou gh , f in d it qu icker t o cr ea t e a n ew pr oject wit h t h e desir ed f ea t u r es a n d t h en pa st e t h eir own f u n ct ion s a n d r esou r ces f r om t h e pa r t ia l l y bu il t pr oject in t o t h e n ew empt y on e. It 's on l y a ma t t er of t a st e, bu t a f t er you go t h r ou gh eit h er pr ocess f or ch a n gin g you r min d, you pr oba bl y wil l move a l it t l e mor e sl owl y t h r ou gh t h ose AppWiz a r d dia l og boxes.

Understanding AppWizard's Code

(27)

or der t o wr it e you r own simpl e a ppl ica t ion s. You r pr ogr a ms wil l be bet t er on es, t h ou gh , if you kn ow wh a t t h ey a r e doin g, so a qu ick t ou r of AppWiz a r d's boil er pl a t e code is a good idea . You 'l l see t h e cor e of a n SDI a ppl ica t ion , a n MDI a ppl ica t ion , a n d a dia l og-ba sed a ppl ica t ion .

You 'l l n eed t h e st a r t er a ppl ica t ion s Fir st SDI, Fir st MDI, a n d Fir st Dia l og, so if you didn 't cr ea t e t h em ea r l ier , do so n ow. If you 'r e u n f a mil ia r wit h t h e Devel oper St u dio

in t er f a ce, gl a n ce t h r ou gh Appen dix C t o l ea r n h ow t o edit code a n d l ook a t cl a sses.

A Single Document Interface Application

An SDI a ppl ica t ion h a s men u s t h a t t h e u ser u ses t o open on e docu men t a t a t ime a n d wor k wit h t h a t docu men t . Th is sect ion pr esen t s t h e code t h a t is gen er a t ed wh en you cr ea t e a n SDI a ppl ica t ion wit h n o da t a ba se or compou n d docu men t su ppor t , wit h a

t ool ba r , a st a t u s ba r , Hel p, 3D con t r ol s, sou r ce f il e commen t s, a n d wit h t h e MFC l ibr a r y a s a sh a r ed DLL - in ot h er wor ds, wh en you a ccept a l l t h e AppWiz a r d def a u l t s a f t er St ep 1.

Five cl a sses h a ve been cr ea t ed f or you . For t h e a ppl ica t ion Fir st SDI, t h ey a r e a s f ol l ows:

● CAbou t Dl g, a dia l og cl a ss f or t h e Abou t dia l og box

● CFir st SDIApp, a CWin App cl a ss f or t h e en t ir e a ppl ica t ion

● CFir st SDIDoc, a docu men t cl a ss

● CFir st SDIView, a view cl a ss

● CMa in Fr a me, a f r a me cl a ss

Dia l og cl a sses a r e discu ssed in Ch a pt er 2, "Dia l ogs a n d Con t r ol s." Docu men t , view, a n d f r a me cl a sses a r e discu ssed in Ch a pt er 4. Th e h ea der f il e f or CFir st SDIApp is sh own in List in g 1.1. Th e ea siest wa y f or you t o see t h is code is t o dou bl e-cl ick on t h e cl a ssn a me, CFir st DSIApp, in t h e Cl a ssView pa n e. Th is wil l edit t h e h ea der f il e f or t h e cl a ss.

Listing 1.1 FirstSDI.h - Main Header File for the FirstSDI Application

// FirstSDI.h : main header file for the FIRSTSDI application //

#if

!defined(AFX_FIRSTSDI_H__CDF38D8A_8718_11D0_B02C_0080C81A3AA2__INCLUDED_) #define

(28)

#pragma once

#endif // _MSC_VER >= 1000 #ifndef __AFXWIN_H__

#error include `stdafx.h' before including this file for PCH #endif

#include "resource.h" // main symbols

///////////////////////////////////////////////////////////////////////////// // CFirstSDIApp:

// See FirstSDI.cpp for the implementation of this class //

class CFirstSDIApp : public CWinApp {

public:

CFirstSDIApp(); // Overrides

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CFirstSDIApp)

public:

virtual BOOL InitInstance(); //}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CFirstSDIApp) afx_msg void OnAppAbout();

// NOTE - The ClassWizard will add and remove member functions here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG

DECLARE_MESSAGE_MAP() };

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}}

// Microsoft Developer Studio will insert additional declarations // immediately before the previous line.

#endif

//!defined(AFX_FIRSTSDI_H__CDF38D8A_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)

Th is code is con f u sin g a t t h e begin n in g. Th e #if (!def in ed) f ol l owed by t h e ver y l on g st r in g (you r s wil l be dif f er en t ) is a cl ever f or m of in cl u de gu a r din g. You ma y h a ve seen a code sn ippet l ike t h is bef or e:

#ifndef test_h #include "test.h" #define test_h #endif

(29)

f r om t h e compil er a bou t "r edef in in g" t h e symbol s in BigCor p.h t h e secon d t ime it is in cl u ded.

Th er e is a pr obl em wit h t h is a ppr oa ch : If someon e in cl u des t est .h bu t f or get s t o set

t est _h , you r code wil l in cl u de t est .h t h e secon d t ime. Th e sol u t ion is t o pu t t h e t est a n d t h e def in it ion in t h e h ea der f il e in st ea d, so t h a t t est .h l ooks l ike t h is:

#ifndef test_h

... the entire header file #define test_h

#endif

Al l AppWiz a r d did wa s gen er a t e a mor e compl ica t ed va r ia bl e n a me t h a n t est _h (t h is wil d n a me pr even t s pr obl ems wh en you h a ve sever a l f il es, in dif f er en t f ol der s a n d

pr oject s, wit h t h e sa me n a me) a n d u se a sl igh t l y dif f er en t syn t a x t o ch eck t h e va r ia bl e. Th e #pr a gma on ce code is a l so design ed t o pr even t mu l t ipl e def in it ion s if t h is f il e is ever in cl u ded t wice.

Th e a ct u a l mea t of t h e f il e is t h e def in it ion of t h e cl a ss CFir st SDIApp. Th is cl a ss

in h er it s f r om CWin App, a n MFC cl a ss t h a t pr ovides most of t h e f u n ct ion a l it y you n eed. AppWiz a r d h a s gen er a t ed some f u n ct ion s f or t h is cl a ss t h a t over r ide t h e on es in h er it ed f r om t h e ba se cl a ss. Th e sect ion of code t h a t begin s //Over r ides is f or vir t u a l f u n ct ion over r ides. AppWiz a r d gen er a t ed t h e odd-l ookin g commen t s t h a t su r r ou n d t h e

decl a r a t ion of In it In st a n ce(): Cl a ssWiz a r d wil l u se t h ese t o simpl if y t h e job of a ddin g ot h er over r ides l a t er , if t h ey a r e n ecessa r y. Th e n ext sect ion of code is a messa ge ma p a n d decl a r es t h er e is a f u n ct ion ca l l ed On AppAbou t . You ca n l ea r n a l l a bou t messa ge ma ps in Ch a pt er 3, "Messa ges a n d Comma n ds."

AppWiz a r d gen er a t ed t h e code f or t h e CFir st SDIApp con st r u ct or , In it In st a n ce(), a n d On AppAbou t () in t h e f il e f ir st sdi.cpp. Her e's t h e con st r u ct or , wh ich in it ia l iz es a

CFir st SDIApp object a s it is cr ea t ed:

CFirstSDIApp::CFirstSDIApp() {

// TODO: add construction code here,

// Place all significant initialization in InitInstance }

Th is is a t ypica l Micr osof t con st r u ct or . Beca u se con st r u ct or s don 't r et u r n va l u es, t h er e is n o ea sy wa y t o in dica t e t h a t t h er e h a s been a pr obl em wit h t h e in it ia l iz a t ion . Th er e a r e sever a l wa ys t o dea l wit h t h is. Micr osof t 's a ppr oa ch is a t wo-st a ge in it ia l iz a t ion , wit h a sepa r a t e in it ia l iz in g f u n ct ion so t h a t con st r u ct ion does n o in it ia l iz a t ion . For a n a ppl ica t ion , t h a t f u n ct ion is ca l l ed In it In st a n ce(), sh own in List in g 1.2.

(30)

BOOL CFirstSDIApp::InitInstance() {

AfxEnableControlContainer(); // Standard initialization

// If you are not using these features and want to reduce the size

// of your final executable, you should remove from the following

// the specific initialization routines you don't need. #ifdef _AFXDLL

Enable3dControls(); // Call this when using MFC in a shared DLL

#else

Enable3dControlsStatic(); // Call this when linking to MFC statically

#endif

// Change the registry key under which our settings are stored. // You should modify this string to be something appropriate, // such as the name of your company or organization.

SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including // MRU)

// Register the application's document templates. Document templates

// serve as the connection between documents, frame windows, and views.

CSingleDocTemplate* pDocTemplate;

pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME,

RUNTIME_CLASS(CFirstSDIDoc),

RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CFirstSDIView));

AddDocTemplate(pDocTemplate);

// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo))

return FALSE;

// The one and only window has been initialized, so show and update it.

m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();

return TRUE; }

(31)

In it In st a n ce() goes on t o r egist er sin gl e docu men t t empl a t es, wh ich is wh a t ma kes t h is a n SDI a ppl ica t ion . Docu men t s, views, f r a mes, a n d docu men t t empl a t es a r e a l l discu ssed in Ch a pt er 4.

Fol l owin g t h e commen t a bou t pa r sin g t h e comma n d l in e, In it In st a n ce() set s u p a n empt y CComma n dLin eIn f o object t o h ol d a n y pa r a met er s t h a t ma y h a ve been pa ssed t o t h e a ppl ica t ion wh en it wa s r u n , a n d it ca l l s Pa r seComma n dLin e() t o f il l t h a t . Fin a l l y, it ca l l s Pr ocessSh el l Comma n d() t o do wh a t ever t h ose pa r a met er s r equ est ed. Th is mea n s you r a ppl ica t ion ca n su ppor t comma n d-l in e pa r a met er s t o l et u ser s sa ve t ime a n d ef f or t , wit h ou t ef f or t on you r pa r t . For exa mpl e, if t h e u ser t ypes a t t h e comma n d l in e

Fir st SDI f o o bl e, t h e a ppl ica t ion st a r t s a n d open s t h e f il e ca l l ed fooble. Th e comma n d-l in e pa r a met er s t h a t Pr ocessSh ed-l d-l Comma n d() su ppor t s a r e t h e f od-l d-l owin g:

Par ame t e r Ac t io n

Non e St a r t a pp a n d open n ew f il e. Fil en a me St a r t a pp a n d open f il e.

/p f il en a me St a r t a pp a n d pr in t f il e t o def a u l t pr in t er . /pt f il en a me pr in t er dr iver por t St a r t a pp a n d pr in t f il e t o t h e specif ied

pr in t er .

/dde St a r t a pp a n d a wa it DDE comma n d. /Au t oma t ion St a r t a pp a s a n OLE a u t oma t ion ser ver . /Embeddin g St a r t a pp t o edit a n embedded OLE it em.

If you wou l d l ike t o impl e

Gambar

Table 3.1  Windows Message Prefixes and Window Types
FIG. 4.1 When you create an SDI application with AppWizard, the project information summary
Table 4.1  View Classes
Figur e 4.4 shows how t he view cl asses fit  int o MFC's cl ass hier ar chy. Descr ibing t hese var ious view cl asses ful l y is beyond t he scope of t his chapt er
+7

Referensi

Dokumen terkait

Manajemen Pelatihan Ketenagakerjaan Pendekatan Terpadu Pengembangan Sumber Daya Manusia.. Jakarta:

pengunjung Central Park Mall yang menggunakan layanan GO-CAR. 1.2

Dari hasil pengujian analisis regresi diperoleh nilai t hitung sebesar 2,217 dengan tingkat signifikansi sebesar 0,025 (p &lt; 0,05) maka variabel Sifat Konsumtif

&#34;roses pengeluaran sputum dari paruparu, bronkus dan trakea yang dihasilkan oleh klien &#34;roses pengeluaran sputum dari paruparu, bronkus dan trakea yang dihasilkan oleh

Menurut Kristiyanasari (2010), ada beberapa cara yang dapat digunakan untuk mengetahui status gizi ibu hamil, antara lain penilaian status gizi secara langsung dan tidak

Kondisi SM Rimbang Baling sangat memprihatinkan saat ini, dan sangat disayangkan jika pada akhirnya, pemasalahan yang terjadi di kawasan konservasi menyebabkan

Adaptasi atau penyesuaian diri merupakan salah satu dari sekian banyak permasalahan yang dihadapi manusia.Selama dua bulan tinggal di luar negeri dengan corak sosial budaya

Pada saat Peraturan Daerah ini mulai berlaku, Peraturan Daerah Kabupaten Nomor 7 Tahun 2001 tentang Retribusi dan Sewa Pemakaian Kekayaan Daerah (Lembaran Daerah