Microsoft Visual Basic 6.0 Vodi~ za programere Microsoft Press MMiiccrroossooffttfifiVViissuuaall BBaassiicc 66..00 pprrooggrraammeerr(cid:146)(cid:146)ss GGuuiiddee Authorized translation from the English language edition published by Microsoft Press. Copyright ' 1998 by Microsoft Corporation All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying or by any informaton storage retrieval system, without permission in writing from the Publisher. Croatian language edition published by Strijelac d.o.o. Copyright ' 1999 Preveo s engleskoga:Dioniz Daubachy Izdava~: (cid:132)Strijelac(cid:147) Zagreb Urednik: Goran Jedna~ak Lektura i korektura: Rada Fedor Priprema za tisak: Grafi~ki studio (cid:132)MI[(cid:147) Tisak: Tiskara Express digitalni tisak d.o.o., Rijeka CIP (cid:150) Katalogizacija u publikaciji Nacionalna i sveu~ili{na knji‘nica, Zagreb UDK 004.430 Visual Basic 6.0 Microsoft Visual Basic 6.0 Vodi~ za programere /<preveo s engleskoga Dioniz Daubachy; urednik Goran Jedna~ak>.(cid:150) Zagreb: Strijelac, 1999. 960 str.: ilustr.; 23 cm. Prijevod djela: Microsoft Visual Basic 6.0 Programmer(cid:146)s Guide (cid:150) Kazalo. ISBN 953-210-004-0 990920071 Copyright 'za hrvatsko izdanje STRIJELAC, Zagreb, 1999. Sva prava pridr‘ana. Niti jedan dio knjige nije dozvoljeno upotrebljavati ili reproducirati u bilo kojem obliku ili na bilo koji na~in, niti pohranjivati u bazu podataka bilo kojeg oblika ili namjene bez prethodne pismene dozvole izdava~a, osim u slu~ajevima kratkih navoda u stru~nim ~lancima. Izrada kopija bilo kojeg dijela knjige u bilo kojem obliku predstavlja povredu Zakona. Izdava~ i autor ulo‘ili su najve}i mogu}i napor u kvalitetnom pripremanju ove knjige. Izdava~ i autor objavom ove knjige ne pru‘aju nikakve garancije, izravne ili posredne, za bilo koji dio sadr‘aja knjige, proceduru, postupak, radnju ili navod koje se nalaze u ovoj knjizi. Izdava~ i autor ne snose nikakve posljedice koje bi mogle nastati uporabom ove knjige, niti se smatraju odgovornim za bilo kakvu {tetu ili gubitak uzrokovan izravno ili posredno ovom knjigom. Sadr‘aj Uobi~ajeni na~ini ispisa........................................................................................................ xi Vodi~ za programere ........................................................................................................... xv 1. DIO Temelji Visual Basica.....................................................................................................1 1. POGLAVLJE Uvod u Visual Basic................................................................................... 3 Dobro do{li u Visual Basic................................................................................................................................3 Instaliranje Visual Basica..................................................................................................................................5 Dobivanje pomo}i tijekom rada........................................................................................................................7 2. POGLAVLJE Razvijanje aplikacije u Visual Basicu...................................................... 11 Pojmovi Visual Basica.....................................................................................................................................11 Elementi ugra|ene razvojne okoline...............................................................................................................13 Va{a prva Visual Basic aplikacija...................................................................................................................18 3. POGLAVLJE Forme, kontrole i izbornici....................................................................... 31 Tuma~enje svojstava, postupaka i doga|aja...................................................................................................32 Oblikovanje forme...........................................................................................................................................33 Pokretanje akcija klikom na gumb..................................................................................................................36 Kontrole za prikaz i unos teksta......................................................................................................................37 Kontrole za prikaz slika i grafike....................................................................................................................50 Dodatne kontrole.............................................................................................................................................56 Razumijevanje fokusa.....................................................................................................................................57 Postavljanje tabularnog reda...........................................................................................................................59 Osnove izbornika.............................................................................................................................................60 Upit korisniku dijalo{kim okvirom.................................................................................................................63 4. POGLAVLJE Upravljanje projektima............................................................................. 65 Rad s projektima.............................................................................................................................................65 Sastav projekta u Visual Basicu......................................................................................................................67 Stvaranje, otvaranje i spremanje projekata.....................................................................................................70 Dodavanje, micanje i spremanje datoteka.......................................................................................................71 Vodi~ za programere iii Sadr‘aj Dodavanje kontrola u projekt..........................................................................................................................72 Izrada i pokretanje izvr{ne datoteke................................................................................................................76 Postavljenje mogu}nosti projekta...................................................................................................................77 Kori{tenje ~arobnjaka i dodataka....................................................................................................................77 5. POGLAVLJE Osnove programiranja.............................................................................. 79 Struktura aplikacije Visual Basica..................................................................................................................80 Prije programiranja..........................................................................................................................................82 Na~in pisanje koda..........................................................................................................................................84 Uvod u varijable, konstante i tipove podataka................................................................................................92 Uvod u potprograme.....................................................................................................................................120 Uvod u strukturu kontrala.............................................................................................................................131 Rad s objektima.............................................................................................................................................139 2. DIO [to mo‘ete napraviti s Visual Basicom................................................................ 159 6. POGLAVLJE Stvaranje korisni~kog su~elja................................................................ 161 Stilovi su~elja................................................................................................................................................162 Aplikacije s vi{e dokumenata (MDI)............................................................................................................164 Vi{e o formama.............................................................................................................................................173 Kori{tenje izbornika u va{oj aplikaciji..........................................................................................................175 Alatne trake...................................................................................................................................................192 Dijalo{ki okviri..............................................................................................................................................195 Oblikovanje za razli~ite tipove prikaza.........................................................................................................204 Oblikovanje s korisnikom na pameti............................................................................................................205 7. POGLAVLJE Kori{tenje standardnih kontrola Visual Basica...................................... 219 Uvod u kontrole Visual Basica......................................................................................................................220 Provjera valjanosti podataka u kontroli ograni~avanjem fokusa..................................................................227 Kori{tenje kontrole ADO podataka...............................................................................................................233 Kori{tenje kontrole kontrolne ku}ice............................................................................................................239 Kori{tenje kontrole kombiniranog okvira.....................................................................................................241 Kori{tenje kontrole naredbenog gumba........................................................................................................246 Kori{tenje kontrole op}eg dijaloga...............................................................................................................248 Kori{tenje kontrole podataka........................................................................................................................257 Kori{tenje kontrola kombiniranog okvira za podatke i okvira s popisom podataka....................................258 Kori{tenje kontrole mre‘e s podacima..........................................................................................................264 Kori{tenje kontrola datote~nog sustava........................................................................................................280 iv Vodi~ za programere Sadr‘aj Kori{tenje kontrole okvira.............................................................................................................................289 Kori{tenje kontrole hijerarhijske fleksibilne mre‘e......................................................................................291 Kori{tenje kontrola vodoravne i okomite klizne trake..................................................................................321 Kori{tenje kontrole slike...............................................................................................................................326 Kori{tenje kontrole natpisa...........................................................................................................................329 Kori{tenje kontrole linije...............................................................................................................................330 Kori{tenje kontrole okvira s popisom............................................................................................................332 Kori{tenje kontrole OLE spremnika.............................................................................................................342 Kori{tenje kontrole gumba izbora.................................................................................................................343 Kori{tenje kontrole okvira za sliku...............................................................................................................345 Kori{tenje kontrole udaljenih podataka........................................................................................................348 Kori{tenje kontrole lika.................................................................................................................................350 Kori{tenje kontrole okvira s tekstom............................................................................................................352 Kori{tenje kontrole mjera~a vremena 8. POGLAVLJE Vi{e o programiranju.............................................................................. 359 Rad s vi{e projekata......................................................................................................................................359 Rad s datotekama izvora...............................................................................................................................366 Rad s predlo{cima.........................................................................................................................................368 Rad s prekida~ima naredbene linije..............................................................................................................370 Prevo|enje va{eg projekta u strojni kod.......................................................................................................371 Stvaranje vlastitih tipova podataka...............................................................................................................373 Kori{tenje nabrajanja za rad s nizovima konstanti.......................................................................................377 Kori{tenje zbirki umjesto matrica.................................................................................................................383 9. POGLAVLJE Programiranje objektima........................................................................ 385 [to trebate znati o objektima u Visual Basicu..............................................................................................386 Odgonetavanje objekata................................................................................................................................402 Stvaranje vlastitih klasa.................................................................................................................................409 Dodavanje svojstava i postupaka klasi..........................................................................................................429 Dodavanje doga|aja klasi.............................................................................................................................444 Stvaranje klasa svjesnih podataka.................................................................................................................453 Imenovanje svojstava, postupaka i doga|aja................................................................................................460 Polimorfizam.................................................................................................................................................461 Programiranje va{im vlastitim objektima.....................................................................................................473 Modeli objekata.............................................................................................................................................475 Stvaranje vlastitih klasa zbirki......................................................................................................................480 ActiveX kreatori............................................................................................................................................493 Vodi~ za programere v 10. poglavlje Programiranje sastavnim dijelovima ......................................................... 499 Tipovi ActiveX sastavnih dijelova ...............................................................................................................500 Poslu‘itelji vrste u-procesu i izvan-procesa .................................................................................................501 Rad s ActiveX sastavnim dijelovima ...........................................................................................................502 Stvaranje pokaziva~a na objekt ....................................................................................................................503 Kori{tenje svojstava, postupaka i doga|aja objekta ....................................................................................513 Otpu{tanje ActiveX sastavnog dijela ...........................................................................................................516 Kretanje kroz modele objekata ....................................................................................................................517 Rukovanje pogre{kama tijekom rada u ActiveX sastavnim dijelovima ......................................................521 Rukovanje zahtjevima u tijeku za ActiveX sastavni dio .............................................................................523 Kori{tenje vidljivog su~elja sastavnih dijelova ............................................................................................529 11. POGLAVLJE Odgovaranje na doga|aje mi{a i tipkovnice.......................................... 543 Odgovaranje na doga|aje mi{a ....................................................................................................................544 Otkrivanje tipki mi{a ....................................................................................................................................549 Otkrivanje stanja SHIFT, CTRLi ALT ........................................................................................................555 Povla~enje i ispu{tanje .................................................................................................................................556 Povla~enje i ispu{tanje tipa OLE .................................................................................................................562 Prilago|avanje pokaziva~a mi{a ..................................................................................................................582 Odgovaranje na doga|aje tipkovnice ...........................................................................................................583 Prekidanje pozadinske obrade ......................................................................................................................591 12. POGLAVLJE Rad s tekstom i grafikom ...................................................................... 595 Rad s pismima ..............................................................................................................................................596 Prikazivanje teksta na formama i okvirima za sliku ....................................................................................600 Oblikovanje brojeva, datuma i vremena ......................................................................................................604 Rad s odabranim tekstom .............................................................................................................................606 Preno{enje teksta i grafike s objektom Clipboard .......................................................................................607 Razumijevanje koordinatnog sustava ...........................................................................................................612 Kori{tenje grafi~kih kontrola .......................................................................................................................618 Kori{tenje grafi~kih postupaka ....................................................................................................................634 Rad s bojama ................................................................................................................................................648 Kori{tenje objekta tipa Picture .....................................................................................................................657 Ispis ..............................................................................................................................................................658 13. POGLAVLJE Tra‘enje i obrada pogre{aka ................................................................. 667 Kako rukovati pogre{kama ..........................................................................................................................668 Sadr‘aj Oblikovanje rukovatelja pogre{kama ...........................................................................................................670 Hijerarhija rukovanja pogre{kama ...............................................................................................................676 Ispitivanje rukovanja pogre{kama stvaranjem pogre{aka ............................................................................679 Ugra|eno rukovanje pogre{kama .................................................................................................................681 Centralizirano rukovanje pogre{kama ..........................................................................................................684 Isklju~ivanje rukovanja pogre{kama ............................................................................................................688 Rukovanje pogre{kama s ActiveX sastavnim dijelovima ............................................................................690 Pristupi otkrivanju pogre{aka .......................................................................................................................693 Izbjegavanje pogre{aka ................................................................................................................................696 Vrijeme izrade, vrijeme izvo|enja i mod prekida ........................................................................................697 Kori{tenje prozora za otkrivanje pogre{aka .................................................................................................699 Kori{tenje moda prekida ..............................................................................................................................701 Izvo|enje odabranih dijelova va{e aplikacije ..............................................................................................711 Nadziranje stoga poziva ...............................................................................................................................714 Ispitivanje podataka i potprograma prozorom za neposredan upis naredbi ................................................716 Razmatranje posebnog otkrivanja pogre{aka ...............................................................................................724 Savjeti za otkrivanje pogre{aka ....................................................................................................................728 14. POGLAVLJE Obrada pogona, mapa i datoteka .......................................................... 731 Uvod u model objekata datote~nog sustava .................................................................................................731 Programiranje u modelu FSO objekata ........................................................................................................733 Obrada datoteka starim naredbama i funkcijama ........................................................................................742 15. POGLAVLJE Oblikovanje u korist izvo|enja i sukladnosti ........................................ 751 Razumijevanje optimizacije .........................................................................................................................752 Optimiziranje brzine .....................................................................................................................................754 Optimiziranje veli~ine ..................................................................................................................................765 Optimiziranje objekata .................................................................................................................................774 Prevedene protiv tuma~enih aplikacija ........................................................................................................777 Sukladnost s ostalim Microsoft aplikacijama ..............................................................................................778 16. POGLAVLJE Me|unarodna izdanja ............................................................................ 781 Definicije me|unarodnog softtvera ..............................................................................................................781 Oblikovanje me|unarodnog softtvera ..........................................................................................................782 Kori{tenje datoteka izvora za lokalizaciju ...................................................................................................785 Oblikovanje me|unarodnog korisni~kog su~elja .........................................................................................788 Op}enita razmi{ljanja za pisanje me|unarodnog koda ................................................................................790 Vodi~ za programere vii Sadr‘aj Pisanje me|unarodnog koda u Visual Basicu ..............................................................................................792 Izdanja specifi~na za dvobajtni skup znakova (DBCS) ...............................................................................801 Dvosmjerne osobine Visual Basica ..............................................................................................................810 17. POGLAVLJE Distribuiranje va{ih aplikacija .............................................................. 811 ^arobnjak za pakiranje i raspore|ivanje ......................................................................................................812 Pakiranje aplikacije sa ~arobnjakom ............................................................................................................816 Raspore|ivanje aplikacije s ~arobnjakom ....................................................................................................825 Upravljanje skriptama ~arobnjaka ................................................................................................................827 Alat za pode{avanje ......................................................................................................................................828 Ru~no editiranje datoteke Setup.lst ..............................................................................................................834 Ru~no stvaranje medija distribucije .............................................................................................................840 Kori{tenje ~arobnjaka za pakiranje i raspore|ivanje s alatom za pode{avanje ...........................................842 Ispitivanje va{eg programa pode{avanja ......................................................................................................843 Omogu}avanje korisniku da ukloni va{u aplikaciju ....................................................................................844 Raspore|ivanje lokaliziranih ActiveX kontrola ...........................................................................................846 DODATAK A Specifikacije, ograni~enja i vrste datoteka Visual Basica ................... 847 Sistemski zahtjevi za aplikacije Visual Basica ............................................................................................847 Ograni~enja projekta ....................................................................................................................................848 Oblici datoteka projekta ...............................................................................................................................852 Gra|a forme .................................................................................................................................................854 DODATAK B Pravila programiranja Visual Basica ..................................................... 869 Za{to pravila programiranja? .......................................................................................................................869 Pravila imenovanja objekata ........................................................................................................................870 Pravila imenovanja konstanti i varijabli ......................................................................................................874 Pravila strukturiranog programiranja ...........................................................................................................877 DODATAK C Opcije prevoditelja u strojni kod ........................................................... 881 Optimiziranje za brzi kod .............................................................................................................................882 Optimiziranje za mali kod ............................................................................................................................882 Bez optimizacije ...........................................................................................................................................882 Podr{ka Pentiumu Pro ..................................................................................................................................882 Stvaranje simboli~kih informacija za otkrivanje pogre{aka ........................................................................883 Prihva}anje bez pseudonima ........................................................................................................................883 Uklanjanje provjere granica matrica ............................................................................................................883 Uklanjanje provjere prekora~enja cijelih brojeva ........................................................................................884 viii Vodi~ za programere Sadr‘aj Uklanjanje provjere pogre{ke s pomi~nim zarezom ....................................................................................884 Uklanjanje provjere sigurnog dijeljenja s Pentiumom .................................................................................884 Omogu}avanje nezaokru‘enih operacija s pomi~nim zarezom ...................................................................885 DODATAK D Dodavanje pomo}i va{oj aplikaciji ....................................................... 887 Dodavanje podr{ke za pomo} ......................................................................................................................887 Dodavanje podr{ke za pomo} What(cid:146)s This ..................................................................................................889 Distribuiranje pomo}i s va{om aplikacijom ................................................................................................890 Kazalo................................................................................................................................. 891 Vodi~ za programere ix