vb6 16 (13)

Download Vb6 16 (13)

Post on 15-Jul-2015

71 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

Chng Mi Hai - Dng Ha (Phn I)

Chng Mi Hai - Dng Ha (Phn I)Tc ng Anh c cu: "Mt hnh nh ng gi mt ngn ch (a picture is worth a thousand words)", ni khi ta dng hnh nh din t s gip ngi xem hiu nhanh hn khi ta ch c ni thi.

Visual Basic 6 c cho ta mt s phng tin v ha (graphics) trang im cho cc ca s phong ph, thn thin, d lm vic vi, v th v. D rng cc phng tin v th ny khng nhanh cho ta vit nhng chng trnh tr chi (games) nhng tng i cng kh nng p ng cc nhu cu cn thit thng thng.

Khi ni n ha, ta mun phn bit n vi Text thng thng. Th d ta dng Notepad edit mt bi th trong mt ca s. Trong lc bi th ang c hin th ta c th sa i d dng bng cch dng bn phm nh thm cc ch mi vo, dng cc nt Delete, Backspace xa cc ch. l ta lm vic vi Text.

By gi, trong khi bi th cn ang hin th, ta dng mt chng trnh Graphic nh PhotoImpact Capture ca ULead chp ci hnh ca s ca bi th (active window) thnh ging nh mt photo, th ta c mt Graphic. Sau , mun sa i bi th t graphic ny ta phi dng mt graphic editor nh MSPaint, PaintShopPro,.v.v.. Cc ch trong hnh cng c cng dng graphic nh ta thy mt photo, nn mun edit phi dng mt c vi mu sn.

Di y l graphic ca mt ca s Notepad sau khi c thm ch g v du chm hi cui bng cch dng MSPaint.

Mu (color) v mn (resolution)Ta ni mt tm hnh tt v n c mu sc so v r rng. Bn c cn nh trong ngy L khai mc Th Vn Hi Moscow, ngi ta cho hin th nhiu hnh rt hay bng cch nh khn gi, trong mt khu hnh ch nht, mi ngi cm a ln mt tm ct-tng mu. Hng ngn tm ct-tng a ln rp li thnh ra mt hnh tuyt p.

Mt graphic trong Windows cng gm c nhiu m nh, mi m, c gi l mt pixel, c kh nng hin th 16, 256, ... mu khc nhau.

mn (resolution)Thng thng mn (resolution) ca mn nh ta dng l 800x600, tc l chiu ngang c 800 pixels v chiu cao c 600 pixels. Sau ny, xem cc hnh r hn ta cn dng mn 1028x768 vi ct SuperVGA v Monitor tt. Ta ni ct SuperVGA c n 2MB RAM, ti sao phi cn n 2MB hin th graphic p?

Nu mu ca mi pixel c biu din bi mt byte d kin th vi mt byte ta c th cha mt con s t 0 n 255. Ngi ta ng vi nhau theo mt quy c rng s 0 tng trng cho mu en, s 255 tng trng cho mu trng chng hn. Nu mn ca mn nh l 1024x768 th ta s cn 1024x768=786432 bytes, tc l gn 0,8 MB.

Mt byte c 8 bits. i khi ta nghe ni 16 bit color, ni thay v mt byte, ngi ta dng n 2 bytes cho mi pixel. Nh vy mi pixel ny c kh nng hin th 216 = 65536 mu khc nhau. Mun dng 16 bit color cho SuperVGA, ta cn phi c 1024x768x2 =1572864 bytes, tc l gn 1,6 MB. l l do ti sao ta cn 2MB RAM. Lu l RAM ca VGA (Vector Graphic Adapter) card khng lin h g vi RAM ca b nh computer.

Khng ng cc c ng-L ngy xa bit Tin Hc nn ni trc:"Mt hnh nh ng gi mt ngn ch". Ch word thi IBM gm c 4 bytes, nn mt mn nh ng gi 400 ngn ch, nh vy cc c khng chnh xc lm, nhng coi nh ng.

Nn nh rng cng mt graphic hin th trn hai mn nh c cng mn, th d nh 800x600, nhng kch thc khc nhau, th d nh 14 inches v 17 inches, th d nhin hnh trn mn nh 17 inches s ln hn, nhng n vn c cng mt s pixels, c iu pixel ca n ln hn pixel ca mn nh 14 inches.

Ni mt cch khc, nu ta dng mn nh ln hn th graphic s ln hn nhng khng c ngha l n r hn. Mun thy r chi tit, ta phi lm cho graphic c mn cao hn. Tr li cu chuyn Th Vn Hi Moscow, mun c hnh r hn, th trong cng mt din tch, ta phi nh b con ngi xch li gn nhau khong t cha nhiu ngi hn v mi ngi cm mt tm ct-tng nh hn.

Ta thay i Display Properties ca mt mn nh bng cch right click ln desktop ri select Properties, k click Tab Settings ri chn Screen resolution v Color quality ging nh hnh di y:

Khi ta tng mn ca mn nh, cc hnh nh s nh li v kch thc ca pixel c thu nh li. Do , ta c th cho hin th nhiu th hn trn desktop. Phm cht ca cc graphic vn khng thay i, mc du hnh nh hn. Nh l mun hnh r hn th khi cu to v cha graphic, ta phi dng mt mn cao. Ging nh khi chp hnh, mun hnh p ta cn ci my chp hnh dng phim ln ca th chuyn nghip v focus k lng, thay v dng my r tin t ng, ch a ln l bm chp c.

Mu (color)Khi ta dng ch c mt bit (ch c tr s 0 hay 1) cho mi pixel th ta ch c trng hay en. Lc y ta c th dng mt byte (8 bits) cho 8 pixels. Du vy, nu mn ca graphic cao , th hnh cng p. Th xem cc tuyt tc photos trng en ca Cao m, Cao Lnh th bit. Cc my Fax dng nguyn tc scan hnh giy c A4 ra thnh nhng pixels trng en ri gi qua ng dy in thoi qua u kia ti to li hnh t nhng d kin pixels.

Visual Basic 6 cho ta ch nh mt con s vo mi mu VB c th hin th, hay chn trc tip mt mu t Dialog. C bn cch:

Bn ch nh trc tip mt con s hay chn mt mu t ci Palette.

Bn chn mt trong cc hng s nh ngha sn trong VB, gi l intrinsic color constants (intrinsic c ngha nm na l cy nh l vn hay in-built), chng hn nh vbRed , vbBlue. Danh sch ca intrinsic color constants ly t VB6 online help c lit k di y:

Dng Function QBColor chn mt trong 16 mu. Function QBColor xut pht t thi Quick Basic (QBasic) ca Microsoft. QBsic l tin thn ca Visual Basic. Trong QBasic bn c th dng cc con s 1,2,3 .. ch nh cc mu Blue, Green, Cyan , .v.v..Function QBColor gin tin ha cch dng mu, user khng cn phi bn tm v cch trn ba th mu cn bn Red, Green, Blue. Bn vit code mt cch n gin nh:

Di y l tr s cc mu ta c th dng vi Function QBColor.

Tr sMuTr sMu

0Black8Gray

1Blue9Light Blue

2Green10Light Green

3Cyan11Light Cyan

4Red12Light Red

5Magenta13Light Magenta

6Yellow14Light Yellow

7White15Bright White

Dng Function RGB trn ba mu Red, Green v Blue. Trong ci bng lit k cc intrinsic color constants pha trn, nu bn s thy vbWhite(0xFFFFFF) l tng s ca vbRed(0x0000FF), vbGreen(0x00FF00) v vbBlue(0xFF0000). Mt mu c biu din bng s pha trn ca ba thnh phn mu cn bn, mi mu bng mt byte c tr s t 0 n 255. 0 l khng dng mu y, 255 l dng ti a mu y.

H thng s ta dng hng ngy l Thp Phn. Tr s 0xFF ca vbRed l con s 255 vit di dng Thp lc phn (Hexadecimal hay Hex cho gn v y c nh du bng 0x trc con s phn bit vi s Thp phn). Trong h thng s Hex ta m t 0 n 9 ri A,B,C,D,E,F ri qua s hng thp lc 10, 11,.., 19, 1A, 1B, ..1E,1F,20,21..v.v. Tc l thay v ch dng 10 symbols t 0 n 9 trong Thp phn, ta dng 16 symbols t 0 n F. Mun bit thm v h thng s Hex hy c bi C s Nh Phn.

Trong hnh di y l mt th d cho thy mu xanh nht c chn gm ba thnh phn Blue(0x990000= 153*256*256), Green(0xCC00= 204*256) v Red(0xFF= 255):

Ghi ch: Bn c th dng Windows Calculator hon chuyn s gia cc dng Decimal, Binary v Hexadecimal. Chn View|Scientific thay v View|Standard.

Function RGB p dng Function RGB, ta s vit mt chng trnh VB6. Bn hy khi ng mt chng trnh VB6 mi, b vo mt Label tn Label1 vi Caption Red v mt Vertical Scroll tn VScroll1. K select c hai Label1 v VScroll1 ri Copy v Paste hai ln l thm hai cp. i Caption ca hai Label mi ny ra Green v Blue. By gi ta c mt Array ba Vertical Scrolls cng tn VScroll1, vi index l 0,1 v 2.

t mt PictureBox tn picColor vo bn phi ba ci VScrolls. Thm mt Label pha di, t tn n l lblRGBValue, nh clear caption ca n, ng c ch Label1 nh di y:

By gi select c ba VScrolls v edit value ca property Max trong ca s Properties thnh 255, ni khi ko ci bar ca mt VScroll1 ln xung ta gii hn tr s ca n t Min l 0 n Max l 255.

Chuyn chnh ta phi lm l vit code x l Event Change ca cc VScrolls. V chng l mt Array nn ta c th dng mt Sub duy nht handle events n t c ba VScrolls. Mi lc mt trong 3 VScrolls thay i tr s ta s trn ba mu Red, Green, Blue biu din bi tr s ca 3 VScrolls thnh mu BackColor ca PictureBox picColor. ng thi ta cho hin th tr s ca ba thnh phn mu Red, Green v Blue trong Label lblRGBValue. Bn hy double click ln mt trong 3 VScrolls ri vit code nh sau:

Private Sub VScroll1_Change(Index As Integer)

' Use Function RGB to mix 3 colors VScroll1(0) for Red, ' VScroll1(1) for Green and VScroll1(2) for Blue ' and assign the result to BackColor of PictureBox picColor picColor.BackColor = RGB(VScroll1(0).Value, VScroll1(1).Value, VScroll1(2).Value)

' Variable used to prepare display string Dim strRGB As String

' Description of what is displayed strRGB = "picColor.BackColor = RGB(Red, Green, Blue) " & vbCrLf

' Values of Red, Green, Blue in Decimal strRGB = strRGB & " Decimal: " & VScroll1(0).Value & ", " & VScroll1(1).Value & ", " & VScroll1(2).Value & vbCrLf

' Values of Red, Green, Blue in Hexadecimal strRGB = strRGB & " Hex: 0x" & Hex(VScroll1(0).Value) & ", 0x" & Hex(VScroll1(1).Value) & ", 0x" & Hex(VScroll1(2).Value)

' Assign the resultant string to caption of Label lblRGBValue lblRGBValue.Caption = strRGB

End Sub

Bn hy khi ng chng trnh ri nm cc bar ca 3 VScrolls ko ln, ko xung xem kt qu. Ca s ca chng trnh s c dng ging nh di y:

Color MappingNu dng Hex Calculator i con s 0xFFFFFF ra decimal ta s c 16777215, nu k c s 0 ta s c tng cng 16777216 mu. Lc ny ta bn v 8bit (1 byte) v 16bit (2 bytes) color, nhng y ta ni chuyn 3 byte color. Nh th c th mn nh khng kh nng cung cp mi mu m Function RGB tnh ra. Vy VGA card s lm sao?

Th d mt ct VGA ch h tr n 8 bits. N s cung cp 256 mu khc nhau. Nu Function RGB i hi mt mu m VGA card c th cung cp chnh xc th tt, nu khng n s tm cch dng hai hay ba m gn nhau trn mu v cho ta o tng mu ta mun. Cng tc ny c gi l Color Mapping v ci mu c lm ra c gi l custom color.

Dng Intrinsic Color ConstantsMt trong nhng features ca MSWindows l cho ta chn Color Scheme ca Windows theo s thch. Bnh thng, Color Scheme ca Windows l Blue, nhng ta c th chn Olive Green hay Silver, nu ta mun.

Ch kh ni nu ta dng mt mu m hin th tuyt p th g trong chng trnh VB6 m by gi user t nhin thay i Color Scheme thnh Olive Green chng hn khin cho mu m y coi chng ging ai trong ci Color Scheme mi.

trnh trng hp y, thay v ni thng ra l mu g (xanh hay ) ta ni dng mu vbActiveTitlebar hay vbDesktop, .v.v.Dng Intrinsic Color Constant s bo m mu ta dng s c bin i theo Color Scheme m user chn khi b trng hp ci mu tr nn chng ging ai. Lc thit k, ta cng c th c