5. Visual BasicªºÀ³¥Îµ{¦¡»P¨ç¦¡®w
±±¨î¬yµ{
¡@
©Ò¦³ªºµ{¦¡»y¨¥³£¥²¶·´£¨Ñ¤@Ó¥H¤Wªº¤è¦¡¨Ó°õ¦æ¤@¨Ç«D´`§Çªº±Ôz¡C°£¤F©I¥s°Æµ{¦¡©M¨ç¦¡¥~¡A©Ò¦³°ò¥»±±¨î¬yµ{±Ôz¥i¤À¦¨¡G¤À¤ä©M°j°é±Ôz¡C
¤À¤ä±Ôz
¡@
¥Dnªº¤À¤ä±Ôz¬°If...Else...Else If...End If°Ï¶ô¡CVisual Basic¤ä´©¼ÆºØ³oÃþªº±Ôz¡A¥]¬A³æ¦æ©M¦h¦æªºª©¥»¡G
' Single line version, without Else clause If x > 0 Then y = x ' Single line version, with Else clause If x > 0 Then y = x Else y = 0 ' Single line, but with multiple statements separated by colons If x > 0 Then y = x: x = 0 Else y = 0 ' Multiline version of the above code (more readable) If x > 0 Then y = x x = 0 Else y = 0 End If ' An example of If..ElseIf..Else block If x > 0 Then y = x ElseIf x < 0 Then y = x * x Else ' X is surely 0, no need to actually test it. x = -1 End If
·íIF«á±ªº«D¹sȦ¨¥ß®É¡A·|°õ¦æThen°Ï¶ô¡G
' The following lines are equivalent. If value <> 0 Then Print "Non Zero" If value Then Print "Non Zero"
ÁöµM«á±¥i¸`¬Ù¨Ç¥´¦r¥\¤Ò¡A¦ý¤£¸Ó»{¬°¦p¦¹·|Åýµ{¦¡§ó¬°§Ö³t¡A¦Ü¤Ö¤£¤@©w·|¡CBenchmarkÅã¥ÜYÅܼƫ¬ºA¬°Boolean¡AInteger©ÎLong¡A«h¸ûµuªº¤è¦¡¨Ã¤£·|¨Ïµ{¦¡°õ¦æ±o§ó§Ö³t¡CµM¦Ó¡AY¬°¨ä¥L¼ÆÈ«¬ºA¡A³t²v·|¦³¬ù¦Ê¤À¤§¤G¤Q©Î§ó¤Öªº¼W¥[¡C¦pªG¨Ï¥Î³oÓ§Þ¥©Åý±z·P¨ìµÎªAªº¸Ü¡A¨º´N¥h°µ§a¡C¦ý¥²¶·ÁA¸Ñ¦b³\¦hӮפW¡A¼W¶i³t²v¤£È±o´î¤Öµ{¦¡ªº¥iŪ©Ê¡C
µ²¦X³\¦hAND»POR¹Bºâ¤¸¨Ï±o³\¦h¶i¶¥ªº³Ì¨Î¤Æªº§Þ³N¬O¥i¦æªº¡C¤U±ªº¨Ò¤lÅã¥Ü¥iÂǥѧï¼g¥¬ªL¹Bºâ¦¡¨Ó¼¶¼g§ó²©ú¦³®Äªºµ{¦¡¡G
' If two numbers are both zero, you can apply the OR operator ' to their bits and you still have zero. If x = 0 And y = 0 Then ... If (x Or y) = 0 Then ... ' If either value is <>0, you can apply the OR operator ' to their bits and you surely have a nonzero value. If x <> 0 Or y <> 0 Then ... If (x Or y) Then ... ' If two integer numbers have opposite signs, applying the XOR ' operator to them yields a result that has the sign ' bit set. (In other words, it is a negative value.) If (x < 0 And y >= 0) Or (x >= 0 And y < 0) Then ... If (x Xor y) < 0 Then ...
¦bµ{¦¡½X¤¤¹B§@Boolean¹Bºâ¤l¥B¤£·V¤Þ¤J¤£ª¾¤£Ä±ªº¿ù»~®É¡A±`¥O§ÚÌ·P¨ì¼««ë¤£¤w¡C¨Ò¦p¡A§A¥i¯à·|²q·Q¤U±¨â¦æªºµ{¦¡½X¬O¬Ûµ¥ªº¡A¨Æ¹ê¤£µM¡C¡]n¤F¸Ñ¬°¤°»ò¡A·Q·Q¼Æ¦r¦b¤G¶i¦ì¤¤¦p¦óªí¥Ü¡C¡^
' Not equivalent: just try with x=3 and y=4, whose binary ' representations are 0011 and 0100 respectively. If x <> 0 And y <> 0 Then ... If (x And y) Then ... ' Anyway, you can partially optimize the first line as follows: If (x <> 0) And y Then ...
NOT¹Bºâ¤l¬O¥t¤@Ó±`¥Îªº¹Bºâ¤l¡A¥¦¥i¥H¤ÏÂà¼Æ¦r¸Ìªº©Ò¦³bits¡]§Y0ÅÜ1¡A1ÅÜ0¡^¡C¦bVisual Basic¸Ì¡A¥u¦b¨ä°Ñ¼Æ¬°True¡]-1¡^ªº±¡ªp¤U¡A¦¹¹Bºâ¤l¤~¦^¶ÇFalse¡C¦]¦¹°£¤F¤ñ¸ûBooleanµ²ªG©Î¬OBooleanÅܼƥ~¡A§AÀ³±q¤£¨Ï¥Î¥¦¡C
If Not (x = y) Then ... ' The same as x<>y If Not x Then ... ' The same as x<>-1, don't use instead of x=0
Y·Q¨ú±o§ó¦h¬ÛÃö¸ê°T¡A¥i¬Ý¦¹³¹«á±ªº,
"Boolean»PBit-Wise¹Bºâ¤l" ³æ¤¸¡C³\¦h¥Ñ¨ä¥¦µ{¦¡»y¨¥Âà´«¨ìVisual Basicªºµ{¦¡³]pªÌ·|Åå³YIf±Ôz¥y¨Ã¤£¤ä´©©Ò¿×ªºµu¸ôµû¦ô¡]short-circuit evaluation¡^¡C´«¨¥¤§¡AVisual Basic§Y¨Ï¦³¨¬°÷ªº¸ê°T¥i¨M©wµ²ªG¬O°²©Î¯u¡A¥¦¤´Á`¬O·|°õ¦æIf¤l¥yªº¥þ³¡¹Bºâ¦¡¡A¦p¤U©Ò¥Ü¡G
' If x<=0, it makes no sense to evaluate Sqr(y)>x ' because the entire expression is guaranteed to be False. If x > 0 And Sqr(y) < z Then z = 0 ' If x=0, it makes no sense to evaluate x*y>100. ' because the entire expression is guaranteed to be True. If x = 0 Or x * y > 100 Then z = 0
ÁöµMVisual Basic¤£°÷Áo©ú¡A¤£·|¦Û°Ê¦a³Ì¨Î¤Æ¹Bºâ¦¡¡A¨Ã¤£ªí¥ÜµLªk¤â°Ê¥h°µ¡C¥i¥H°Ñ·Ó¤U±ªº±Ôz¥y¨Ó§ï¼g¤W±ªºIf±Ôz¥y¡G
If x > 0 Then If Sqr(y) < z Then z = 0
¥i°Ñ·Ó¤U±¥h§ï¼g¤W±²Ä¤GÓIf±Ôz¥y¡G
If x = 0 Then z = 0 ElseIf x * y > 100 Then z = 0 End If
¤ñ°_If¦Ó¨¥¡ASelect Case±Ôz¥y¥u¯à¦³¤@Ó´ú¸Õ¹Bºâ¦¡¡G
Select Case Mid$(Text, i, 1) Case "0" To "9" ' It's a digit. Case "A" To "Z", "a" To "z" ' It's a letter. Case ".", ",", " ", ";", ":", "?" ' It's a punctuation symbol or a space. Case Else ' It's something else. End Select
¹ï©óSelect Case³Ì¨Î¤Æªº§Þ³N¤j¦h¬O§â±`¥Î¨ìªº±¡ªp²¾¨ì°Ï¶ôªº³Ì³»ºÝ¡C¨Ò¦p¡G¦b¥ý«e¨Ò¤l¤¤¡A¥i¯à·Q¦b´ú¸Õ¬O§_¬°¼Æ¦r«e´ú¸Õ¨ä¬O§_¬°¦r¥À¡C°²Y¥¿¤å¥]§tªº¤å¦r¤ñ¼Æ¦r¦hªº¸Ü¡A³o¼Ëªº§ïÅÜ·|¥[§Öµ{¦¡¡C
¥X¥G·N®Æªº¡ASelect Case°Ï¶ô¾Ö¦³¤@Ó¦³½ìªº¯SÂI¡A³o¦bÆF¬¡ªºIf±Ôz¥y¤¤¬O¯Ê¥Fªº-¨ä¥i°õ¦æµu¸ôµû¦ô¡C¨Æ¹ê¤W¡ACase¤l¹Bºâ¦¡·|³Q°õ¦æªº±¡ªp¥u¦b¦^¶ÇȬOTrue®É¡A¤§«á¨ä¥¦¦b¦P¤@¦æ¤W¨ä¾lªº¹Bºâ¦¡³£·|³Q¸õ¹L¡C¨Ò¦p¡A¦b¥ý«eªºµ{¦¡½X¤ù¬q¤¤´ú¸Õ¼ÐÂI²Å¸¹ªºCase»y¥y¡A¦pªGŪ¨ìªº¦r¤¸¬O"."¡A«h¨ä¥¦©Ò¦³¦b¸Ó¦æªº´ú¸Õ´N¤£·|³Q°õ¦æ¨ì¡C¥i§Q¥Î³oÓ¦³½ìªº¯SÂI§ï¼g¡]¥B³Ì¨Î¤Æ¡^¬Y¨Ç¥Ñ¦h¼ËBoolean¤l¹Bºâ¦¡©Ò²Õ¦¨ªº½ÆÂøIf±Ôz¥y¡C
' This series of subexpressions connected by the AND operator: If x > 0 And Sqr(y) > x And Log(x) < z Then z = 0 ' can be rewritten as: Select Case False Case x > 0, Sqr(y) > x, Log(x) < z ' Do nothing if any of the above meets the condition, ' that is, is False. Case Else ' This is executed only if all the above are True. z = 0 End Select ' This series of subexpressions connected by the OR operator: If x = 0 Or y < x ^ 2 Or x * y = 100 Then z = 0 ' can be rewritten as: Select Case True Case x = 0, y < x ^ 2, x * y = 100 ' This is executed as soon as one of the above is found ' to be True. z = 0 End Select
¦]¬°¦P¼Ë¬O«D¥¿²Î¤Æªº³Ì¨Î¤Æ§Þ¥©¡A«Øij¬°¦Û¤vªºµ{¦¡»{¯uªº¼Ð¤Wµù¸Ñ¡A»¡©ú§A°µªº¨Æ¡A¨ÃÁ`¬O§âì©lIf±Ôz¥y§@¬°µù¸Ñ¡C³o§Þ¥©¹ï©ó¥[§Öµ{¦¡½Xªº³t«×¬O«Ü¦³®Äªº¡A¦ý¨M¤£n§Ñ°O³Ì¨Î¤Æ¨Ã¤£¬O°ß¤@«nªº¨Æ¡A¦pªG§A§Ñ°O´¿¸g¼g¹Lªºì©lµ{¦¡½X©ÎÅýµ{¦¡½X¬Ý°_¨Ó«ÜÃøÀ´¡A±N·|ÅýºûÅ@¥¦ªº¦P¨ÆÌı±o«ÜÀY¯k¡C
±µµÛ»¡©úGoTo±Ôz¥y¡A³\¦h¤Hµø¨ä¬°½E¬Ì¯ë¦a·q¦Ó»·¤§¡C¦ýµ§ªÌ¥²¶·©Ó»{¡A¤£ºÞ«ç¼Ë¡A¹ï©ó³o¥|Ó¦r¨Ã¤£¨º»òªº§_©w¡C¨Æ¹ê¤W¡A¤´¥i¨Ï¥Î¤@ÓGoTo±Ôz¥y¸õ¥X¤@³s¦êªº±_ª¬°j°é¡A¨ú¥N³sÄòªºExit Do©ÎExit For±Ôz¥y¡C«Øij¡G¨Ï¥ÎGoTo±Ôz¥y¹ï©ó¥¿³W¬yµ{¦Ó¨¥´N¹³¬OÓ¨Ò¥~ªº¨Æª«¯ë¡A¥BÁ`¬O¦bµ{¦¡½X¤¤¡A¥Î¦³·N¸qªº¼ÐÅÒ¦WºÙ¤Î¦³·N¸qªº¨¥Ãã¨Ó¸ÑÄÀ©Ò°µªº¨Æ¡C
GoSub...Return¤ñ°_GoTo¦Ó¨¥¬O¸û¦n¨Ç¡A¦]¬°¸û¦³µ²ºc¡C¦b¬Y¨Ç±¡ªp¤U¡A¨Ï¥ÎGoSub©I¥s²{¦æµ{§Ç¤º³¡ªº¤@¬qµ{¦¡¤ñ°_©I¥s¥~³¡ªºSub©ÎFunctionn¨Óªº¦n¡C¬J¤£¥Î¶Ç»¼°Ñ¼Æ¤]¤£¥Î±µ¦¬¦^¶ÇÈ¡F¦ý¥t¤@¤è±¡A³Q©I¥sªºµ{¦¡¦@¥Î©Ò¦³ªº°Ñ¼Æ»P²{¦æµ{§Ç¤¤ªº°Ï°ìÅܼơA©Ò¥H¦b¤j³¡¤Àªº±¡ªp¤U¡A¤£¶·n¶Ç»¼¥ô¦óªF¦è¡CµM¦Ó§A±oª¾¹D¡A·í½sĶ¦¨°õ¦æ½X®É¡AGoSub¤j¬ù¤ñ¦b¦P¤@Ó¼Ò²Õ¤ºªº¥~³¡¨ç¦¡©I¥snºC6¨ì7¿¡A©Ò¥H¦pªG¥¿¦b¼¶¼g¹ï®É¶¡«Üp¸ûªºµ{¦¡½X®É¡AÁ`¬On¤ñ¸û³o¨âºØ¤èªk¡C
°j°é±Ôz
¡@
¦bVisual Basic¤¤¡A²@µLºÃ°ÝªºFor...Next°j°é¬O§Ú̳̱`¥Îªº°j°éµ²ºc¡C
For counter = startvalue To endvalue [Step increment] ' Statements to be executed in the loop... Next
¦pªGincrement¤£µ¥©ó¤@¡A´N¦³¥²n¸Ô²Ó«ü©úStep¤l¥y¡C¥i¥ÎExit For±Ôz¥yÂ÷¶}°j°é¡A¦ý¤£©¯¦a¡AVisual Basic¨Ã¤£´£¨Ñ¥ô¦ó¥i¸õ¹L²{¦æ±Ôz«áªº³¡¤À¡A¥B«·s¶}©l°j°éªº¡u«½Æ¡v«ü©w¡C³Ì¦nªº¤èªk¬O¨Ï¥Î¡]±_ª¬¡^ If±Ôz¥y¡A©Î¦pªG¤£·QÅýÅÞ¿è¤Ó¹L½ÆÂøªº¸Ü¡A¥i¨Ï¥ÎGoTo«ü¦V°j°éªº³Ì¥½ºÝ¡C¨Æ¹ê¤W¡A³o¬O³æ¤@GoTo±Ôz¥y¥i¼W¥[µ{¦¡ªº¥iŪ©Ê»P¥iºûÅ@©Êªº«Ü¤Ö¼Æ®É¾÷¤§¤@¡G
For counter = 1 To 100 ' Do your stuff here ... ' if you want to skip over what follows, just GoTo NextLoop. If Err Then Goto NextLoop ' more code that you don't want to enclose within nested IF blocks ' ... NextLoop: Next
¤p¯µ³Z
¦pªG¦³¼W¥[¤@Ó¯BÂIÅܼƪº»Ýn®É¡AÁ`¬O¨Ï¥ÎInteger©ÎLongÅܼƧ@¬°For...Next°j°é¤ºªº±±¨îÅܼơA¦]¬°¨ä¤ñSingle©ÎDouble±±¨îÅܼƨӱo§Ö¦h¤F¡C¤UÓ¨Ò¤l·|¸ÑÄÀ³Ì¦³®Äªº§Þ³N¡C
ª`·N
ÁקK¨Ï¥Î¯BÂIÅܼƧ@¬°For...Next°j°éªº±±¨îÅܼƪº¤@Ó²z¥Ñ¬O¦]¬°¤p¼ÆÂI¿ù»~¡C·í¼W¥[¶q¬O¤À¼Æ®É¡AµLªk½T«O¼W¥[ªº¯BÂIÅܼƬOµL»~ªº¡A¦Ó¥B°j°é°õ¦æ¦¹¼Æ¥i¯à¤ñ¹w´Án¨Ó±o¤Ö©Î§ó¦h¡G
Dim d As Single, count As Long For d = 0 To 1 Step 0.1 count = count + 1 Next Print count ' Displays "10" but should be "11"
·ín§¹¥þ¦a½T«O°j°éªº°õ¦æ¦¸¼Æ®É¡A¨Ï¥Î¾ã¼Æ±±¨îÅܼơA¨Ã©ú½T¦a¼W¥[°j°é¤ºªº¯BÂIÅܼơG
Dim d As Single, count As Long ' Scale start and end values by a factor of 10 ' so that you can use integers to control the loop. For count = 0 To 10 ' Do what you want with the D variable, and then increment it ' to be ready for the next iteration of the loop. d = d + 0.1 Next
µ§ªÌ¤w¦b²Ä¥|³¹¦³´£¨ìFor Each...Next°j°é¡A¦b³o¸Ì¤£¦A«½Æ¨ä¸Ô²Ó¤º®e¡C¥u·Q®i¥Ü¤@Ó¦b¦¹Ãþ°j°é»PArray¨ç¦¡¤Uªº§Þ¥©¡C¦¹§Þ¥©¤¹³\°õ¦æ¤@Ó¦³¤£¦P±±¨îÅܼƪº°Ï¶ô±Ôz¡A¥B¨ä¤£»Ý¦³¶¶§Ç¡G
' Test if Number can be divided by any of the first 10 prime numbers. Dim var As Variant, NotPrime As Boolean For Each var In Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) If (Number Mod var) = 0 Then NotPrime = True: Exit For Next
¦¹È¨Ã¤£¤@©w±o¬O¼ÆÈ¡G
' Test if SourceString contains the strings "one", "two", "three", etc. Dim var2 As Variant, MatchFound As Boolean For Each var2 In Array("one", "two", "three", "four", "five") If InStr(1, SourceString, var2, vbTextCompare) Then MatchFound = True: Exit For End If Next
Do...Loopµ²ºc¸ûFor.. .Next°j°é¦³¼u©Ê¡A¦]¬°¦b°j°éªº°_ºÝ©Î¥½ºÝ¥i©ñ¸m²×¤î´ú¸Õ¡C¡]¹ï«á±¦Ó¨¥¡A°j°é¦Ü¤Ö·|³Q°õ¦æ¤@¦¸¡C¡^¤]¥i¥H¨Ï¥ÎWhile¤l¥y¡]·í´ú¸Õ±ø¥ó¬OTrue«h«½Æ¡^©ÎUntil¤l¥y¡]·í´ú¸Õ±ø¥ó¬OFalse«h«½Æ¡^¡C¥iÀH®É°õ¦æExit Do±Ôz¡AÂ÷¶}Do°j°é¡A¦ý¦p¦PFor...Next°j°é-VBA¨Ã¤£´£¨Ñ¥i¸õÂ÷°j°é³Ñ¾lªº±Ôz¥y¦Óª½±µ«·s¶}©l°j°éªºÃöÁä¦r¡C
' Example of a Do loop with test condition at the top. ' This loop is never executed if x <= 0. Do While x > 0 y = y + 1 x = x \ 2 Loop ' Example of a Do loop with test condition at the bottom. ' This loop is always executed at least once, even if x <= 0. Do y = y + 1 x = x \ 2 Loop Until x <= 0 ' Endless loop: requires an Exit Do statement to get out. Do ... Loop
While...Wend°j°é¦b·§©À¤W¬Ûªñ©óDo While...Loop¡C¦ý¥u¤¹³\¦b°j°éªº°_ºÝ´ú¸Õ±ø¥ó¡A¨Ã¨S¦³Until¤l¥y¡A¥B¤]¨S¦³Exit... While«ü¥O¡C¦]¦¹¡A¤j³¡¤Àªºµ{¦¡³]p®v¹çÄ@¿ï¾Ü¸û¦³¼u©ÊªºDo... Loopµ²ºc¡A¨Æ¹ê¤W¡A¥»®Ñ±z±N¬Ý¤£¨ì¤@ÓWhile...Wend°j°é¡C
¨ä¥L¨ç¦¡
¡@
´XÓVBA¨ç¦¡±µªñ©ó±±¨î¬yµ{¡AÁöµM¥¦Ì¥»¨¨Ã¤£·|§ïÅÜ°õ¦æ¬yµ{¡C¨Ò¦p¡A Iif¨ç¦¡¡A¥i¨ú¥NIf...Else...End If°Ï¶ô¡A¦p¤U©Ò¥Ü¡G
' These lines are equivalent. If x > 0 Then y = 10 Else y = 20 y = IIf(x > 0, 10, 20)
Choose¨ç¦¡¥i¿ï¾Ü¸s²Õ¤¤ªº¤@ÓÈ¡F¥i¥Î¨ÓÃѧO¤TөΧó¦h¨Ò¤l¡C©Ò¥H¥i¥N´Àµ{¦¡½X¡G
' The classic three-choices selection If x > y Then Print "X greater than Y" ElseIf x < y Then Print "X less than Y" Else Print "X equals Y" End If
¥i¨Ï¥Î³oÓ¸ûµuªºª©¥»¡G
' Shortened form, based on Sgn() and Choose() functions. ' Note how you keep the result of Sgn() in the range 1-3. Print "X " & Choose(Sgn(x _ y) + 2, "less than", "equals", _ "greater than") & " Y"
Switch¨ç¦¡±µ¨ü¤@¦ê¡]condition, value¡^È¡A¥B¦^¶Ç²Ä¤@Ócondition¬°True®É¬Û¹ï¸Óconditionªºvalue¡C¨Ò¦p¡A¥i¨Ï¥Î¦¹¨ç¦¡¨Ó¨ú¥NSelect Case°Ï¶ô¡G
Select Case x Case Is <= 10: y = 1 Case 11 To 100: y = 2 Case 101 To 1000: y = 3 Case Else: y = 4 End Select
¦bè¤~¨º¦æ²£¥Í¬Û¦Pªºµ²ªG¡C
' The last "True" expression replaces the "Else" clause. y = Switch(x <= 10, 1, x <= 100, 2, x <= 1000, 3, True, 4)
·í¨Ï¥Î¦¹¨ç¦¡®É¡An°O±o¨â¥ó¨Æ¡G²Ä¤@¡A¦pªG¨S¦³¹Bºâ¦¡¦^¶ÇTrueªºÈ¡A«hSwitch¨ç¦¡·|¦^¶ÇNull¡C²Ä¤G¡A§Y¨Ï¥u¦³¤@ÓȳQ¦^¶Ç¡A©Ò¦³¹Bºâ¦¡ÁÙ¬On³Q¨DÈ¡C¦]¦¹¡A¥i¯à±o¨ì·N·Q¤£¨ìªº¿ù»~©Î¤£§Æ±æ±o¨ìªº°Æ§@¥Î¡C¡]¨Ò¦p¡A°²³]¹Bºâ¦¡¤Þ°_·¸¦ì©Î°£¥H¹sªº¿ù»~¡C¡^
ª`·N
ÁöµMIIf¡BChoose¤ÎSwitch¨ç¦¡¦³®É¦³¯q©ó´î§Cµ{¦¡½X¼Æ¶q¡A¦ýnª¾¹D¥¦ÌÁ`¬O¸û¦Pµ¥ªºIf©ÎSelect Caseµ²ºcn¨Ó±oºC¡C¦]¦¹¡A¦b®É¶¡´N¬Oª÷¿úªº°j°é¤¤¤£¸Ó¨Ï¥Î¨ì¥¦Ì¡C
¼ÆȪº¾Þ§@
¡@
Visual Basic´£¨ÑÂ×´Iªº¼Æ¾Ç¹Bºâ¤¸¤Î¨ç¦¡¡C¨ä¤¤¤j³¡¤Àªº¹Bºâ¤¸·N¸q¤W¬O¡u¦h§Î¡vªº¡A¥¦Ì¥i¹B§@©ó¥ô¦ó«¬ºAªº°Ñ¼Æ¡A¥]¬AInteger¡BLong¡BSingle¡BDouble¡BDate»PCurrency¡C¨Ì¾a¯S©w¹Bºâ¤¸©Î¨ç¦¡¡AVisual Basic½sĶµ{¦¡¯à±N¹Bºâ¤lÂà´«¬°§ó¦X¾Aªº¸ê®Æ«¬ºA¡CµM¦Ó¡A³o¬O»y¨¥ªº¤u§@¡A¤£¥Î¦h¥[¾Þ¤ß¡A¦]¬°©Ò¦³ªº¨Æ±¡³£·|¬°§A¥ý¦Û°Ê³B²z¦n¡C
¼Æ¾Ç¹Bºâ¤¸
¡@
¦p§A©Òª¾¡AVisual Basic¤ä´©©Ò¦³¥|ӼƾǹBºâ¤¸¡C·í²Õ¦X¨âÓ¤£¦P«¬ºAªºÈ®É¡AVisual Basic·|¦Û°ÊÀ³¥Î±j¢«¬§OÂà´«¡A¨Ã§â¸û¯¶ªº«¬ºAÂà´«¦¨¸û¼sªº«¬ºA¡]¨Ò¦p¡AIntegerÂà´«¦¨Long©ÎªÌ¬OSingleÂনDouble¡^¡C¦³½ìªº¬O¡A°£ªk¹Bºâ¤¸¡]/¡^Á`¬O§â¥¦¨âÓ¹Bºâ¤lÂà´«¦¨Double¡A¨ä·|¾ÉP¬Y¨Ç·N·Q¤£¨ìªºª¬ªp¡C¦pªG§âInteger©ÎLongÈ®³¨Ó³Q¥t¤@ÓInteger©ÎLongÈ°µ°£ªk°Ê§@¡A¦Ó¥B¤£¹ï°Ó¼Æªº¤p¼Æ³¡¤À·P¿³½ìªº¸Ü¡A«hÀ³¨Ï¥Î°õ¦æ¸û§Öªº¾ã¼Æ°£ªk¡]\¡^¡G
Dim a As Long, b As Long, result As Long result = a / b ' Floating point division result = a \ b ' This is about 4 times faster.
Visual Basic¤]¤ä´©¦¸¤èªº¹Bºâ¤¸¡]^¡^¡A¥¦¥i¥H¨D¥X¤@ӼƦrªº¦¸¤èÈ¡C¬JµM³o¼Ë¡Aµ²ªGÁ`¬O¬°Double«¬ºA¡A§Y¨Ï¬O¨D¤@Ó¾ã¼Æªº¦¸¤èÈ¥çµM¡C¤@¯ë¦Ó¨¥¡A^¹Bºâ¤¸¬Û·íºC¡A©Ò¥H¹ï©ó¤p¦¸¤è¼Æ¦Ó¨¥¡A¥i¨Ï¥Î¼ªk¹Bºâªº¹Bºâ¦¡§@¬°´À¥N¡G
Dim x As Double, result As Double x = 1.2345 result = x ^ 3 result = x * x * x ' This is about 20 times faster.
MOD¹Bºâ¤¸±o¨ì¾ã¼ÆÈ°£ªk«áªº¾l¼Æ³¡¤À¡C¨ä±`¥Î©ó´ú¸Õ¬Y¼Æ¦r¬O§_¬°¥t¤@¼Æ¦rªº¿¼Æ¡C¦¹¹Bºâ¤¸«D±`¦³®Ä²v¦ý«o¦³¨î¡G¨ä¹Bºâ¤l«¬ºA³QÂà´«¬°Long¡A¦]¦ÓµLªk¥Î¦b«D±`¤jªº¼ÆȤW¡C¨ä¤]ºI±¼¥ô¦ó¤p¼Æ³¡¤À¡C³o¸Ì¦³Ó³B²z¥ô·NDoubleȪº¨ç¦¡¡G
Function FPMod(ByVal Number As Double, ByVal divisor As Double) As Double ' Note: this differs from MOD when Number is negative. FPMod = Number - Int(Number / divisor) * divisor End Function
·í³B²z¼ÆȮɡA¦³´XÓ±`³Q¥Î¨ìªº¨ç¦¡¡G
Function Log10(Number As Double) As Double Log10 = Log(Number) / 2.30258509299405 End Function
¤ñ¸û¹Bºâ¤¸
¡@
Visual Basic¤ä´©¤»Ó¤ñ¸û¹Bºâ¤¸¡A¥iÀ³¥Î¦b¼ÆȤΦr¦ê«¬ºA¤W¡G
= < <= > >= <>
³o¨Ç¹Bºâ¤¸±`³Q¨Ï¥Î¦bIf°Ï¶ô¡A¦ýn°O¦í¥¦Ì¦b·§©À¤W¬O¤£¦P©ó¥ô¦ó¨ä¥¦¼Æ¾Ç¹Bºâ¤¸ªº¡A·N¸q¤W¥¦Ì±µ¨ü¨âÓ«¬ºA¥B·|¶Ç°eµ²ªG¡C³o¼Ëªºµ²ªG·|¬OFalse¡]0¡^©ÎTrue¡]-1¡^¡C¦³®É¥i§Q¥Î³oÓÆ[ÂI¨Ó¼¶¼g§ó²©úªºµ{¦¡½X¡A¦p¤U©Ò¥Ü¡G
' The following lines are equivalent. If x > y Then x = x _ 1 x = x + (x > y)
ª`·N
·í¦bSingle¤ÎDoubleȤ¤¨Ï¥Î=¹Bºâ¤¸®ÉÁ`¬OnÂÔ·V¤p¤ß¡A¦]¬°·í¦b¯BÂI¼ÆȤW¹Bºâ®É¡AVisual Basic±`·|µo¥Í¤pªº´`Àô¤p¼Æ¿ù»~¡C¨Ò¦p¡A½Ð¬Ý¤U±ªºµ{¦¡½X¡G
Dim d As Double, i As Integer For i = 1 To 10: d = d + 0.1: Next Print d, (d = 1) ' Displays "1 False" !!!
«e±ªºµ²ªG¬Ý°_¨Ó¦n¹³¬O¤£¦X²zªº¡A¦]¬°ÅܼƦü¥G¥]¬A¥¿½TªºÈ¡A¦ý¨ä´ú¸Õ¡]d=1¡^¦^¶ÇFalse¡C¦]¬°¥¦¬O´`Àô¤p¼Æ¡A©Ò¥H¤£¸Ó«H¥ôVisual BasicªºPrint±ÔzÅã¥Üªº¸ê°T¡C¹ê»Ú¤W¡AdÅܼƪºÈ¬Oµy·L¤p©ó1ªºÈ¡Aºë½Tªº®t§O¦b1.11022302462516E-16¡]¦b¤p¼ÆÂI«á¸òµÛ15Ó¹s¡^¡A¥i¬O«o¨¬¥H³y¦¨µ¥¦¡´ú¸Õ¥¢±Ñ¡C«Øijµ´¤£n¦b¯BÂI¼ÆȤW°µ=ªº¹Bºâ¡C³o¨à¦³¸û¦nªº¤èªk¡G
' "equal" up to 10th decimal digit Function AlmostEqual(x, y) As Boolean AlmostEqual = (Abs(x - y) <= 0.0000000001) End Function
Boolean»PBit-Wiseªº¹Bºâ¤¸
¡@
Visual BasicªºÀ³¥Îµ{¦¡¤ä´©´XÓBoolean¹Bºâ¤¸¡A¯S§O¦³§U©ó¥Î¨Óµ²¦X¦h«Boolean¤l¹Bºâ¦¡¡C¨ä¤¤¸û³Q±`¨Ï¥Îªº¬OAND¡BOR¡BXOR¤ÎNOT¡C¨Ò¦p¡A¤U±ªºµ{¦¡½X§Q¥ÎBoolean¹Bºâ¤¸¨Ó½T©w¨âÅܼƪº¥¿t¸¹¡G
If (x > 0) And (y > 0) Then ' Both X and Y are positive. ElseIf (x = 0) Or (y = 0) Then ' Either X or Y (or both) are zero. ElseIf (x > 0) Xor (y > 0) Then ' Either X or Y (but not both of them) are positive. ElseIf Not (x > 0) Then ' X is not positive. End If
n°O±o¡A³o¨Ç¹Bºâ¤¸¹ê»Ú¤W¬Obit-wise¹Bºâ¤¸¡A¥¦Ì¥i¹ï¨CÓ¿W¥ßªº¦ì¤¸§@¥Î¡C¹ê»Ú¤W¡A¦pªG¹Bºâ¤l¤£¬OBooleanÈ¡]·N«ä¬O»¡¥¦ÌªºÈ¤£¬O-1»P0¡^¡A«h·|¦³®t§O¡C¥i¨Ï¥ÎAND¹Bºâ¤¸¨Ó´ú¸Õ¤@ӼƦrªº¤@Ó©Î¥H¤Wªº¦ì¤¸¡G
If (number And 1) Then Print "Bit 0 is set (number is an odd value)" If (number And 6) = 6 Then Print "Both bits 1 and 2 are set" If (number And 6) Then Print "Either bits 1 and 2, or both, are set"
¨Ï¥ÎOR¹Bºâ¤¸¨Ó³]©w¤@Ó©Î¥H¤Wªº¦ì¤¸¡G
number = number Or 4 ' Set bit 2. number = number Or (8 + 1) ' Set bits 3 and 0.
¥iµ²¦XAND»PNOT¹Bºâ¤¸¨Ó«·s³]©w¤@Ó©Î¥H¤Wªº¦ì¤¸¡G
Number = number And Not 4 ' Reset bit 2.
³Ì«á¡AXOR¹Bºâ¤¸¥i¤Á´«¤@Ó©Î¥H¤W¦ì¤¸ªºª¬ºA¡G
Number = number Xor 2 ' Flip the state of bit 1.
¦pªG¤£ª¾¹D¦b½sĶ®É¶¡þӦ줸¸Ó³Q³]©w¡B«³]¡A©Î¤Á´«¡A¥i¨Ï¥Îò»¦¸¹Bºâ¤¸¡A¦p¤U©Ò¥Ü¡G
Number = Number Or (2 ^ N) ' Set Nth bit (N in range 0-30).
¦¹¤èªk¦³¨âÓ¯ÊÂI¡G·íN¡×31®É¡A·|µo¥Í·¸¦ì¡A¥B¥Ñ©ó¤Ó¹L¨Ì¿à¯BÂI¹Bºâ¡A©Ò¥H®Ä²v«D±`®t¡C¥i¥Î¤U±¨ç¦¡¸Ñ¨M¨âÓ°ÝÃD¡G
Function Power2(ByVal exponent As Long) As Long Static result(0 To 31) As Long, i As Integer ' Evaluate all powers of 2 only once. If result(0) = 0 Then result(0) = 1 For i = 1 To 30 result(i) = result(i - 1) * 2 Next result(31) = &H80000000 ' This is a special value. End If Power2 = result(exponent) End Function
¤p¼Æ¤Æ»P¾ã¼Æ¤Æ
¡@
Int¨ç¦¡ºI¥h¤p¼Æ³¡¤À¦¨¬°¾ã¼ÆÈ¡A¥B¦¹¾ã¼Æȵ¥©ó©Î¤p©ó¨ä°Ñ¼Æ¡C©M¥u»¡¡uºI¥h¼Æ¦rªº¤p¼Æ³¡¤À¡v¬O¤£¦Pªº¡C¦pªG°Ñ¼Æ¬Otªº¡A«h®t§O´N§óÅã¦Ó©ö¨£¤F¡G
Print Int(1.2) ' Displays "1" Print Int(-1.2) ' Displays "-2"
Fix¨ç¦¡ºI¥h¼Æ¦rªº¤p¼Æ³¡¤À¡G
Print Fix(1.2) ' Displays "1" Print Fix(-1.2) ' Displays "-1"
Visual Basic 6¤Þ¤J·sªº¼Æ¾Ç¨ç¦¡¡ARound¡A¨ä¥i§â¼ÆȤp¼ÆÂI«áªº´X¦ì¼Æ«áºI±¼¡]¦pªG²Ä¤GӰѼƬٲ¤ªº¤Æ¡A´N¨ì³Ì±µªñªº¾ã¼Æ¡^¡G
Print Round(1.45) ' Displays "1" Print Round(1.55) ' Displays "2" Print Round(1.23456, 4) ' Displays "1.2346"
Round¦³Ó¥¼¤å¥ó¤Æªºªá©Û¡G·í¤p¼Æ³¡¤À«ê¦n¬O0.5®É¡AY¨ä¾ã¼Æ³¡¤À¬O©_¼Æ«h¶i¦ì¡AY¬°°¸¼Æ«h®ø¥h¡G
Print Round(1.5), Round(2.5) ' Both display "2".
³oӦ欰¬O¥²nªº¡A¥H«K©ó·í¶i¦æ²Îp®É¥iÁ׶}¿ù»~¡A©Ò¥H¨ä¤£¸Ó³Q»{¬°¬OÓ¿ù»~¡C
·í¨ú¤p¼Æ®É¡A¦³®É¥²¶·n½T©w¤j©ó©Îµ¥©ó°Ñ¼Æªº³Ì±µªñªº¾ã¼Æ¡A¦ýVisual Basic¯Ê¤ÖÃþ¦ü³o¼Ëªº¨ç¦¡¡C¥i¨Ï¥Î¤U±³o¬q²µuªºµ{¦¡¨Ó¹F¦¨³oÓ°ÝÃD¡G
Function Ceiling(number As Double) As Long Ceiling = -Int(-number) End Function
Âà´«¤£¦P¼Æ¦r°ò©³
¡@
VBA¤ä´©¤Q¶i¦ì¡B¤Q¤»¶i¦ì»P¤K¶i¦ì¼Æ¦r¨t²Î¡G
value = &H1234 ' The value 4660 as a hexadecimal constant value = &O11064 ' The same value as octal constant
¥i¨Ï¥ÎVal¨ç¦¡±N¥ô¦ó¤Q¤»¶i¦ì©Î¤K¶i¦ì¦r¦êÂà´«¦¨¤Q¶i¦ìÈ¡G
' If Text1 holds a hexadecimal value value = Val("&H" & Text1.Text)
¥i°µ¬Û¹ïªºÂà´«-¥Ñ¤Q¶i¦ì¨ì¤Q¤»¶i¦ì©Î¤K¶i¦ì-¨Ï¥ÎHex¤ÎOct¨ç¦¡¡G
Text1.Text = Hex$(value)
¯S§Oªº¬O¡AVisual Basic¨Ã¤£¥]¬A¥iÂà´«¦¨¤G¶i¦ì¼Æ¦r©Î¥Ñ¤G¶i¦ì¼Æ¦rÂà´«¥X¥hªº¨ç¦¡¡AÁöµM¨äÅãµM¤ñ¤K¶i¦ì§ó±`¨£¡C¥i¨Ï¥Î¤@¹ï«Ø¥ß¦bPower2¨ç¦¡¤Wªº¨ç¦¡§Y¥i¹F¨ì³oÓÂà´«¡AÃö©óPower2¨ç¦¡½Ð°Ñ¾\¥»³¹µy«eªº³¹¸`¡q
Boolean»PBit-Wise¹Bºâ¤¸ ¡r¡C' Convert from decimal to binary. Function Bin(ByVal value As Long) As String Dim result As String, exponent As Integer ' This is faster than creating the string by appending chars. result = String$(32, "0") Do If value And Power2(exponent) Then ' We found a bit that is set, clear it. Mid$(result, 32 - exponent, 1) = "1" value = value Xor Power2(exponent) End If exponent = exponent + 1 Loop While value Bin = Mid$(result, 33 - exponent) ' Drop leading zeros. End Function ' Convert from binary to decimal. Function BinToDec(value As String) As Long Dim result As Long, i As Integer, exponent As Integer For i = Len(value) To 1 Step -1 Select Case Asc(Mid$(value, i, 1)) Case 48 ' "0", do nothing. Case 49 ' "1", add the corresponding power of 2. result = result + Power2(exponent) Case Else Err.Raise 5 ' Invalid procedure call or argument End Select exponent = exponent + 1 Next BinToDec = result End Function
¼ÆȪº®æ¦¡¿ï¶µ
¡@
VBA»y¨¥ªº©Ò¦³ª©¥»¬Ò¥]§tFormat¨ç¦¡¡A¬OÓ±j¦Ó¦³¤Oªº¤u¨ã¡A¾A¦X§A¤j³¡¤Àªº®æ¦¡¤Æ»Ý¨D¡C¨ä»yªk¦³ÂI½ÆÂø¡G
result = Format(Expression, [Format], _ [FirstDayOfWeek As VbDayOfWeek = vbSunday], _ [FirstWeekOfYear As VbFirstWeekOfYear = vbFirstJan1])
©¯¹B¦a¡A¹ï©ó¤j³¡¤Àªº¤u§@¦Ó¨¥¡A«e¨âӰѼƴN°÷¥Îªº¡A°£«D¬On®æ¦¡¤Æ¤é´Á¡A¦b¥»³¹µy«á·|½Í¨ì¡C²{¦b§Ú´N¨Ó¤¶²ÐFormat¨ç¦¡ªº¦h¶µ¯S©Ê¡A¡CÁöµM§Ú²q§A¤w¬Ý¹LVisual Basic¤å¥ó¤ºªº¤º®e¤F¡C
·í®æ¦¡¤Æ¼ÆȮɡAFormat¨ç¦¡¤ä´©¡u«ü¦W®æ¦¡¡v»P¡u¦Ûq®æ¦¡¡v¡C«ü¦W®æ¦¡¥]§t¥H¤U¦r¦ê¡G General Number¡]¨S¦³¯S§Oªº®æ¦¡¡A¥²n®É¨Ï¥Î¬ì¾Ç°O¸¹ªk¡^¡ACurrency¡]³f¹ô²Å¸¹¡A¥H¤d¬°°Ï¤À¤l»P¤p¼ÆÂI«á¨â¦ì¡^¡AFixed¡]¤p¼ÆÂI«á¨â¦ì¡^¡AStandard¡]¥H¤d¬°°Ï¤À¤l»P¤p¼ÆÂI«á¨â¦ì¡^¡APercent¡]¦Ê¤À²v¡Aªþ¥[%²Å¸¹¡^¡AScientific¡]¬ì¾Ç°O¸¹ªk¡^¡AYes/No¡BTrue/False¡BOn/Off¡]¦pªG¬O0«h¬°False©ÎOff¡A§_«h¬°True©ÎOn¡^¡CFormat¬O¤@Ó¦a°ì¤Æªº¨ç¦¡¡A¥B·|¦Û°Ê¦b¾A·íªºª¬ªp¤U¨Ï¥Î³f¹ô¼Ð»x¡B¤d°Ï¤À¤l»P¤p¼ÆÂI¡C
¦pªG«ü¦W®æ¦¡µLªk§¹¦¨§Anªº¤u§@¡A¥i¨Ï¥Î¥Ñ¯S®í¦r¤¸²Õ¦¨ªº®æ¦¡¤Æ¦r¦ê¨Ó«Ø¥ß¦Ûq®æ¦¡¡C¡]¹ï©ó²Ó¸`»P³oÃþ®æ¦¡¤Æ¦r¤¸ªº·N¸q¡A½Ð°Ñ¾\Visual Basic¤å¥ó¡C¡^
' Decimal and thousand separators. (Format rounds its result.) Print Format(1234.567, "#,##0.00") ' "1,234.57" ' Percentage values Print Format(0.234, "#.#%") ' "23.4%" ' Scientific notation Print Format(12345.67, "#.###E+") ' "1.235E+4" Print Format(12345.67, "#.###E-") ' "1.235E4"
Format¨ç¦¡¯S§Oªº¦a¤è¬O¡A¦pªG¼ÆȬO¥¿¡Bt¡B0¡A©Î¬ONull®É¡A¬Ò¥i¤ä´©¤£¦Pªº®æ¦¡¤Æ¦r¦ê¡C¦b¦Ûq®æ¦¡¦r¦ê¤W¥i¨Ï¥Î¤À¸¹§@¬°¨C°Ï¬qªº°Ï¤À¡C¡]¥i«ü©w¤@Ó¡B¤GÓ¡B¤TÓ¡B©Î¥|Ó¤£¦Pªº°Ï¬q¡C¡^
' Two decimal digits for positive numbers, enclose negative numbers within ' a pair of parentheses, use a blank for zero, and "N/A" for Null values. Print Format(number, "##,###.00;(##,###.00); ;N/A")
Visual Basic 6¤Þ¤J¤TÓ·sªº®æ¦¡¤Æ¨ç¦¡µ¹¼ÆȨϥÎ-FormatNumber¡BFormatPercent¡A»PFormatCurrency¡ÐɥΦÛVBScript¡C¡]¥t¤TӨ禡-FormatDate¡BMonthName»PWeekdayName¡Ð¦b¥»³¹µy«áªº¡q³B²z¤é´Á¡r¤¤·|°µ¸ÑÄÀ¡C¡^³o¨Ç·sªº¨ç¦¡½Æ»s¤F³Ì±jall-in-oneªº®æ¦¡¤Æ¯à¤O¡A¦ý¨ä»yªk¦p¤U¦Cªºµ{¦¡½X¯ë§ó²L¦Ó©ö¨£¡C
Result = FormatNumber(expr, [DecDigits], [InclLeadingDigit], _ [UseParens], [GroupDigits] ) result = FormatPercent(expr, [DecDigits], [InclLeadingDigit], _ [UseParens], [GroupDigits] ) result = FormatCurrency(expr, [DecDigits], [InclLeadingDigit], _ [UseParens], [,GroupDigits] )
¦b©Ò¦³ªº±¡ªp¤U¡ADecDigits¬O§Anªº¤p¼Æ¦ì¼Æ¡]¹w³]ȬO2¡^¡FInclLeading»¡©ú¦b[-1,1]½d³ò¤ºªº¼ÆȬO§_¶}ÀYn¥X²{0¡FUseParensªí¥Üt¼Æ¬O§_n³Q¬A°_¨Ó¡FGroupDigits§iª¾¬O§_¨Ï¥Î¤d°Ï¤À¤l¡C³Ì«á¤TÓ«D¥²n°Ñ¼Æ¨C¤@Ó³£¥i¬°¡G0-vbFalse¡B-1-vbTrue©Î-2-vbUseDefault¡]¨Ì¾Ú¨Ï¥ÎªÌªº¦a°Ï¦Ó¹w³]¡^¡C¦pªG¬Ù²¤¡A«hªí¥Ü¬°vbUseDefault¡C
¶Ã¼Æ
¡@
¦³®É¡A»Ýn²£¥Í¤@өΦhӶüơC¹CÀ¸±`¨Ï¥Î¨ì¦¹¥\¯à¡A¦ý³o¥\¯à¤]À³¥Î¦b¥]§t¼ÒÀÀªº°Ó·~À³¥Î¤W¡CVisual Basic¥u´£¨Ñ¤@Ó±Ôz¥y¤Î¨ç¦¡¨Ó²£¥Í¶Ã¼Æ¡C¥i¨Ï¥ÎRandomize±Ôz¥yªì©l¤Æ¤º³¡¶Ã¼Æ²£¥Í¾¹ªººØ¤l¡C¥i¶Çµ¹¥L¤@Ó¼Æȧ@¬°¶Ã¼ÆºØ¤l¡FVisual Basic·|¦Û°Ê¨Ï¥ÎTimer¨ç¦¡¶Ç¦^ªºÈ¡G
Randomize 10
¨C¦¸©I¥sRnd¨ç¦¡¡A¨ä·|¦^¶Ç¤@ӶüơC¦¹¦^¶ÇÈÁ`¬O¤p©ó1¥B¤j©ó©Îµ¥©ó0¡A¦]¦¹¬°¤F±o¨ì·Qn½d³ò¤ºªº¼ÆÈ¡A¥²¶·¹ïµ²ªG¶i¦æ³B²z¡G
' Simple computerized dice Randomize For i = 1 To 10 Print Int(Rnd * 6) + 1 Next
¦³®É¡A¥i¯àn¤@ª½«½Æ¤@³s¦ê¬Û¦Pªº¶Ã¼Æ¡A¯S§O¬O¦b¹ïµ{¦¡½X¶i¦æ°£¿ù®É¡C¦ü¥G¥i¥H¬Û¦PºØ¤l©I¥sRandomize±Ôzªº¤èªk¦ÓÀò±o³o¼Ëªº§@¥Î¡A¦ý³o¬O¦æ¤£³qªº¡Cn«½Æ¬Û¦Pªº¶Ã¼Æ§Ç¦C¡A«h©I¥sRnd¨ç¼Æ®Éµ¹¤©¤@Ótȧ@¬°°Ñ¼Æ¡C
dummy = Rnd(-1) ' Initialize the seed. (No Randomize is needed!) For i = 1 To 10 ' This loop will always deliver the same Print Int(Rnd * 6) + 1 ' sequence of random numbers. Next
¤]¥i¥HÂǵ۶ǤJ0µ¹Rnd·í§@°Ñ¼Æ¨Ó«·s¸ü¤Jè²£¥Íªº¶Ã¼Æ¡C
¤@¯ë¶Ã¼Æªº±`¥Î§@¥Î¬°²£¥X¯S©w½d³ò¤º¼Æ¦rªºÅÜ´«¡GÁ|¨Ò¨Ó»¡¡A¦b¥d¤ù¹CÀ¸¤¤±N·|«Ü¦³¥Î¡C³o¸Ì¦³Ó²©ö¥B¦³®Äªº½d¨Ò¡A·|¦^¶Ç¤¶©ófirst©Mlast¤¤ªº©Ò¦³ªø¾ã¼Æ°}¦C¡A¨Ã¥H¶Ã¼Æ±Æ¦C¡G
Function RandomArray(first As Long, last As Long) As Long() Dim i As Long, j As Long, temp As Long ReDim result(first To last) As Long ' Initialize the array. For i = first To last: result(i) = i: Next ' Now shuffle it. For i = last To first Step -1 ' Generate a random number in the proper range. j = Rnd * (last - first + 1) + first ' Swap the two items. temp = result(i): result(i) = result(j): result(j) = temp Next RandomArray = result End Function
¦r¦êªº³B²z
¡@
VBA¥]§t³\¦h±j¤jªº¦r¦ê¨ç¦¡¡A¦b²Ä¤@¦¸±½µø¹L®ÉÃø¥H½T©wþÓ¬O¾A¦X©Ò»Ýªº¡C¦b¦¹¸`¡Aµ§ªÌ²³æ¦a´yz§A¥i¨Ï¥Îªº©Ò¦³¦r¦ê¨ç¦¡¡A¦b¬Y¨Ç¤@¯ë±¡ªp¤U¡A´£¨Ñ¿ï¾Ü³Ì¦X©y¨ç¦¡ªº´£¥Ü¡A¨Ã¥B´£¨Ñ¤@¨Ç¥i¦bÀ³¥Îµ{¦¡¤º«½Æ¨Ï¥Îªº¦r¦ê¨ç¦¡¡C
°ò¥»¦r¦ê¹Bºâ¤¸¤Î¨ç¦¡
¡@
°ò¥»¦r¦ê¹Bºâ¤¸"¡®"°õ¦æ¦r¦ê³sµ²ªº°Ê§@¡Cµ²ªG¬O¥Ñ²Ä¤@Ó¦r¦êªº©Ò¦³¦r¤¸¥[¤W²Ä¤GÓ¦r¦êªº©Ò¦³¦r¤¸²Õ¦¨¡G
Print "ABCDE" & "1234" ' Displays "ABCDE1234"
³\¦h¨Ó¦ÛQuickBasicªºµ{¦¡³]pªÌ¤´µM¨Ï¥Î"+"¹Bºâ¤¸¨Ó°õ¦æ¦r¦ê³sµ²¡C³o¬O«Ü¦MÀIªº¡A·|¼vÅTµ{¦¡½Xªº¥iŪ©Ê¡A¥B·í¨ä¤¤¥ô¤@Ó¹Bºâ¤l¤£¬°¦r¦ê®É¡A¥i¯à·|¤Þ°_µLªk¹w´ÁªºÅܤơC
¤U±¦C¥X¨Ç¸û´¶¹Mªº¦r¦ê¨ç¦¡¡A¨ä¤¤¥]§tLeft$¡BRight$ ¤ÎMid$¡A·|¥Ñì©l¦r¦êªº¶}©l¡Bµ²§À¡A©ÎªÌ¬O¤¤¶¡ºI¨ú¥X¤l¦r¦ê¡C
Dim text As String text = "123456789" Print Left$(text, 3) ' Displays "123" Print Right$(text, 2) ' Displays "89" Print Mid$(text, 3, 4) ' Displays "3456"
¤p¯µ³Z
VBA¤å¥ó¤@³e¦a¬Ò¬Ù²¤©Ò¦³¦r¦ê¨ç¼Æªº$¦r¤¸¡A¦Ó¨Ï¥Î¤Ö¤F$ªº·s¨ç¦¡¡C§O³o¼Ë°µ¡I¤Ö¤F$ªº¨ç¦¡¦^¶Çªº¥]§t¦r¦êµ²ªGªºVariant¡A³oªí¥Ü¦b¥¦¥i³Q«·s¨Ï¥Î©ó¹Bºâ¦¡©Î«ü¬£¬°StringÅܼƫe¡AVariant¥²¶·³QÂàÅܬ°¦r¦ê¡C«D¥¿¦¡ªºµû¦ôÅã¥Ü¡A¨Ò¦pLeft$ ¨ç¦¡´N¤ñ¤Ö¤F$ªº¬Û¦P¨ç¦¡§Ö¨â¿¡C¦P¼Ë¦a¡A¨ä¥¦¨ç¦¡³£¦³¨âӧΦ¡¡A¥]¬A¡GLcase¡BUcase¡BRtrim¡BTrim¡BChr¡BFormat¡BSpace¥H¤ÎString¡C
Mid$ ¤]¥i¹³«ü¥O¯ë¡AÅý§Aקï¦r¦ê¤ºªº¼ÆÓ¦r¤¸¡G
Text = "123456789" Mid$(Text, 3, 4) = "abcd" ' Now Text = "12abcd789"
Len¨ç¦¡¶Ç¦^²{¦æ¦r¦êªºªø«×¡C±`¥Î¨Ó´ú¸Õ¦r¦ê¤¤¬O§_¥]§t¥ô¦ó¦r¤¸¡G
Print Len("12345") ' Displays "5" If Len(Text) = 0 Then ... ' Faster than comparison with an empty string.
¥i¨Ï¥ÎLtrim$¡BRtrim$ ¤ÎTrim$ ¨ç¦¡¡A¥h±¼µ²§À©Î¶}ÀY¤£nªºªÅ¥Õ³B¡G
Text = " abcde " Print LTrim$(Text) ' Displays "abcde " Print RTrim$(Text) ' Displays " abcde" Print Trim$(Text) ' Displays "abcde"
³o¨Ç¨ç¦¡¹ï©ó©T©wªø«×ªº¦r¦ê¯S§O¦³¥Î¡A©T©wªø«×¦r¦ê·|¸Éº¡ªÅ¥Õ¥H«K¨Ï¦r¦êªø«×«O«ù©T©w¡C¥i§Q¥ÎRtrim$ ¨ç¦¡¨Ó«d´î¨º¨ÇÃB¥~ªÅ¥Õ¡G
Dim Text As String * 10 Text = "abcde" ' Text now contains "abcde ". Print Trim$(Text) ' Displays "abcde"
ª`·N
·í«Å§i©T©wªø«×¦r¦ê¡A«o©|¥¼¨Ï¥Î¥¦®É¡A«h¥¦¥]§t¤FNull¦r¤¸¡A¦Ó«DªÅ¥Õ¡C³oªí¥ÜRtrim$ ¨ç¦¡µLªk«d´î³oÃþªº¦r¦ê¡G
Dim Text As String * 10 Print Len(Trim$(Text)) ' Displays "10", no trimming has occurred.
nÁ׶}³o¼Ëªº°ÝÃD¡A¥i¦b¨ä«Å§i«á¤Î¨Ï¥Î¥¦Ì«e¤À°t¤@ӪŦr¦êµ¹À³¥Îµ{¦¡¤ºªº©Ò¦³©T©wªø«×¦r¦ê¡C
Asc¨ç¦¡¶Ç¦^¦r¦ê¤º²Ä¤@Ó¦r¥Àªº¦r¤¸½X¡C¨äÃþ¦ü©ó¨Ï¥ÎLeft¢C ¨ç¦¡¨Ó©â¥X²Ä¤@Ó¦r¤¸¡A¦ýAsc¨ç¦¡¤ñ¥¦§Ö¦h¤F¡C
If Asc(Text) = 32 Then ' Test whether the fist char is a space. If Left$(Text, 1) = " " Then ' Same effect, but 2 to 3 times slower
¨Ï¥ÎAsc¨ç¦¡®ÉÀ³¸Ó½T»{¦¹¦r¦ê¤£¬OªÅªº¡A¦]¬°³o¼Ë·|¤Þµo¿ù»~¡C´N¬YºØ·N¸q¨Ó»¡¡A©MAsc¨ç¦¡¬Û¹ïªºChr$ ¨ç¦¡·|±N¼ÆȽXÂà´«¬°¬Û¹ïÀ³ªº¦r¤¸¡G
Print Chr$(65) ' Displays "A"
Space$ ©MString$ ¨ç¦¡«Ü¬Û¦ü¡C«eªÌ¦^¶Çªø«×¦ÛqªºªÅ¥Õ¦r¦ê¡A¦Ó«áªÌ¦^¶Ç¤@Ó¦r¦ê¡A¦¹¦r¦êªø«×¥Ñ²Ä¤@ӰѼƩҫü©w¡A¦Ó¦r¦ê¤º®e«h¤@¦A«½Æ²Ä¤GӰѼơG
Print Space$(5) ' Displays " " (five spaces) Print String$(5, " ") ' Same effect Print String$(5, 32) ' Same effect, using the char code Print String$(50, ".") ' A row of 50 dots
³Ì«áStrComp¨ç¦¡¥Î¨Ó¤ñ¸û¦r¦ê¡A¥BY²Ä¤@ӰѼƤp©ó¡Bµ¥©ó©Î¤j©ó²Ä¤GӰѼƮɡA¨Ì§Ç¦^¶Ç-1¡B0©Î1¡C²Ä¤TӰѼƪí¥Ü¬O§_¸Ó¤w¨S¦³¤À¤j¤p¼gªº¤è¦¡¤ñ¹ï¡G
Select Case StrComp(first, second, vbTextCompare) Case 0 ' first = second (e.g. "VISUAL BASIC" vs. "Visual Basic") Case -1 ' first < second (e.g. "C++" vs. "Visual Basic") Case 1 ' first > second (e.g. "Visual Basic" vs. "Delphi") End Select
StrComp¨ç¦¡¦³®É¹ï©ó°Ï¤À¤j¤p¼gªº¤ñ¹ï¤è¦¡º¡¤è«Kªº¡A¦]¬°¤£»Ýn¨â¦¸¤£¦Pªº´ú¸Õ¨Ó¨M©w¤@Ó¦r¦ê¬O§_¤p©ó¡Bµ¥©ó©Î¤j©ó¥t¤@Ó¦r¦ê¡C
Âà´«¨ç¦¡
¡@
³Ì±`¥Î¨ìªº¦r¦êÂà´«¨ç¦¡¬OUCase$ ©MLCase$¡A¥LÌ·|¤À§OÂà´«¦r¦ê¬°¤j¼g©Î¤p¼g¡G
Text = "New York, USA" Print UCase$(Text) ' "NEW YORK, USA" Print LCase$(Text) ' "new york, usa"
StrConv¨ç¦¡¥]§t¤F«e¨âªÌªº¥\¯à¡A¨Ã¥[¤J§ó¦h¥\¯à¡C¥i¥Î¥¦¨ÓÂà´«¤j¼g¡B¤p¼g©M¾A·í¤j¤p¼g¡]¨CÓ³æ¦rªº²Ä¤@Ó¦r¥À¬°¤j¼g¡A¨ä¾l¬°¤p¼g¡^¡G
Print StrConv(Text, vbUpperCase) ' "NEW YORK, USA" Print StrConv(Text, vbLowerCase) ' "new york, usa" Print StrConv(Text, vbProperCase) ' "New York, Usa"
¡]¦Xªkªº¦r¦ê¤À¹j¤¸¦³ªÅ®æ¡BNull¦r¤¸¡B¸õ¦æ©M¦^¦æºµ¥¡C¡^¦¹¨ç¦¡ÁÙ¯à¨Ï¥ÎvbUnicode©MvbFromUnicode°Ñ¼Æ¨Ó¶i¦æANSI©MUnicode¶¡ªºÂà´«¡C¦ý¦b¼Ð·ÇªºVisual BasicÀ³¥Îµ{¦¡¤¤«Ü¤Ö¨Ï¥Î³o¨Ç¨ç¦¡¡C
Val¨ç¦¡¯à°÷Âà´«¦r¦ê¬°¤Q¶i¦ì¼Æ¦r¡C¡]¥i°Ñ¾\«eÀYªº
¡qÂà´«¼ÆȪº°ò©³¡r ¡C¡^Visual BasicÁÙ¦³¯à±N¦r¦êÂà´«¬°¼ÆȪº¨ç¦¡¡A¦pCInt¡BCLng¡BCSng¡BCDbl¡BCcu r©MCDate¡C¥LÌ©MVal¨ç¦¡¥Dnªº®t§O¬°¥L̬O¦a°ì¤Æªº¡CÁ|¨Ò¨Ó»¡¡A¥L̯ॿ½T¦a¿ëÃÑ¥X³r¸¹¬°¬Y¨Ç°ê®aªº¤p¼ÆÂI¦Ó¤£·í¦¨¬°¤d¦ì¼Æªº¤À¹j¦r¤¸¡C¬Û¤Ï¦a¡AVal¨ç¦¡¶È¯à¿ëÃѤp¼ÆÂI¡A¦Ó·íµo²{¤£¦Xªkªº¦r¤¸®É§Y°±¤î¹B§@¡]¥]¬A³f¹ô²Å¸¹©Î¥Î©ó¤d¦ì°Ï¹jªº³r¸¹¡^¡CStr$ ¨ç¦¡¯àÂà´«¼ÆȬ°¦r¦ê¡C¸òCStr¥Dnªº®t§O¦b©ó«eªÌ·|¦b°Ñ¼Æ¬°¥¿ªº±¡ªp¤U¼W¥[¤@Ó«e¾ÉªÅ¥Õ¡A¦Ó«áªÌ«h¤£·|¡C
´M§ä©M¨ú¥N¦r¦ê
¡@
InStr¨ç¦¡·|¦b¤@Ó¦r¦ê¤º´M§ä¤@¬q¤l¦r¦ê¡AµL½×¦³¨S¦³¤À¤j¤p¼g¡CYn¶Ç»¼°Ñ¼Æ«ü¥Ün¨Ï¥ÎþºØ·j´M®É¡A¤£¯à¬Ù²¤¶}©lµù¼ÐÈ¡G
Print InStr("abcde ABCDE", "ABC") ' Displays "7" (case sensitive) Print InStr(8, "abcde ABCDE", "ABC") ' Displays "0" (start index > 1) Print InStr(1, "abcde ABCDE", "ABC", vbTextCompare) ' Displays "1" (case insensitive)
InStr¨ç¦¡©ö©ó«Ø¥ß¨ä¥LVBA»y¨¥¤£´£¨Ñªº±j¤j¦r¦ê¨ç¦¡¡C¨Ò¦p¡G¬Y¨ç¦¡n¦b¤@Ó·j´Mªí¤¤´M§ä¦r¤¸²Ä¤@¦¸¥X²{ªº¦a¤è¡C¹ï©ó¤À¸Ñ³Q³\¦h¤£¦P¤À¹j¦r¤¸©Ò°Ï¤Àªº¦r¦ê¬O«Ü¦³¥Îªº¡C
Function InstrTbl(source As String, searchTable As String, _ Optional start As Long = 1, _ Optional Compare As VbCompareMethod = vbBinaryCompare) As Long Dim i As Long For i = start To Len(source) If InStr(1, searchTable, Mid$(source, i, 1), Compare) Then InstrTbl = i Exit For End If Next End Function
Visual Basic 6¥i§Q¥Î·s¥[¤JªºInStrRev¨ç¦¡¨Ó°õ¦æ¤Ï¦V·j´M¡C¨ä»yªkÃþ¦ü³ÌªìªºInStr¨ç¦¡¡A¦ý¨ä°Ñ¼Æ¶¶§Ç«o¤£¦P¡G
found = InStrRev(Source, Search, [Start], [CompareMethod])
³o¨à¦³´XÓ¨Ò¤l¡C»¡©ú¦pªG¬Ù²¤start°Ñ¼Æ®É¡A±N·|¥Ñ¦r¦êªº³Ì¥½ºÝ¶}©l·j´M¡G
Print InStrRev("abcde ABCDE", "abc") ' Displays "1" (case sensitive) Print InStrRev("abcde ABCDE", "abc", ,vbTextCompare ) ' Displays "7" (case insensitive) Print InStrRev("abcde ABCDE", "ABC", 4, vbTextCompare ) ' Displays "1" (case insensitive, start<>0)
Visual Basic¤]¥]§t©ö©ó¾Þ§@ªº¦r¦ê¹Bºâ¤¸¡A·í¤ÀªR¦r¦ê»P°õ¦æ½ÆÂøªº·j´M°Ê§@®É¡ALike¹Bºâ¤¸©¹©¹§êºt±Ï¥Íûªº¨¤¦â¡C¨ä»yªk¦p¤U¡G
result = string Like pattern
¨ä¤¤string¬°n¤ÀªRªº¦r¦ê¡A¦Ópattern¬O©w¸q·j´M±ø¥ó¡A¦Ó¥Ñ¯S®í¦r¤¸²Õ¦¨ªº¦r¦ê¡C³Ì±`¨Ï¥Îªº¯S®í¦r¤¸¦³?¡]¥ô¦ó¤@Ó¦r¤¸¡^¡A*¡]¤@өΦhÓ¦r¤¸¡^©M#¡]¥ô¦ó¤@¦ì¼Æ¦r¡^¡C³o¸Ì¦³¨Ç¨Ò¤l¡G
' The Like operator is affected by the current Option Compare setting. Option Compare Text ' Enforce case-insensitive comparisons. ' Check that a string consists of "AB" followed by three digits. If value Like "AB###" Then ... ' e.g. "AB123" or "ab987" ' Check that a string starts with "ABC" and ends with "XYZ". If value Like "ABC*XYZ" Then ... ' e.g. "ABCDEFGHI-VWXYZ" ' Check that starts with "1", ends with "X", and includes 5 chars. If value Like "1???X" Then ... ' e.g. "1234X" or "1uvwx"
¤]¥iÂǥѴ¡¤J¤@¦C¥Ñ¤¤¬A©·¬A°_¨Óªº¦r¦ê¨Óªí¥Üþ¨Ç¦r¤¸¬On³Q¥]§t¡]©Î±Æ°£¡^¦b·j´M¤ºªº¡C
' One of the letters "A","B","C" followed by three digits If value Like "[A-C]###" Then ... ' e.g. "A123" or "c456" ' Three letters, the first one must be a vowel If value Like "[AEIOU][A-Z][A-Z]" Then... ' e.g. "IVB" or "OOP" ' At least three characters, the first one can't be a digit. ' Note: a leading "!" symbol excludes a range. If value Like "[!0-9]??*" Then ... ' e.g. "K12BC" or "ABHIL"
Visual Basic 6¤Þ¤J·sªºReplace¨ç¦¡¡A¥i¨³³tªº·j´M¨Ã¨ú¥N¤l¦r¦ê¡C¦]¬°¥]§t¤F´XÓ¿ï¾Ü©Ê°Ñ¼Æ¡A¨Ï¨ä»yªk¬Ý°_¨Ó¨Ã¤£¨º»ò²³æ¡G
Text = Replace(Source, Find, Replace, [Start], [Count], [CompareMethod])
³Ì²³æªº¼Ò¦¡¥i¥H¤£¤À¤j¤p¼g¤è¦¡¶i¦æ·j´M¡A¥B¨ú¥N©Ò¦³§ä¨ìªº¦r¦ê¡G
Print Replace("abc ABC abc", "ab", "123") ' "123c ABC 123c"
¨Ì·Ó¨ä¾lªº°Ñ¼Æ¡A¥i¥H¥Ñ¤£¦Pªº¦ì¸m¶}©l·j´M¡A©w´À¥N¦¸¼Æ¡A»P¶i¦æ¤j¤p¼g°Ï¤Àªº·j´M¡C°O¦ístartȤj©ó1®É¡A·|¦b·j´M¶}©l«e¡AºI±¼source°Ñ¼Æªº«e«áºÝªÅ¥Õ¡G
Print Replace("abc ABC abc", "ab", "123", 5, 1) ' "ABC 123c" Print Replace("abc ABC abc", "ab", "123", 5, 1, vbTextCompare) ' "123C abc"
¤]¥i¥ÎReplace¨ç¦¡¨Ópºâ¦r¦ê¤º¤l¦r¦ê¥X²{ªº¦¸¼Æ¡G
Function InstrCount(Source As String, Search As String) As Long ' You get the number of substrings by subtracting the length of the ' original string from the length of the string that you obtain by ' replacing the substring with another string that is one char longer. InstrCount = Len(Replace(Source, Search, Search & "*")) - Len(Source) End Function
·sªºStrReverse¨ç¦¡¥i§Ö³tªºÄA˦r¦ê¤º¦r¤¸ªº¶¶§Ç¡C¦¹¨ç¼Æ¥»¨¨SÔ£¥Î³B¡A¦ý¥i¹ï¨ä¥L¦r¦ê³B²z¨ç¦¡¦Ó¨¥«o¦³»ùÈ¡G
' Replace only the LAST occurrence of a substring. Function ReplaceLast(Source As String, Search As String, _ ReplaceStr As String) As String ReplaceLast = StrReverse(Replace(StrReverse(Source), _ StrReverse(Search), StrReverse(ReplaceStr), , 1)) End Function
Split¨ç¦¡¥i§ä¥X¦r¦ê¤º©Ò¦³ªº°Ï¹jªº¶µ¥Ø¡C¨ä»yªk¦p¤U¡G
arr() = Split(Source, [Delimiter], [Limit], [CompareMethod])
¨ä¤¤delimiterªí¥Ü°Ï¹j¦U¦Û¶µ¥Øªº¦r¤¸¡C¦pªG¤£·Qn¹L¦h¶µ¥Ø¡A¥i¶Çµ¹limit°Ñ¼Æ¤@Ó¥¿È¡A¥B¥iÅý³Ì«á¤@ӰѼÆȬ°vbTextCompareȪí¥Ü°õ¦æ¤j¤p¼g¤£¤Àªº·j´M¡C¥Ñ©ó¹w³]ªº°Ï¹j¦r¤¸¬°ªÅ¥Õ¡A¥i¨Ï¥Î¤U¦Cµ{¦¡»´©ö¦aÂ^¨ú¥y¤l¤¤ªº©Ò¦³³æ¦r¡G
Dim words() As String words() = Split("Microsoft Visual Basic 6") ' words() is now a zero-based array with four elements.
Join¨ç¦¡»PSplit¨ç¦¡¬O¤¬¸Éªº¡A¦]¬°¥¦±µ¨ü¤@Ó¦r¦ê°}¦C»P¤@ӰϹj¦r¤¸¡A¨Ã««Øì©l¦r¦ê¡G
' Continuing the preceding example ... ' The delimiter argument is optional here, because it defaults to " ". Print Join(words, " ") ' Displays "Microsoft Visual Basic 6"
°O¦íSplit»PJoin¨ç¦¡¤¤ªºdelimiter°Ñ¼Æ¥i¦h©ó¤@Ó¦r¤¸¡C
Filter¨ç¦¡¬O¥t¤@Ó¦bVBA»y¨¥¤¤¨üÅwªïªº¨ç¦¡¡A¥¦¥i¥H«Ü§Ö¦a±½´y¤@Ó´M§ä¤l¦r¦êªº°}¦C¡A¨Ã¦^¶Ç¥t¤@Ó¥]§t¡]©Î¤£¥]§t¡^·j´M¤l¦r¦ê¶µ¥Øªº°}¦C¡C¨ä»yªk¦p¤U¡G
arr() = Filter(Source(), Search, [Include], [CompareMethod])
¦pªGInclude°Ñ¼Æ¬°True©Î¬Ù²¤¡A«hµ²ªG°}¦C¥]¬A©Ò¦³¦bsource¤¤¥]§tsearch¤l¦r¦êªº¶µ¥Ø¡FY¬°False¡A«h²£¥Íªº°}¦C¥u¥]§t¤£§tsearchªº¶µ¥Ø¡C¦p©¹±`¡ACompareMetod°Ñ¼Æªí¥Ü¬O§_·j´M°Ï¤À¤j¤p¼g¡G
ReDim s(2) As String s(0) = "First": s(1) = "Second": s(2) = "Third" Dim res() As String res = Filter(s, "i", True, vbTextCompare) ' Print the result array ("First" and "Third"). For i = 0 To UBound(res): Print res(i): Next
Y¦bì°}¦C¤¤¨S¦³¶µ¥Ø¦X¥G´M§äªºn¨D¡A·í¶Çµ¹Ubound¨ç¦¡®É¡AFilter¨ç¦¡·|¶Ç°e¤@Ó¦^¶ÇÈ-1ªº¯S§O°}¦C¡C
¦r¦êªº®æ¦¡¿ï¾Ü
¡@
§A¥i¥H¨Ï¥ÎFormat¨ç¦¡¨Ó®æ¦¡¤Æ¦r¦ê¡C¥u¥i¥H«ü©w¤@Ó¦Ûq®æ¦¡¡]¹ï©ó¦r¦ê¸ê®Æ¡A¨S¦³«ü¦W®æ¦¡¥i¥Î¡^¡A¥B¥u¦³¦³ªº¯S®í¦r¤¸¡A¦ý¦Ü¤Ö¥i±o¨ì³\¦h¼u©Ê¡C¥i«ü©w¨âӰϬq¡A¤@ӥΩó«DªÅªº¦r¦êÈ¡A¤@ӥΩóªÅªº¦r¦êÈ¡A¦p¤U©Ò¥Ü¡G
' By default, placeholders are filled from right to left. ' "@" stands for a character or a space, "&" is a character or nothing. Print Format("abcde", "@@@@@@@") ' " abcde" ' You can exploit this feature to right align numbers in reports. Print Format(Format(1234.567, "Currency"), "@@@@@@@@@@@") ' " $1,234.57" ' "!" forces left to right fill of placeholders. Print Format("abcde", "!@@@@@@@") ' "abcde " ' ">" forces to uppercase, "<" forces to lowercase. Print Format("abcde", ">& & & & &") ' "A B C D E" ' This is a good way to format phone numbers or credit-card numbers. Print Format("6152127865", "&&&-&&&-&&&&") ' "615-212-7865" ' Use a second section to format empty strings. ' "\" is the escape character. Print Format("", "!@@@@@@@;\n\o\n\e")
¤é´Á©M®É¶¡ªº¹B§@
¡@
Visual Basic¤£¶È¥i¥Î¯S§OªºDate¸ê®Æ«¬ºA¨ÓÀx¦s¸ê®Æ¤Î®É¶¡¸ê°T¡A¤]´£¨Ñ³\¦hÃö©ó¤é´Á»P®É¶¡ªº¨ç¼Æ¡C³o¨Ç¨ç¼Æ¦b°Ó·~À³¥Î¤W¬Û·í«n¡A¦]¦¹¸Ó²`¤Jªº±´°Q¡C
¨ú±o©M³]©w²{¦b¤é´Á»P®É¶¡
¡@
ÄY®æ¦a»¡¡ADate»PTime¨Ã«D¬O¨ç¦¡¡G¥L̬OÄÝ©Ê¡C¨Æ¹ê¤W¡A¥i¨Ï¥Î¥¦Ì¨Ó¨ú±o²{¦bªº¤é´Á»P®É¶¡¡]¬°Date«¬ºA¡^¡A©Î«ü¬£·sȵ¹¥¦Ì¨Ó§ó·s¨t²Î³]©w¡G
Print Date & " " & Time ' Displays "8/14/98 8:35:48 P.M.". ' Set a new system date using any valid date format. Date = "10/14/98" Date = "October 14, 1998"
»¡©ú
¬°¤FÀ°§U¤ñ¸û©Ò¦³¤é´Á©M®É¶¡ªº¨ç¦¡ªºµ²ªG¡A¥»¸`¥þ³¡ªº¨Ò¤l°²©w¤é´Á®É¶¡¬°¥ý«eµ{¦¡¤ù¬q©ÒÅã¥Üªº¡GAugust 14, 1998, 8:35:48 p.m.¡C
ªºDate$ ©MTime$ Äݩʤ]¥i°µ¦P¼Ëªº¤u§@¡CµM¦Ó¥¦Ì¬OStringÄÝ©Ê¡A¦]¦¹¶È»{±omm/dd/yy©Îmm/dd/yyyy®æ¦¡©Mhh:mm:ss©Mhh:mm®æ¦¡¡C°ò©ó³oÓ²z¥Ñ¡A³q±`·|¨Ï¥Î·sªºµL$¨ç¦¡¡C
Now¨ç¦¡·|¦^¶Ç²{¦bªº¤é´Á©M®É¶¡È¡]Date«¬ºA¡^¡G
Print Now ' Displays "8/14/98 8:35:48 P.M.".
Timer¨ç¦¡¦^¶Ç±q¹s®É°_ºâªº¬í¼Æ¡A¥B¤ñNowºë·Ç¡A¦]¬°Timer¨ç¦¡¥]§t¬í¼Æªº¤p¼Æ³¡¥÷¡C¡]¹ê»Úºë½T«×¨Ì·Ó¨t²Î¦Ó©w¡C¡^¦¹¨ç¦¡±`¥Î¦bµû¦ôµ{¦¡ªº°õ¦æ³t«×¡G
StartTime = Timer ' Insert the code to be benchmarked here. Print Timer - StartTime
¥ý«eªºµ{¦¡¦³¨Ç¤£·Ç½T¡GStartTimeÅܼƥi¯à·|¦b¨t²Î®ÉÄÁ±N¹L®É«e´N³Q«ü©w¡A©Ò¥Hµ{§Ç¥i¯à¤ñ¹ê»Ú¤W§óºC¡C³o¸Ì¦³Ó¸û¦nªº¤èªk¡G
StartTime = NextTimerTick ' Insert the code to be benchmarked here. Print Timer _ StartTime ' Wait for the current timer tick to elapse. Function NextTimerTick() As Single Dim t As Single t = Timer Do: Loop While t = Timer NextTimerTick = Timer End Function
¦pªG¦bµ{¦¡¤¤¨Ï¥ÎTimer¨ç¦¡¡AÀ³nª`·N®É¶¡¦b¹s®É·|«³]¡A©Ò¥H¶TµM¤Þ¤J¥i¯à·|³y¦¨¼ç¦bªºÄY«¿ù»~¡C¨Ò¦p¸ÕµÛ¼W¥[»PCPUµLÃöªº¼È°±¥i¯à¾ÉP¿ù»~¡G
' WARNING: this procedure has a bug. Sub BuggedPause(seconds As Integer) Dim start As Single start = Timer Do: Loop Until Timer _ start >= seconds End Sub
¿ù»~¨ä¹ê«Ü¤Ö¥X²{¡ÐÁ|Ó¨Ò¤l¡A¦pªGµ{¦¡¦b11:59:59 p.m.n¨D¨â¬íÄÁªº¼È°±¡C§Y¨Ï¥i¯à©Ê«Ü¤Ö¡A³o¤p¿ù»~ªº¼vÅTÁÙ¬O«Ü¤jªº¡A¥B¥²¶·«ö¤UCtrl+Alt+Del¨Ó§R±¼½sĶ¦nªºÀ³¥Îµ{¦¡¡C³o¸Ì¦³Ó¸Ñ¨M¤èªk¡G
' The correct version of the procedure Sub Pause(seconds As Integer) Const SECS_INDAY = 24! * 60 * 60 ' Seconds per day Dim start As Single start = Timer Do: Loop Until (Timer + SECS_INDAY - start) Mod SECS_INDAY >= seconds End Sub
«Ø¥ß©M¨ú±o¤é´Á®É¶¡È
¡@
¦³«Ü¦h¤èªk¥i¥H²Õ¦XDateÈ¡C¨Ò¦p¡A¨Ï¥ÎDate±`¼Æ¡A¦p¤U©Ò¥Ü¡G
StartDate = #8/15/1998 9:20:57 PM#
¦ý§ó¦h®ÉÔ¥i¥H±q«Ü¦hVBA¨ç¦¡¨ä¤¤¤§¤@¨Ó«Ø¥ßDateÈ¡CDateSerial¨ç¦¡·|«Ø¥ß¦~/¤ë/¤éªº¤é´ÁÈ¡F¦P¼Ë¦a¡ATimeSerial¨ç¦¡¯à«Ø¥ß®É/¤À/¬íªº®É¶¡È¡C
Print DateSerial(1998, 8, 14) ' Displays "8/14/98" Print TimeSerial(12, 20, 30) ' Displays "12:20:30 P.M." ' Note that they don't raise errors with invalid arguments. Print DateSerial(1998, 4, 31) ' Displays "5/1/98"
DateSerial¨ç¦¡¤]¯à°÷¦³®Ä¶¡±µ¦a¨M©w¬Y¦~¬O§_¬°¶|¦~¡G
Function IsLeapYear(year As Integer) As Boolean ' Are February 29 and March 1 different dates? IsLeapYear = DateSerial(year, 2, 29) <> DateSerial(year, 3, 1) End Function
DateValue©MTimeValue¨ç¦¡·|¦^¶Ç¨ä°Ñ¼Æªº¤é´Á©Î®É¶¡³¡¤À¡A¥i¥H¬O¥H¦r¦ê©Î¤é´Áªí¥Ü¡G
' The date a week from now Print DateValue(Now + 7) ' Displays "8/21/98"
VBA¨ç¦¡¶°¯à±q¤é´Áªí¥Ü¦¡©ÎÅܼƨú±o¤é´Á©M®É¶¡ªº¸ê°T¡CDay¡BMonth©MYear¨ç¦¡·|¦^¶Ç¤é´ÁÈ¡A¦p¦PHour¡BMinute©MSecond¨ç¦¡¯à¦^¶Ç®É¶¡È¡G
' Get information about today's date. y = Year(Now): m = Month(Now): d = Day(Now) ' These functions also support any valid date format. Print Year("8/15/1998 9:10:26 PM") ' Displays "1998"
Weekday¨ç¦¡¦^¶Ç¤¶©ó1¨ì7ªº¼Æ¦r¡A¨äȹïÀ³¨ìµ¹©wDate°Ñ¼Æªº¬P´Á§O¡G
Print Weekday("8/14/98") ' Displays "6" (= vbFriday)
Weekday¨ç¦¡¦b¤é´Á¬°¤@¶gªº²Ä¤@¤Ñ®É·|¦^¶Ç1¡C¦¹¨ç¦¡¬O¦a°ì¤Æªº¡A³oªí¥Ü¦b¤£¦PªºMicrosoft Windows¤U¯à§PÂ_¨ä¤@¶gªº²Ä¤@¤Ñ¡]¦³¨Ç¨Ã«D¬°vbSunday¡^¡C¦b¤j³¡¤À±¡ªp¤U¡A³oºØ±¡ªp¤£·|¼vÅTµ{¦¡½Xªºµ²ºc¡C¦ý¦pn½T©w1ªí¥Ü¬P´Á¤Ñ¡B2ªí¥Ü¬P´Á¤@µ¥µ¥¡A¥i±j¢¦b¤£¦PWindows¨t²Î¬Ò¶Ç¦^©T©wÈ¡A¦p¤U¡G
Print Weekday(Now, vbSunday)
ÁöµM¨Ï¥Î¿ï¾Ü©Êªº²Ä¤GӰѼƱj¢¨ç¦¡¦^¶Ç¥¿½TÈ¡A¦ý¨Ã¤£·|§ïÅܨt²Îªº¦a°ì©Ê¡C¦pªG¦A©I¥sWeekday¨ç¦¡¦Ó¤£¨Ï¥Î²Ä¤GӰѼơA¥¦±N¤´µMµø¤@¶gªº²Ä¤@¤Ñ¦p¦P¥H©¹¡C
³Ì«á¨Ï¥ÎDatePart¨ç¦¡¥i±q¤é´ÁȩΪí¥Ü¦¡¤¤¨ú±o¥ô¦ó¤é´Á©M®É¶¡ªº¸ê°T¡A¨ä»yªk¬°¡G
Result = DatePart(Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear])
³q±`«Ü¤Ö¨Ï¥Î³oӨ禡¡A¦]¬°¨Ï¥Î¤§«eªº¨ä¥L¨ç¦¡´N¯à°µ¨ì¤j³¡¤Àªº¥\¯à¡CµM¦Ó¦b©³¤U¨âӮרҤ¤¡A¦¹¨ç¦¡´N¯uªº«Ü¦³¥Î¡G
' The quarter we are in Print DatePart("q", Now) ' Displays "3" ' The week number we are in (# of weeks since Jan 1st) Print DatePart("ww", Now) ' Displays "33"
²Ä¤@ӰѼƥi¬°ªí5-1©Ò¦Cªº¦r¦ê¡C§ó¦hÃö©ó³o¤GÓ¿ï¾Ü©Ê°Ñ¼Æ¡A½Ð°Ñ¾\¤U¸`
DateAdd¨ç¦¡ ªº¤¶²Ð¡C³]©w | »¡©ú |
---|---|
"yyyy" | ¦è¤¸¦~ |
"q" | ©u¸` |
"m" | ¤ë¼Æ |
"y" | ¤@¦~¤¤ªº¤Ñ¼Æ¡]¦Pd¡^ |
"d" | ¤Ñ¼Æ |
"w" | ¬P´Á§O |
"ww" | ¬P´Á |
"h" | ¤p®É |
"n" | ¤À |
"s" | ’ |
ªí5-1 DatePart¡BDateAdd©MDateDiff¨ç¦¡¤¤interval°Ñ¼Æ¥i¯àªºÈ¡C |
¤é´Ápºâ
¡@
¦b¤j³¡¤À±¡ªp¤U¡A¤£»Ýn¥ô¦ó¯S§Oªº¨ç¦¡¨Ó¶i¦æ¤é´Ápºâ¡C©Ò¦³»Ýnª¾¹DªºDateÅܼƪº¾ã¼Æ³¡¥÷¥]§t¤é´Á¸ê°T¡A¦Ó¤p¼Æ³¡¤À¥]§t®É¶¡¸ê°T¡G
' 2 days and 12 hours from now Print Now + 2 + #12:00# ' Displays "8/17/98 8:35:48 A.M."
npºâ¤é´Á¡A¥i¨Ï¥ÎDateAdd¨ç¦¡¡A¨ä»yªk¦p¤U©Ò¥Ü¡G
NewDate = DateAdd(interval, number, date)
Interval¬Oªí¥Ü¤é´Á©Î®É¶¡³æ¦ìªº¦r¦ê¡]¨£ªí5-1¡^¡Anumber¬O©Òn¼W¥[ªº³æ¦ì¼Æ¶q¡A¦Ódate«h¬°°_©l¤é´Á¡C¥i¨Ï¥Î¦¹¨ç¦¡¨Ó¼W´î¤é´Á©M®É¶¡È¡G
' The date three months from now Print DateAdd("m", 3, Now) ' Displays "11/14/98 8:35:48 P.M." ' One year ago (automatically accounts for leap years) Print DateAdd("yyyy", -1, Now) ' Displays "8/14/97 8:35:48 P.M." ' The number of months since Jan 30, 1998 Print DateDiff("m", #1/30/1998#, Now) ' Displays "7" ' The number of days since Jan 30, 1998 _ you can use "d" or "y". Print DateDiff("y", #1/30/1998#, Now) ' Displays "196" ' The number of entire weeks since Jan 30, 1998 Print DateDiff("w", #1/30/1998#, Now) ' Displays "28" ' The number of weekends before 21st century - value <0 means ' future dates. ' Note: use "ww" to return the number of Sundays in the date interval. Print DateDiff("ww", #1/1/2000#, Now) ' Displays "-72"
·í¾Ö¦³¨âÓ¤é´Á¡A¥B·Qnpºâ¨âªÌÓ®t¶Z®É—´N¬O¨âªÌ¬Û®tªº®É¶¡À³¸Ó¨Ï¥ÎDateDiff¨ç¦¡¡A¨ä»yªk¬°¡G
Result = DateDiff(interval, startdate, enddate _ [, FirstDayOfWeek[, FirstWeekOfYear]])
¨ä¤¤interval·N¸qÅã¥Ü¦pªí5-1©Ò¥Ü¡AFirstDatOfWeek¬OӥΨӫü©w¬P´Á´X¬°¤@¶gªº¶}©lªº¿ï¾Ü©Ê°Ñ¼Æ¡]¥i¥ÎvbSunday¡BvbMonday±`¼Æµ¥µ¥¡^¡A¦ÓFirstWeekOfYear¬°¥t¤@Ó¯à«ü©wþ¶g¬O¤@¦~ªº¶}©l¶gªº¿ï¾Ü©Ê°Ñ¼Æ¡C¡]¨£ªí5-2¡^
±`¼Æ | È | »¡©ú |
---|---|---|
vbUseSystem | 0 | ¨Ï¥ÎNLS API³]©w¡C |
vbFirstJan1 | 1 | ²Ä¤@¶g¥]§t¬°¥]§t¤@¤ë¤@¤éªº¨º¶g¡C¡]³o¬O¦¹³]©wªº¹w³]È¡C¡^ |
vbFirstFourDays | 2 | ²Ä¤@¶g¬O¦b·sªº¤@¦~¤¤¾Ö¦³³Ì¤Ö¥|¤Ñªº²Ä¤@Ó¬P´Á¡C |
VbFirstFullWeek | 3 | ²Ä¤@¶g¬O²Ä¤@Ó§¹¾ã¥]§t¦b·sªº¤@¦~ªº²Ä¤@Ó¬P´Á¡C |
ªí5-2 DateDiff¨ç¦¡¤¤FirstWeekOfYear¥i¯àªºÈ |
¤é´Á©M®É¶¡Èªº®æ¦¡¤Æ¿ï¾Ü
¡@
³Ì«n»P³Ì¦³¼u©Êªº®æ¦¡¤Æ¤é´Á©M®É¶¡Èªº¨ç¦¡¬°Format¨ç¦¡¡C¦¹¨ç¦¡¦³¤CÓÃö©ó¤é´Á®É¶¡¤£¦Pªº«ü¦W®æ¦¡¡G
¤]¥i¥Î¤@¨Ç¯S§O¦r¤¸¨Ó¦Ûq¤é´Á©M®É¶¡®æ¦¡ªº¦r¦ê¡A¥]¬A¤@өΨâӼƦrªº¤é¼Æ©M¤ë¼Æ¡A§¹¾ã©Î¨äÁY¼gªº¤ë¥÷©M¬P´Á¦WºÙ¡Aa.m¡þp.m.Åã¥Ü¡A¬P´Á§O©M¥|©u¼Æ¦rµ¥µ¥¡G
' mmm/ddd = abbreviated month/weekday, ' mmmm/dddd = complete month/weekday Print Format(Now, "mmm dd, yyyy (dddd)") ' "Aug 14, 1998 (Friday)" ' hh/mm/ss always use two digits, h/m/s use one or two digits Print Format(Now, "hh:mm:ss") ' "20:35:48" Print Format(Now, "h:mm AMPM") ' "8:35 P.M." ' y=day in the year, ww=week in the year, q=quarter in the year ' Note how a backslash can be used to specify literal characters. Print Format(Now, "mm/dd/yy (\d\a\y=y \w\e\e\k=ww \q\u\a\r\t\e\r=q)") ' Displays "08/14/98 (day=226 week=33 quarter=3)"
Visual Basic 6¤Þ¶i·sªºFormatDateTime¨ç¦¡¡C¥¦»·¤Ö©ó¼Ð·ÇªºFormat¨ç¦¡n¨Ó±o¨S¼u©Ê¡A¥B¶È¤¹³\³¡¤ÀªºFormat¨ç¦¡ªº«ü¦W®æ¦¡¡C ForamtDateTime¨ç¦¡°ß¤@ªºÀuÂI§Y¬°VBScript¤]¤ä´©¥¦¡A¥BÅýVisual Basic»PVBA¨ìVBScript¶¡ªºÂà´««Ü®e©ö¡C¥¦ªº»yªk¬°¡G
result = FormatDateTime(Expression, [NamedFormat])
¨ä¤¤VamesFormat¥i¥H¬O¤U¦C±`¼Æ¤§¤@¡G0-vbGeneralDate (¹w³]È)¡B1-vbLongDate¡B2-vbShortDate¡B3-vbLongTime©Î4-vbShortTime¡C³o¸Ì¦³¨Ç¨Ò¤l¡G
Print FormatDateTime(Now) ' "8/14/98 8:35:48 P.M." Print FormatDateTime(Now, vbLongDate) ' "Saturday, August 15, 1998" Print FormatDateTime(Now, vbShortTime) ' "20:35"
Visual Basic 6¤]´£¨Ñ¨âÓÃö©ó¤é´Á®æ¦¡ªº·s¨ç¦¡¡CMonthName¨ç¦¡·|¦^¶Ç§¹¾ã©ÎÁY¼gªº¤ë¥÷¦W¡A¦ÓWeekdatName¨ç¦¡«h¦^¶Ç§¹¾ã©ÎÁY¼gªº¬P´Á¦WºÙ¡C¨âªÌ³£¬O¦a°ì¤Æªº¡A©Ò¥H¥i¥Î¥¦Ì¨Ó¦C¥X§@·~¨t²Î»y¨¥³]©wªº¤ë¥÷©M¬P´Á¦WºÙ¡G
Print MonthName(2) ' "February" Print MonthName(2, True) ' "Feb" Print WeekdayName(1, True) ' "Sun"
Àɮתº³B²z
¡@
Visual BasicÁ`¬O¾Ö¦³³\¦h±j¤jªº«ü¥O¨Ó³B²z¤å¦r©M¤G¶i¦ìÀɮסCÁöµMVisual Basic 6¨S¦³ÂX¥R¤º«Ø¨ç¦¡¶°¡A¦ý¥¦ÂǥѼW¥[·sÂA¥B¦³½ìªºFileSystemObjectª«¥ó¨Ó³B²zÀɮשM¥Ø¿ý¡A¦Ó¶¡±µ¦a©µ¦ù»y¨¥ªº¥\¯à¡C¥»¸`¤¶²Ð©Ò¦³Ãö©óÀɮתºVBA¨ç¦¡©M±ÔzªºÁ`Äý¡A¥]§t³\¦h¦³¥Îªº§Þ¥©¡A¥H«KÅý±z¯àÀò±o³Ì¦h±q¦Ó»·Â÷¤@¦Aµo¥Íªº°ÝÃD¡C
¾Þ§@ÀÉ®×
¡@
¤@¯ë¨Ó»¡¡A¤£¶}±ÒÀɮ׫hµLªk°µ¨ä¥L¬ÛÃöªº¨Æ¡C¦ýVisual Basic¯à§R°£¤@ÓÀɮס]¨Ï¥ÎKill«ü¥O¡^¡A²¾°Ê©Î§ó§ï¦WºÙ¡]¨Ï¥ÎName...As«ü¥O¡^©M½Æ»s¨ì§O³B¡]¨Ï¥ÎFileCopy«ü¥O¡^¡G
' All file operations should be protected against errors. ' None of these functions works on open files. On Error Resume Next ' Rename a file--note that you must specify the path in the target, ' otherwise the file will be moved to the current directory. Name "c:\vb6\TempData.tmp" As "c:\vb6\TempData.$$$" ' Move the file to another directory, possibly on another drive. Name "c:\vb6\TempData.$$$" As "d:\VS98\Temporary.Dat" ' Make a copy of a file--note that you can change the name during the copy ' and that you can omit the filename portion of the target file. FileCopy "d:\VS98\Temporary.Dat", "d:\temporary.$$$" ' Delete one or more files--Kill also supports wildcards. Kill "d:\temporary.*"
¨Ï¥ÎGetAttr©MSetAttr¨ç¦¡¤À§O¥i¥HŪ¨ú©MקïÀɮתºÄÝ©Ê¡CGetAttr¨ç¦¡·|¦^¶Ç¤@Óbit-codeÈ¡A©Ò¥H»ÝnÂÇ¥ÑVBA¤º³¡ªº±`¼Æ¨Ó´ú¸Õ¦U¦ì¤¸È¡C³o¸Ì¦³Ó¥i«¥Î¨ç¦¡¥i¥H«Ø¥ßÃö©ó©Ò¦³ÀÉ®×Äݩʪº´yz¦r¦ê¡G
' This routine also works with open files ' and raises an error if the file doesn't exist. Function GetAttrDescr(filename As String) As String Dim result As String, attr As Long attr = GetAttr(filename) ' GetAttr also works with directories. If attr And vbDirectory Then result = result & " Directory" If attr And vbReadOnly Then result = result & " ReadOnly" If attr And vbHidden Then result = result & " Hidden" If attr And vbSystem Then result = result & " System" If attr And vbArchive Then result = result & " Archive" ' Discard the first (extra) space. GetAttrDescr = Mid$(result, 2) End Function
¦P¼Ë¦a¡AÂǥѶǵ¹SetAttr«ü¥O¤@Ó²Õ¦XÈ¥i¥H§ïÅÜÀɮשθê®Æ§¨ªºÄÝ©ÊÈ¡A¦p¤U©Ò¥Ü¡G
' Mark a file as Archive and Read-only. filename = "d:\VS98\Temporary.Dat" SetAttr filename, vbArchive + vbReadOnly ' Change a file from hidden to visible, and vice versa. SetAttr filename, GetAttr(filename) Xor vbHidden
SetAttr¨ç¦¡µLªk¥Î¦b¤w¶}±ÒªºÀɮסC¦b¨S¦³¶}±ÒÀɮתº±¡ªp¤U¡A¥i¥H¤À§O¨Ï¥ÎFileLen©MFileDateTime¨ç¦¡±oª¾Àɮתº¨âÓ¸ê°T¡GÀÉ®×®e¶q©M«Ø¥ßªº¤é´Á©M®É¶¡¡C
Print FileLen("d:\VS98\Temporary.Dat") ' Returns a Long value Print FileDateTime("d:\VS98\Temporary.Dat") ' Returns a Date value
FileLen¨ç¦¡¤@¼Ë¤£¯à¥Î¦b¤w¶}±ÒªºÀɮפW¡A¦Ó¨ä¥i¦b¶}±ÒÀɮ׫e¨ú±o¥¿½Tªº®e¶q¡C
¾Þ§@¸ê®Æ§¨
¡@
¨Ï¥ÎCurDir$ ¨ç¦¡¥i¥H±oª¾¥Ø«e¸ê®Æ§¨ªº¦WºÙ¡]©Î¥¦ªºµL$¦Pµ¥¨ç¦¡¡ACurDir¡^¡C·í¶Çµ¹¦¹¨ç¦¡¤@ӺϺХN¸¹«á¡A¥¦·|¦^¶Ç¦¹¯S©w¸ô®|¥¿½Tªº¸ê®Æ§¨¡C¦b³oÓ¨Ò¤l¤¤¡A°²³]Microsoft Visual Studio¦w¸Ë¦bºÏºÐD©MMicrosoft Windows NT¦w¸Ë¦bºÏºÐC¡A¦ý¥i¯à¦b±zªº¨t²Î¤W¦³¤£¦Pªº³]©w¡G
' Always use On Error--the current dir might be on a removed floppy disk. On Error Resume Next Print CurDir$ ' Displays "D:\VisStudio\VB98" ' The current directory on drive C: Print = CurDir$("c") ' Displays "C:\WinNT\System"
Chdrive©MChdir¤À§O¯à°÷§ïÅܥثeªººÏºÐ©M¸ê®Æ§¨¡C¦pªGChDir«ü¥OªººÏºÐ¨Ã«D²{¦æªººÏºÐ®É¡A«h¹ê»Ú¤W¥u§ïÅܲ{¦æ¸ê®Æ§¨¨ì¸ÓºÏºÐ¡A©Ò¥H¥²¶·¨Ï¥Î³o¨âÓ«ü¥O¨Ó½T«O§ïÅܨt²Îªº²{¦æ¸ê®Æ§¨¡G
' Make "C:\Windows" the current directory. On Error Resume Next SaveCurDir = CurDir$ ChDrive "C:": ChDir "C:\Windows" ' Do whatever you need to do... ' .... ' and then restore the original current directory. ChDrive SaveCurDir: ChDir SaveCurDir
¤]¥i¥H¨Ï¥ÎMkdir©MRmDir«ü¥O¨Ó«Ø¥ß©M²¾°£¤l¥Ø¿ý¡G
' Create a new folder in the current directory, and then make it current. On Error Resume Next MkDir "TempDir" ChDir CurDir$ & "\TempDir" ' (Assumes current dir is not the root) ' Do whatever you need to do... ' .... ' then restore the original directory and delete the temporary folder. ' You can't remove directories with files in them. Kill "*.*" ' No need for absolute path. ChDir ".." ' Move to the parent directory. RmDir CurDir$ & "\TempDir" ' Remove the temporary directory.
¥i¥ÎName«ü¥O¨Ó§ïÅܸê®Æ§¨ªº¦WºÙ¡A¦ýµLªk²¾°Ê¸ê®Æ§¨¡G
' Assumes that "TempDir" is a subdirectory of the current directory Name "TempDir" As "TempXXX"
¦CÁ|¸ê®Æ§¨¤º©Ò¦³ÀÉ®×
¡@
VBAªºDir¨ç¦¡´£¨Ñ¤@Ó¥j¦Ñ«o¦³®Äªº¤èªk¨Ó¦CÁ|¸ê®Æ§¨¤¤©Ò¦³ªºÀɮסC¥Îfilespec°Ñ¼Æ©I¥sDir¨ç¦¡¡]¥]¬A¸U¥Î¦r¤¸¡^©M¤@Ó«D¥²»Ýªº°Ñ¼Æªí¥ÜÀɮתºÄÝ©Ê¡CµM«á¦b¨C¦¸¤ÏÂÐÂ^¨ú¤¤¡A³æ¿W©I¥sDirª½¨ì¦^¶Ç¤@ӪŪº¦r¦ê¡A¤U±ªº¨Ò¤l¦^¶Ç¯S©w¸ê®Æ§¨¤¤ÀɮצWºÙªº°}¦C©M¥Ü½d¥¿½T«Ø¥ß°j°éªº¤èªk¡G
Function GetFiles(filespec As String, Optional Attributes As _ VbFileAttribute) As String() Dim result() As String Dim filename As String, count As Long, path2 As String Const ALLOC_CHUNK = 50 ReDim result(0 To ALLOC_CHUNK) As String filename = Dir$(filespec, Attributes) Do While Len(filename) count = count + 1 If count > UBound(result) Then ' Resize the result array if necessary. ReDim Preserve result(0 To count + ALLOC_CHUNK) As String End If result(count) = filename ' Get ready for the next iteration. filename = Dir$ Loop ' Trim the result array. ReDim Preserve result(0 To count) As String GetFiles = result End Function
¤p¯µ³Z
¥i¥H¨Ï¥ÎDir$ ¨ç¦¡¶¡±µ¦a¥h´ú¸ÕÀɮשθê®Æ§¨ªº¦s¦b¡A¨Ï¥Î¤U±ªº¨ç¦¡¡G
Function FileExists(filename As String) As Boolean On Error Resume Next FileExists = (Dir$(filename) <> "") End Function Function DirExists(path As String) As Boolean On Error Resume Next DirExists = (Dir$(path & "\nul") <> "") End Function
ÁöµM¦bFileExistsªºµ{¦¡©úÅã©öÀ´¡A¦ý¤´¥i¯à¹ïDirExists°g´b¡G"\nul"¦r¦ê±q¦ó¦Ó¨Ó¡H³on¦^®Ò¨ìMS-DOS®É¥N¥H¤Î¨ä¯S®íÀɮצWºÙ"nul"©M"con"µ¥µ¥¡C³o¨Ç¦WºÙ¹ê»Ú¤WÄÝ©ó¯S®íªº¸Ë¸m¡]null¸Ë¸m¡B²×ºÝ¾÷µ¥µ¥¡^¨Ã¥X²{¦b¥ô¦ó¯à·j´M¨ì¥B¹ê»Ú¦s¦bªº¥Ø¿ý¤¤¡C³o¤èªk¥i¥Î¦b¥ô¦ó¸ê®Æ§¨¡A¦pn´ú¸Õ¸ê®Æ¬O§_¬°ªÅªº¦Ó¨Ï¥ÎDir$("*.*") ¤Ï¦Ó·|¥¢±Ñ¡C
GetFilesµ{§Ç¯à¥Î¦bŪ¨ú¤@¸sÀɮצWºÙ¨ìComboBox±±¨î¶µ¤¤¡C¤×¨ä¬O¦b±±¨î¶µªºSortedÄݩʬ°True®É§ó¬O¦³¥Î¡G
Dim Files() As String, i As Long ' All files in C:\WINDOWS\SYSTEM directory, including system/hidden ones. Files() = GetFiles("C:\windows\system\*.*", vbNormal + vbHidden _ + vbSystem) Print "Found " & UBound(Files) & " files." For i = 1 To UBound(Files) Combo1.AddItem Files(i) Next
¦pªGAttribute°Ñ¼Æ¥]§tvbDirectory¦ì¤¸¡A«hDir$ ¨ç¦¡¤]·|¦^¶Ç¸ê®Æ§¨ªº¦WºÙ¡C¨Ï¥Î³oÓ¯S©Ê¨Ó«Ø¥ßGetDirectories¨ç¦¡¥H¦^¶Ç«ü©w¦ì¸mªº©Ò¦³¤l¥Ø¿ý¦WºÙ¡G
Function GetDirectories(path As String, Optional Attributes As _ VbFileAttribute, Optional IncludePath As Boolean) As String() Dim result() As String Dim dirname As String, count As Long, path2 As String Const ALLOC_CHUNK = 50 ReDim result(ALLOC_CHUNK) As String ' Build the path name + backslash. path2 = path If Right$(path2, 1) <> "\" Then path2 = path2 & "\" dirname = Dir$(path2 & "*.*", vbDirectory Or Attributes) Do While Len(dirname) If dirname = "." Or dirname = ".." Then ' Exclude the "." and ".." entries. ElseIf (GetAttr(path2 & dirname) And vbDirectory) = 0 Then ' This is a regular file. Else ' This is a directory. count = count + 1 If count > UBound(result) Then ' Resize the result array if necessary. ReDim Preserve result(count + ALLOC_CHUNK) As String End If ' Include the path if requested. If IncludePath Then dirname = path2 & dirname result(count) = dirname End If dirname = Dir$ Loop ' Trim the result array. ReDim Preserve result(count) As String GetDirectories = result End Function
¤@¯ëµ{¦¡ªº¥Øªº¬O°õ¦æ¦b¤@ӥؿý¾ð¤¤©Ò¦³ªºÀɮסC·PÁ©ҦCªº¨Ò¤l©M»¼°jªº¯à¤O¡A³o¤wÅܦ¨¡]´X¥G¡^¤p«Äªº¹CÀ¸¡G
' Load the names of all executable files in a directory tree into a ListBox. ' Note: this is a recursive routine. Sub ListExecutableFiles(ByVal path As String, lst As ListBox) Dim names() As String, i As Long, j As Integer ' Ensure that there is a trailing backslash. If Right(path, 1) <> "\" Then path = path & "\" ' Get the list of executable files. For j = 1 To 3 ' At each iteration search for a different extension. names() = GetFiles(path & "*." & Choose(j, "exe", "bat", "com")) ' Load partial results in the ListBox lst. For i = 1 To UBound(names) lst.AddItem path & names(i) Next Next ' Get the list of subdirectories, including hidden ones, ' and call this routine recursively on all of them. names() = GetDirectories(path, vbHidden) For i = 1 To UBound(names) ListExecutableFiles path & names(i), lst Next End Sub
³B²z¤å¦rÀÉ®×
¡@
¯Â¤å¦rÀɬO³Ì®e©ö³B²zªºÃþ«¬¡C¨Ï¥Î¥]§tFor Input¡BFor Output©ÎFor Appending°Ñ¼ÆªºOpen±Ôz¶}±Ò¥¦Ì¡AµM«á¶}©lŪ¸ê®Æ¡B¼g¸ê®Æµ¥¡Cn¶}±ÒÀÉ®×¢w¤£½×¬O¤å¦rÀɩΤG¶i¦ìÀÉ¢w»ÝnÀɮ׸¹½X¡A¦p¤U¦Cµ{¦¡¡G
' Error if file #1 is already open Open "readme.txt" For Input As #1
¦b³æ¿WªºÀ³¥Îµ{¦¡¤¤¡A³q±`¥i¥H¤À°t°ß¤@ÀÉ®×½s¸¹µ¹¤£¦Pªºµ{§Ç¡CµM¦Ó¡A³o·|ÄY«¼vÅT¨ìµ{¦¡ªº«¥Î©Ê¡A©Ò¥H«Øij¨Ï¥ÎFreeFile¨ç¦¡¥H¬d¸ßVisual BasicÃö©ó²Ä¤@Ó¥i¥ÎÀÉ®×½s¸¹¡G
Dim fnum As Integer fnum = FreeFile() Open "readme.txt" For Input As #fnum
¦b¿é¤J¤@ӯ¤å¦rÀÉ«á¡A³q±`¨Ï¥ÎLine Input±Ôz¤@¦¸Åª¨ú¤@¦æ¤å¦rª½¨ìEOF¨ç¦¡¶Ç¦^True¡C·í¶}±ÒÀɮשMŪ¨úÀɮתº¤º®e¡A³£¥²¶·³B²z¿ù»~¡A¦ý¥ÎLOF¨ç¦¡¨Ó§P©wÀɮתºªø«×©M¥ÎInput$ ¨ç¦¡Åª¨ú©Ò¦³¦r¤¸¨Ó°µ³o¥ó¨Æ·|§ó¦n¡C³o¸Ì¦³Ó±`¥Î¥B§ó§¹¬üªº¨Ò¤l¡G
Function ReadTextFileContents(filename As String) As String Dim fnum As Integer, isOpen As Boolean On Error GoTo Error_Handler ' Get the next free file number. fnum = FreeFile() Open filename For Input As #fnum ' If execution flow got here, the file has been open without error. isOpen = True ' Read the entire contents in one single operation. ReadTextFileContents = Input(LOF(fnum), fnum) ' Intentionally flow into the error handler to close the file. Error_Handler: ' Raise the error (if any), but first close the file. If isOpen Then Close #fnum If Err Then Err.Raise Err.Number, , Err.Description End Function ' Load a text file into a TextBox control. Text1.Text = ReadTextFileContents("c:\bootlog.txt")
·í»Ýn¼g¤J¸ê®Æ¨ì¤@ÓÀɮ׮ɡA¨Ï¥ÎFor Output¶}±Òn¨ú¥N¤º®eªºÀɮשΥÎFor Appendªþ¥[·sªº¸ê®Æ¨ìÀɮפ¤¡C³q±`¥Î¤@³s¦êªºPrint # ±Ôz±N¿é¥Xµ²ªG¶Ç°e¨ì¿é¥XÀɮסA¦ý¦pªG¶°¦X°_n¿é¥Xµ²ªG¨ì¤@¦r¦ê¡AµM«á¦A¤@¦¸°e¥X·|§ó¬°§Ö³t¡C³o¸Ì¦³Ó±`¨£ªº¤èªk¡G
Sub WriteTextFileContents(Text As String, filename As String, _ Optional AppendMode As Boolean) Dim fnum As Integer, isOpen As Boolean On Error GoTo Error_Handler ' Get the next free file number. fnum = FreeFile() If AppendMode Then Open filename For Append As #fnum Else Open filename For Output As #fnum End If ' If execution flow gets here, the file has been opened correctly. isOpen = True ' Print to the file in one single operation. Print #fnum, Text ' Intentionally flow into the error handler to close the file. Error_Handler: ' Raise the error (if any), but first close the file. If isOpen Then Close #fnum If Err Then Err.Raise Err.Number, , Err.Description End Sub
§Y¨ÏVisdual Basic 6¨S¦³¼W¥[¥ô¦ó³B²z¤å¦rÀɮתº¨ç¦¡¡A¦ý·sªºSplit¨ç¦¡¹ï©ó¤å¦r³B²z«o¬O«D±`¦³¥Î¡C´N¬O»¡¥]§t¶µ¥Øªº¤å¦rÀɮ׳QŪ¨ú¨ìListBox©ÎComboBox±±¨î¶µ¡C¤£¯à¨Ï¥Î¦b¥ý«eª½±µÅª¨ú¶i±±¨î¶µªºReadTextFileContents¡A¦ý¥i¥H¨Ï¥Î¥¦Åýµ{¦¡§ó²©ú¡G
Sub TextFileToListbox(lst As ListBox, filename As String) Dim items() As String, i As Long ' Read the file's contents, and split it into an array of strings. ' (Exit here if any error occurs.) items() = Split(ReadTextFileContents(filename), vbCrLf) ' Load all non-empty items into the ListBox. For i = LBound(items) To UBound(items) If Len(items(i)) > 0 Then lst.AddItem items(i) Next End Sub
³B²z¤À¹jªº¤å¦rÀÉ
¡@
¤À¹jªº¤å¦rÀɮצb¨C¤@¦æ¤å¦r¥]§t¼ÆӰϬq¡C§Y¨Ï¨S¦³³\¦hªºµ{¦¡³]pªÌ¨Ï¥Î¤À¹j¤å¦rÀɨÓÀx¦s«nªºÀ³¥Îµ{¦¡¸ê®Æ¡A¦ý³o¨ÇÀɮפ´µM¦³µÛ«nªº¨¤¦â¡A¦]¬°¥¦Ì´£¨Ñ¤@Ó«nªº¤èªk¦b¤£¦Pªº¸ê®Æ®w®æ¦¡¥æ´«¸ê®Æ¡CÁ|Ó¨Ò¤l¡A¤À¹j¤å¦rÀɳq±`¬OÓ¥i¦æªº¤èªk¥Î¨Ó¶×¥X»P¶×¤J¸ê®Æ¨ì¥D¾÷ªº¸ê®Æ®w¡C³o¸Ì¬OÓ²©öªº¥Î¤À¸¹¤ÀÂ÷ªº¤å¦rÀÉ®×µ²ºc¡C¡]ª`·N²ßºD¤WÀɮתº²Ä¤@¦æ¬OÄæ¦ì¦WºÙ¡C¡^
Name;Department;Salary John Smith;Marketing;80000 Anne Lipton;Sales;75000 Robert Douglas;Administration;70000
Split©MJoin¨ç¦¡¯S§O¥Î¦b¶×¤J©M¶×¥X¤À¹j¤å¦rÀÉ¡CÁ|Ó¨Ò¤l¡A¬Ý¬Ý¥¦¦³¦h®e©ö±N¥Î¤À¸¹¹jÂ÷ªº¸ê®ÆÀÉ¿é¤J¨ì¤@Ó°}¦C¤¤¡G
' The contents of a delimited text file as an array of strings arrays ' NOTE: requires the GetTextFileLines routine Function ImportDelimitedFile(filename As String, _ Optional delimiter As String = vbTab) As Variant() Dim lines() As String, i As Long ' Get all lines in the file. lines() = Split(ReadTextFileContents(filename), vbCrLf) ' To quickly delete all empty lines, load them with a special char. For i = 0 To UBound(lines) If Len(lines(i)) = 0 Then lines(i) = vbNullChar Next ' Then use the Filter function to delete these lines. lines() = Filter(lines(), vbNullChar, False) ' Create a string array out of each line of text ' and store it in a Variant element. ReDim values(0 To UBound(lines)) As Variant For i = 0 To UBound(lines) values(i) = Split(lines(i), delimiter) Next ImportDelimitedFile = values() End Function ' An example of using the ImportDelimitedFile routine Dim values() As Variant, i As Long values() = ImportDelimitedFile("c:\datafile.txt", ";") ' Values(0)(n) is the name of the Nth field. ' Values(i)(n) is the value of the Nth field on the ith record. ' For example, see how you can increment employees' salaries by 20%. For i = 1 to UBound(values) values(i)(2) = values(i)(2) * 1.2 Next
¨Ï¥Î°}¦C¬O¬Û·í¦nªº¤èªk¡A¦]¬°«Ü®e©ö«Ø¥ß·sªº°O¿ý¡G
' Add a new record. ReDim Preserve values(0 To UBound(values) + 1) As Variant values(UBound(values)) = Split("Roscoe Powell;Sales;80000", ";")
©Î§R°£¤w¦s¦bªº°}¦C¡G
' Delete the Nth record For i = n To UBound(values) - 1 values(i) = values(i + 1) Next ReDim Preserve values(0 To UBound(values) _ 1) As Variant
¼g¤J¦r¦ê°}¦C¨ì¤À¹jÀɤ]¬O«Ü²©öªº¤u§@¡A·PÁ³oӫإߦbJoin¨ç¦¡ªº¤èªk¡G
' Write the contents of an array of string arrays to a delimited ' text file. ' NOTE: requires the WriteTextFileContents routine Sub ExportDelimitedFile(values() As Variant, filename As String, _ Optional delimiter As String = vbTab) Dim i As Long ' Rebuild the individual lines of text of the file. ReDim lines(0 To UBound(values)) As String For i = 0 To UBound(values) lines(i) = Join(values(i), delimiter) Next ' Create CRLFs among records, and write them. WriteTextFileContents Join(lines, vbCrLf), filename End Sub ' Write the modified data back to the delimited file. ExportDelimitedFile values(), "C:\datafile.txt", ";"
¦¹³¹¸`©Ò¦³ªº½d¨Ò³£¬O¨Ì¾a¦b°²³]¤À¹jÀɮפp¨ì¥i¥HÀx¦s¦b°O¾ÐÅ餤¡C³oÅ¥°_¨Ó¦³ÂI¾á¤ß¡A¹ê»Ú¤W¤å¦rÀɳq±`¥Î¨Ó«Ø¥ß¤p«¬¸ê®Æ®w©Î¦b¤£¦Pªº¸ê®Æ®w®æ¦¡¤¤²¾°Ê¤Ö¶qªº¸ê®Æ¡C¦pªGµo²{¦³°}¦C®e¶q¤j¤pªº°ÝÃD¡A´N¥²¶·¥Î¤j¶qªºLine Input # ©MPrint # ±Ôz¨ÓŪ¼g¡C¦b¤j³¡¤Àªº±¡ªp¤U¡A§Y¨Ï¤j¨ì1©Î2 megabytesªºÀɮס]©Î§ó¦h¡A¨ú¨M©óRAMªº¤j¤p¡^®É¤´¨S¦³°ÝÃD¡C
³B²z¤G¶i¦ìÀÉ®×
¡@
¬°¤F¶}±Ò¤G¶i¦ìÀÉ¡An¨Ï¥Î¥]§tFor Random©ÎFor Binary¿ï¶µªºOpen±Ôz¡C¥ý¨Ó¸ÑÄÀ«áªÌ¡A¥¦¬O¨âªÌ¤¤¸û¬°Â²³æªº¡C¦bBinary¼Ò¦¡¤U¡A¨Ï¥ÎPut±Ôz¼g¤JÀɮשM¨Ï¥ÎGet±ÔzŪ¨úÀɮסCVisual Basic·|µø³Ì«áªº°Ñ¼ÆªºÅܼƵ²ºc¨Ó¨M©w¦h¤Ö¦ì¤¸³Q¼g¤J©ÎŪ¨ú¡G
Dim numEls As Long, text As String numEls = 12345: text = "A 16-char string" ' Binary files are automatically created if necessary. Open "data.bin" For Binary As #1 Put #1, , numEls ' Put writes 4 bytes. Put #1, , text ' Put writes 16 bytes (ANSI format).
·íŪ¨ú¸ê®Æ®É¡A¥²¶·«½Æ¦P¼ËªºÅܼƪø«×¦r¦ê¡C¤£»ÝnÃö³¬©M«·s¶}±Ò¤@Ó¤G¶i¦ìÀÉ¡A¦]¬°¥i¥H¨Ï¥ÎSeek±Ôz¨Ó§ïÅÜÀɮתº«ü¼Ð¦Ü¤@¯S©wªº¦ì¤¸¡G
Seek #1, 1 ' Back to the beginning (first byte is byte 1) Get #1, , numEls ' All Long values are 4 bytes. text = Space$(16) ' Prepare to read 16 bytes. Get #1, , text ' Do it.
§A¥i¥H¨Ï¥Î²Ä¤GӰѼƦb¼g¤J©ÎŪ¨ú¸ê®Æ«e²¾°ÊÀɮ׫ü¼Ð¡A¦p¦P¦¹µ{¦¡¡G
Get #1, 1, numEls ' Same as Seek + Get
ª`·N
·í¶}±Ò¤@Ó¤G¶i¦ìÀÉ¡A¦pªGÀɮפ£¦s¦b¡AVisual Basic·|¦Û°Ê«Ø¥ß¡C¦]¦¹¡AµLªk¨Ï¥ÎOn Error±Ôz¨Ó§P©wÀɮ׬O§_¤w¦s¦b¡C¦b³oºØ±¡ªp¤U¡A¥i¥H¦b¶}±ÒÀɮ׫e¨Ï¥ÎDir$ ¨ç¦¡½T©wÀɮ׬O§_¦s¦b¡C
¥i¥H¦b¤@Ó¨BÆJ¤¤§Ö³t¦a¼g¤J¥þ³¡ªºÀɮרìºÏºÐ©MŪ¨ú¥X¨Ó¡F¦b¤j³¡¤Àªº¨Ò¤l¡A¥²¶·¦bŪ¨ú«e¥¿½T¦a¤Á³Î°}¦C¡A¦Ó¥B¤]¥²¶·±N¸ê®Æ«e¸m¼Æ¦r¡G
' Store a zero-based array of Double. Put #1, 1, CLng(UBound(arr)) ' First store the UBound value. Put #1, , arr() ' Then store all items in one shot. ' read it back Dim LastItem As Long Get #1, 1, LastItem ' Read the number of items. ReDim arr2(0 To LastItem) As Double Get #1, , arr2() ' Read the array in memory in one operation. Close #1
ª`·N
¦pªG¥Î¤£¦P©ó¼g¤J¶¶§Ç¨ÓŪ¨ú¸ê®Æªº¸Ü¡A¥i¯àŪ¨ú¨ì¿ù»~ªº¸ê®Æ¡C¦b¤@¨Ç±¡ªp¤¤¡A·í¸ÕµÛ¥hÅã¥Ü³o¨ÇÅܼƤº®e®É¡A¥i¯à¤Þ°_Visual BasicÀô¹Ò·l·´¡C°ò©ó³oÓì¦]¡AÁ`¬On«ö¨â¤U·Æ¹«Áä¨Ó¼g¤J©MŪ¨ú¡C¦b¤£½T©wªº±¡ªp¤U¡A¦b°õ¦æµ{¦¡«e¥ýÀx¦s°_¨Ó¡C
·í±q¤@Ó¤G¶i¦ìÀÉŪ¨ú®É¡A±NµLªk¨Ï¥ÎEOF¨ç¦¡§P©w¬O§_¬°¸ê®Æ©³ºÝ¡F¦]¦¹¡AÀ³¥ý´ú¸ÕLOF¨ç¦¡¶Ç¦^ªºÈ¡]Àɮתºªø«×¡^©M¨Ï¥ÎSeek¨ç¦¡¨Ó§PÂ_¬O§_¤w¸gŪ¨ú©Ò¦³ªº¸ê®Æ¤F¡G
Do While Seek(1) < LOF(1) ' Continue to read. .... Loop
¤p¯µ³Z
·íÀx¦s¦r¦ê¦ÜºÏºÐ®É—§Y¨Ï¬O¤å¦r©Î¤G¶i¦ìÀÉ—Visual Basic·|¦Û°Ê±N¥¦Ì±qUnicodeÂà´«¬°ANSI¡A¨ä¸`¬ÙºÏºÐªÅ¶¡¶q©MÅý§A¥i»P16¦ì¤¸ªºVisual BasicÀ³¥Îµ{¦¡¥æ´«¡C¦pªGn¼g¤JUnicodeµ{¦¡¦Ü°ê»Ú¥«³õ¡AµL½×¦p¦ó¡A¦¹ºØ¦æ¬°·|º|¥¢±¼³¡¥÷¸ê®Æ¡A¦]¬°±qÀɮפ¤Åª¨úªº¦r¦ê±N¤£·|²Å¦X¤§«eÀx¦sªº¸ê®Æ¡C¬°¤F׾㦹¿ù»~¡A¥²¶·Âà´«¦r¦ê¬°Byte°}¦CµM«á¦AÀx¦s¡G
Dim v As Variant, s As String, b() As Byte s = "This is a string that you want to save in Unicode format" b() = s: v = b() ' You need this double step. Put #1, , v ' Write that to disk. ' Read it back. Get #1, 1, v: s = v ' No need for intermediary Byte array here.
¥ÎFor Random±Ôz¶}±Ò¤G¶i¦ìÀɤ£¦P©ó¨ì¥Ø«e¬°¤î§Ú©Ò»¡ªº¦³µÛ³\¦h«n¤è±¡G
¥ÎFor Random¶µ¶}±ÒÀx¦s¦b¤G¶i¦ìÀɪº¦r¦ê·|¹w¥ý¸m¤J¤@ÓÅã¥Ü¦r¤¸¼Æªº¨âӦ줸ȡC³oªí¥ÜµLªk¼g¤J¥]§t¶W¹L32,767Ó¦r¤¸ªº¦r¦ê¡A¤]¬O¦Xªk°O¿ýªº³Ì¤j½d³ò¡Cn¼g¤J§ó¤jªº¦r¦ê¡AÀ³¸Ó¨Ï¥ÎFor Binary¶µ¡C
³Ì«á¡G©Ò¦³¥Ø«eªºµ{¦¡½d¨Ò¬Ò°²³]§Ú̬O¦b³æ¤@¨Ï¥ÎªÌªºÀô¹Ò¤U¤u§@¡A©M¤£¦Ò¼{¥Ñ¥t¤@¦W¨Ï¥ÎªÌ¶}±ÒÀɮ׮ɪº¿ù»~¡A©Î¨Ï¥ÎLock±Ôzªº©Ò¦³©Î³¡¥÷¸ê®ÆÀÉ¡]©M¦b¤§«á¥ÎUnlock±Ôz¸ÑÂê¡^¡C§ó¦hªº¸ê°T¡A¨£Visual Basicªº»¡©úÀÉ¡C
¤p¯µ³Z
·í¦b¤G¶i¦ìÀɼg¤J©MŪ¨ú¸ê®Æ®É¤£·QnÀò±o½ÆÂøªºÃB¥~¦ôºâ¡A¥i¥H¨Ì·Ó¸ô®|¨Ï¥Î¸ûµuªº©~¤¤VariantÅܼơC¦pªGÀx¦s¥ô·N§ÎºA¡]°£¤Fª«¥ó¡^ªºÅܼƨìVariantÅܼƵM«á±NÅܼƼg¤J¨ì¤G¶i¦ìÀɤ¤¡AVisual Basic·|¼g¤JÅܼƧκA¡]¦]¦¹¡AVarType·|¦^¶ÇÈ¡^©M¸ê®Æ¡C¦pªGÅܼƾ֦³¦r¦ê©Î°}¦C¡AVisual Basic¤]·|Àx¦s¨¬°÷ªº¸ê°T¥hºë½T¦aŪ¨ú»Ýnªº¦ì¤¸¼Æ¡A±N±qÃB¥~ªºÅª¨ú±Ôz¤¤ÄÀ©ñ¡G
Dim v As Variant, s(100) As String, i As Long ' Fill the s() array with data... (omitted) Open "c:\binary.dat" For Binary As #1 v = s() ' Store the array in a Variant variable, Put #1, , v ' and write that to disk. v = Empty ' Release memory. ' Read data back. Dim v2 As Variant, s2() As String Get #1, 1, v2 ' Read data in the Variant variable, s2() = v2 ' and then move it to the real array. v2 = Empty ' Release memory. Close #1
¦¹¤èªk¤]·|³B²z¦hºûªº°}¦C¡C
FileSystemObject¶¥¼h
¡@
Visual Basic 6¤Þ¶i·sªºÀɮ׫ü¥O®w¡A¯à°÷Åýµ{¦¡³]pªÌ®e©ö¦a±½´yºÏºÐ©M¥Ø¿ý¡A°õ¦æ°ò¥»ªºÀÉ®×¹B§@¡]¥]§t½Æ»s¡B§R°£¡B²¾°Êµ¥µ¥¡^¡A©M±q¤@¯ëªºVisual Basic¨ç¦¡¤¤¨ú¥X¸ê°T¡C¦ý¨Ì§Ú¨Ó¬Ý¡A·s«ü¥O³Ì¯S§Oªº¨Æ¥i¥H¨Ï¥Î·s¼é¡B¤@P©Mª«¥ó¾É¦V»yªk¡A¦¹¯àÅýµ{¦¡§ó®e©ö¾\Äý¡C
¡@
¹Ï5-1 FileSystemObject¶¥¼h |
FileSystemObject®Úª«¥ó
¡@
¶¥¼hªº°_·½¬OFileSystemObject¥»¨¡C¨ä¥]§t³\¦h¤èªk©M°ß¤@ªºÄÝ©Ê¡ADrive¡A¦^¶Ç¨t²Î¤¤©Ò¦³¸Ë¸mªº¶°¦X¡CFileSystemObjectª«¥ó¡]¦p¦P¥X²{¦b±µ¤U¨Óªº¤å³¹©Mµ{¦¡¤¤ªº¤p«¬FSO¡^¬O¼h¯Å¤¤°ß¤@¥i«Ø¥ßªºª«¥ó—¦]¦¹¡A¥¦¬O°ß¤@¥i¥H¥ÎNewÃöÁä¦r¤½¶}ªºª«¥ó¡C¨ä¥L©Ò¦³ªºª«¥ó³£¬O¨ú¦Û³oÓ¦Ó¦A¥]§t³\¦h¤èªk©ÎÄÝ©Ê¡C¬Ý¬Ý¥¦¦³¦h®e©ö±N¨t²Î¸Ë¸m¶ñ¤J¤@Ó°}¦C¡G
Dim fso As New Scripting.FileSystemObject, dr As Scripting.Drive On Error Resume Next ' Needed for not-ready drives For Each dr In fso.Drives Print dr.DriveLetter & " [" & dr.TotalSize & "]" Next
ªí5-3¦C¥X³\¦hFSOª«¥ó©Ò´£¨Ñªº¤èªk¡C¨ä¤¤¤@¨Ç¤]²[»\©óFolder©MFileª«¥ó¤¤¡]³q±`¦³µÛ¤£¬Û¦Pªº¦WºÙ©M»yªk¡^¡C¤j³¡¤Àªº³o¨Ç¤èªk¼W±j¤FVisual Basic«ü¥Oªº¥\¯à¡CÁ|Ó¨Ò¤l¡A¥un¨Ï¥Î¤@Ó«ü¥O´N¥i¥H§R°£«DªÅªº¸ê®Æ§¨¡]«D±`¥J²Óªº¡I¡^¥H¤Î½Æ»s©M§ó¦W¦h¼ÆªºÀɮשM¥Ø¿ý¡C
»yªk | »¡©ú |
---|---|
BuildPath (Path, Name) | ¦^¶Ç§¹¾ãªºÀɮצWºÙ¡A¦Ûqªº¸ô®|¡]¬ÛÃö©Î§¹¾ãªº¡^©M¦WºÙ¡C |
CopyFile Source, Destination , [Overwrite] | ½Æ»s¤@өΦhÓÀɮסGSource¥i¥H¥]§t¸U¥Î¦r¤¸¦ÓDestination«h¥i¥H¤ä´©¥H¤Ï±×½u°µµ²§ôªº¥Ø¿ý¡C°£«D³]Overwrite¬°False§_«h·|½Æ¼g±¼ÀɮסC |
CopyFolder Source, Destination, [Overwrite] | ¦PCopyFile¡A¦ý·|½Æ»s¾ãÓ¸ê®Æ§¨¤Î¨ä¤º®e¡]¤l¥Ø¿ý©MÀɮס^¡C¦pªGDestination¨S¦³¬ÛÃöªº¥Ø¿ý¦s¦b¡A«h·|«Ø¥ß¤@Ó¡]¦ý¦pªGSource¨Ï¥Î¸U¥Î¦r¤¸«h¤£·|¡^¡C |
CreateFolder(Path) As Folder | «Ø¥ß·sªº¸ê®Æ§¨ª«¥ó¨Ã¦^¶Ç¤§¡F¦b¸ê®Æ§¨¤w¸g¦s¦b®É·|µo¥Í¿ù»~¡C |
CreateTextFile(FileName, [Overwrite],[Unicode]) As TextStream | «Ø¥ß·sªº¯Â¤å¦rÀɪ«¥ó¨Ã¦^¶Ç¤§¡F³]©wOverwrite=False¨ÓÁקK½Æ¼g±¼¥H¦s¦bªºÀɮסF³]©wUnicode=True¥H«Ø¥ßUnicodeªº¯Â¤å¦rÀɪ«¥ó¡C |
DeleteFile FileSpec, [Force] | §R°£¤@өΦhÓÀɮסCFileSpec¤ä´©¸U¥Î¦r¤¸¡F³]©w Force=True¥H±j¨î§R°£°ßŪÀɮסC |
DeleteFolder(FolderSpec, [Force]) | §R°£¤@өΦhÓ¸ê®Æ§¨¥H¤Î¨ä¤º®e¡F³]©wForce=True¥H±j¨î§R°£°ßŪÀɮסC |
DriveExists(DriveName) | Yµ¹©wªººÏºÐ¾÷¦s¦b®É¶Ç¦^True |
FileExists(FileName) | ¦pªG«ü©wªºÀɮפw¦s¦b«h¦^¶ÇTrue¡C¡]¨ä¸ô®|¥²¶·²Å¦X¥Ø«eªº¥Ø¿ý¡C¡^ |
FolderExists(FolderName) | ¦pªG«ü©wªº¸ê®Æ§¨Àɮפw¦s¦b«h¦^¶ÇTrue¡C¡]¨ä¸ô®|¥²¶·²Å¦X¥Ø«eªº¥Ø¿ý¡C¡^ |
GetAbsolutePathName(Path) | ²¾°Ê¥Ø«eªº¥Ø¿ý¨ì«ü©wªº¥Ø¿ý¡C |
GetBaseName(Filename) | °õ¦æ°ò¥»ÀɮצWºÙ¡]¤£¥]¬A¸ô®|©M©µ¦ù³¡¥÷¡^¡F¨Ã¤£·|ÀËÅçÀɮשM¡]©Î¡^¸ô®|¬O§_¦s¦b¡C |
GetDrive(DriveName)As Drive | ¦^¶Ç²Å¦X©Ò¶Ç¤J°Ñ¼ÆªºDriveª«¥ó¬ÛÃöªºUNC¸ô®|¡C¡]·|ÀËÅçdrive¬O§_¦s¦b¡^¡C |
GetDriveName(Path) | ±q¸ô®|¤¤¨ú±odrive¡C |
GetExtensionName(FileName) | ±qÀɮצWºÙ¤¤¨ú±o©µ¦ùªº¦r¦ê¡C |
GetFile(FileName) | ¦^¶Ç²Å¦X©Ò¶Ç¤J°Ñ¼ÆªºÀɮת«¥ó¦WºÙ¡C¡]¯à°÷¨ú±o§¹¥þ²Å¦X©Î¬ÛÃöªº¥Ø¿ý¡C¡^ |
GetFileName( | ¨ú±oÀɮצWºÙ¡]¤£¶·¸ô®|¦ý¶·©µ¦ù³¡¥÷¡^¡F¨Ã¤£·|ÀËÅçÀɮשM¡]©Î¡^¸ô®|¬O§_¦s¦b¡C |
GetFolder(FolderName) As Folder | ¦^¶Ç²Å¦X©Ò¶Ç¤J°Ñ¼Æªº¸ê®Æ§¨ª«¥ó¡C¡]¯à°÷¨ú±o§¹¥þ²Å¦X©Î¬ÛÃöªº¥Ø¿ý¡C¡^ |
GetParentFolderName(Path) | ¦^¶Ç²Å¦X©Ò¶Ç¤J°Ñ¼Æªº®Ú¥Ø¿ýªº¦WºÙ¡]©Î¦b®Ú¥Ø¿ý¤£¦s¦bªºª¬ºA¤U¦^¶Ç¤@ӪŦr¦ê¡^¡C |
GetSpecialFolder(SpecialFolder) As Folder | ¦^¶Ç²Å¦X¯S®íªºWindows¥Ø¿ý¤¤ªº¸ê®Æ§¨ª«¥ó¡CSpecialFolder¥i¥H¬°0-WindowsFolder¡B1-SystemFolder¡B2-TemporaryFolder¡C |
GetTempName() | ¦^¶Ç³Q¥Î¨Ó·í§@¼È¦sÀɪº«D¹ê½èÀɮסC |
MoveFile (Source, Destination) | ¦PCopyFile¡A¦ý¥¦·|§R°£µ{¦¡ÀɮסC¤]¥i¥Î¦bdrives¤¤²¾°Ê¡A¦pªG¦¹¨ç¦¡³Q§@·~¨t²Î©Ò¤ä´©ªº¸Ü¡C |
MoveFolder(Source, Destination) | ¦PMoveFile¡A¦ý¶È¯à³B²z¥Ø¿ý¡C |
OpenTextFile(FileName, [IOMode], [Create], [Format])As TextStream | ¶}±Ò¤@ӯ¤å¦rÀɨæ^¶Ç¬ÛÃöªºTextStreamª«¥ó¡CIOMode¥i¥H¬O¤@өΦhÓ¤U¦C±Ôzªº¶°¦X¡G1-ForReading¡B2-ForWriting¡B8-ForAppending¡F¦pªGn«Ø¥ß·sÀɮ׫h³]Create¬°True¡FFormat¥i¥H¬O0-TristateFalse¡]ANSI¡^¡B-1-TristateTrue¡]Unicode¡^©Î-2-TristateUseDefault¡]¨Ì¨t²Î¨M©wdetermined by the system¡^¡C |
ªí5-3 FileSystemObjectª«¥óªº©Ò¦³¤èªk |
Driveª«¥ó
¡@
¦¹ª«¥ó¶È¾Ö¦³ÄÝ©Ê¡]¨S¦³¤èªk¡^¡A¨ä·§n»¡©ú¥þ¦bªí5-4¡C©Ò¦³ªºÄݩʳ£¬O°ßŪªº¡A°£¤FVolumeNameÄÝ©Ê¥~¡C³o¸Ìªºµ{¦¡¤ù¬q¯à±±¨î³Ì¤Ö100 MBªÅ¶¡ªº¥»¾÷¸Ë¸m¡G
Dim fso As New Scripting.FileSystemObject, dr As Scripting.Drive For Each dr In fso.Drives If dr.IsReady Then If dr.DriveType = Fixed Or dr.DriveType = Removable Then ' 2 ^ 20 equals one megabyte. If dr.FreeSpace > 100 * 2 ^ 20 Then Print dr.Path & " [" & dr.VolumeName & "] = " _ & dr.FreeSpace End If End If End If Next
»yªk | »¡©ú |
---|---|
AvailableSpace | ºÏºÐªº³Ñ¾lªÅ¶¡¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡F¥¦³q±`©MFreeSpaceÄݩʦb¤@°_¡Aª½¨ì§@·~¨t²Î¹F¨ìºÏºÐªÅ¶¡¡C |
DriveLetter | ÃöÁp¨ìºÏºÐ¾÷ªº¥N¸¹©ÎÃöÁp¨ìºô¸ôºÏºÐªºªÅ¦r¦ê¡C |
DriveType | ªí¥ÜºÏºÐ§ÎºAªº±`¼Æ¡G0-Unknown¡B1-Removable¡B2-Fixed¡B3-Remote¡B4-CDRom¡B5-RamDisk¡C |
FileSystem | Àɮרt²Î©Ò¨Ï¥Îªº®æ¦¡¡GFAT¡BNTFS¡BCDFS¡C |
FreeSpace | ºÏºÐ¤¤³Ñ¾lªºªÅ¶¡¡C¡]¨£AvailableSpace¡C¡^ |
IsReady | ¦pªGºÏºÐ¦s¦b«h¬°True¡A¤Ï¤§«hFalse¡C |
Path | ¥ÑºÏºÐ²Õ¦¨ªº¸ô®|¡A¤£¥]§t¤Ï±×½u¡]¦pC:¡^¡C |
RootFolder | ²Å¦X®Ú¥Ø¿ýªº¸ê®Æ§¨ª«¥ó¡C |
SerialNumber | ²Å¦X³sÄòªººÏºÐ¼Æ¥Øªºªø¾ã¼Æ¡C |
ShareName | ºô¸ô¤À¨ÉªººÏºÐ¦WºÙ©Î¦b¦pªG¨S¦³ºô¸ôºÏºÐ¤U¬°ªÅ¦r¦ê¡C |
TotalSize | ¥þ³¡ªººÏºÐ®e¶q¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡C |
VolumeName | ºÏºÐ¼ÐÅÒ¡]¥i¥HŪ¼g¡^¡C |
ªí5-4 Driveª«¥óªº©Ò¦³ÄÝ©Ê |
Folderª«¥ó
¡@
¸ê®Æ§¨ª«¥óªí¥ÜÓ§O¤l¥Ø¿ý¡CÀò±o¦¹ª«¥ó¦³¤£¦Pªº¤èªk¡GÂÇ¥ÑFileSystemObjectª«¥óªºGetFolder©MGetSpecialFolder¤èªk¡B¸g¥Ñ¸Ë¸mª«¥óªºRootFolderÄÝ©Ê¡B¸g¥ÑÀɮת«¥ó©Î¥t¤@Ó¸ê®Æ§¨ª«¥óªºParentFolderÄÝ©Ê©ÎÂǥѤÏÂÐ¥t¤@Ó¸ê®Æ§¨ª«¥óªºSubFolders¶°¦X¡C¸ê®Æ§¨ª«¥ó¾Ö¦³¤@¨Ç¦³½ìªºÄÝ©Ê¡]¨£ªí5-5¡^¡A¦ý¥u¦³Attribute©MNameÄݩʯà³Qקï¡C³Ì¥O¤H¦n©_ªºÄÝ©ÊÀ³¸Ó´NÄÝSubFolders©MFiles¶°¦X¡A¯à¸g¥Ñ¤l¥Ø¿ý©MÀɮרϥÎÀu¬ü©M²¼äªº»yªk¨Ó¤ÏÂаõ¦æ¡G
' Print the names of all first-level directories on all drives ' together with their short 8.3 names. Dim fso As New Scripting.FileSystemObject Dim dr As Scripting.Drive, fld As Scripting.Folder On Error Resume Next For Each dr In fso.Drives If dr.IsReady Then Print dr.RootFolder.Path ' The root folder. For Each fld In dr.RootFolder.SubFolders Print fld.Path & " [" & fld.ShortName & "]" Next End If Next
»yªk | »¡©ú | À³¥Î¨ì |
---|---|---|
Attributes | Àɮשθê®Æ§¨ªºÄÝ©Ê¡A¥H¤U±`¼Æªº¶°¦X¡G0- Normal¡B1-ReadOnly¡B2-Hidden¡B4-System¡B8-Volume¡B16-Directory32-Archive¡B64-Alias¡B2048-Compressed¡CÄݩʬ°Volume¡BDirectory¡BAlias©MCompressed¤£¯àקï¡C | ¸ê®Æ§¨©MÀÉ®× |
DateCreated | «Ø¥ßªº¤é´Á¡]°ßŪªº¤é´ÁÈ¡^¡C | ¸ê®Æ§¨©MÀÉ®× |
DateLastAccessed | ³Ì«á¦s¨úªº¤é´Á¡]°ßŪªº¤é´ÁÈ¡^¡C | ¸ê®Æ§¨©MÀÉ®× |
DateLastModified | ³Ì«áק諸¤é´Á¡]°ßŪªº¤é´ÁÈ¡^¡C | ¸ê®Æ§¨©MÀÉ®× |
Drive | Àɮשθê®Æ§¨©Ò¦bªºDriveª«¥ó¡C | ¸ê®Æ§¨©MÀÉ®× |
Files | ©Ò¦³Àɮת«¥óªº¶°¦X¡C | ¸ê®Æ§¨±MÄÝ |
IsRootFolder | ¦pªG¬O¸ÓºÏºÐªº®Ú¥Ø¿ý«h¬°True¡C | ¸ê®Æ§¨±MÄÝ |
Name | ¸ê®Æ§¨©ÎÀɮתº¦WºÙ¡C¯à¤À°t·sªº¦WºÙµ¹ª«¥ó | ¸ê®Æ§¨©MÀÉ®× |
ParentFolder | ¥À¸ê®Æ§¨ªºª«¥ó¡C | ¸ê®Æ§¨©MÀÉ®× |
Path | ¸ê®Æ§¨©ÎÀɮתº¸ô®|¡C¡]¹w³]ÄÝ©Ê¡C¡^ | ¸ê®Æ§¨©MÀÉ®× |
ShortName | 8.3 MS-DOS®æ¦¡ªº¦WºÙª«¥ó¡C | ¸ê®Æ§¨©MÀÉ®× |
ShortPath | 8.3 MS-DOS®æ¦¡ªº¸ô®|ª«¥ó¡C | ¸ê®Æ§¨©MÀÉ®× |
Size | ¥H¦ì¤¸²Õ¬°³æ¦ìªºÀɮת«¥ó¤j¤p¡F¨äÁ`©M¥]¬AÀɮשM¸ê®Æ§¨ª«¥óªº¤l¥Ø¿ý¡C | ¸ê®Æ§¨©MÀÉ®× |
SubFolders | ¥]§t¦b¸Ó¸ê®Æ§¨ªº©Ò¦³¤l¸ê®Æ§¨¶°¦X¡A¥]§t¨t²Î©ÎÁôÂ꺸ê®Æ§¨¡C | ¸ê®Æ§¨±MÄÝ |
Type | ª«¥óªº±Ôz¡C¨Ò¦p¡Gfso.GetFolder("C:\Recycled").Type·|¦^¶Ç¡u¸ê·½¦^¦¬µ©¡v¡F¹ï©óÀɮת«¥ó¡A¦¹È·|ªí¥Ü¥L̪º©µ¦ù±¡§Î¡]¨Ò¦p"Text Document"¹ï©ó¯Â¤å¦r¡^ | ¸ê®Æ§¨©MÀÉ®× |
ªí5-5 Folder©MFileª«¥óªº©Ò¦³ÄÝ©Ê |
¸ê®Æ§¨ª«¥ó¤]¦³¤@¨Ç¤èªk¡AÁ`¬A¦bªí5-6¡Cª`·N¨ä±`±`¯à¨Ï¥Î¥DnªºFSOª«¥óªº¾A·í¤èªk¨Ó¹F¨ìÃþ¦üªºµ²ªG¡C¤]¥i¥Hn¨DSubFolders¶°¦XªºAdd¤èªk¨Ó«Ø¥ß·sªº¸ê®Æ§¨¡A¦p¥H¤Uªº»¼°j½d¨Ò¡A½Æ»s¤@ӺϺФ¤ªº¥Ø¿ýµ²ºc¨ì¥t¤@ӺϺФ¤¦Ó¤£¶·½Æ»sÀɮסG
' Call this routine to initiate the copy process. ' NOTE: the destination folder is created if necessary. Sub DuplicateDirTree(SourcePath As String, DestPath As String) Dim fso As New Scripting.FileSystemObject Dim sourceFld As Scripting.Folder, destFld As Scripting.Folder ' The source folder must exist. Set sourceFld = fso.GetFolder(SourcePath) ' The destination folder is created if necessary. If fso.FolderExists(DestPath) Then Set destFld = fso.GetFolder(DestPath) Else Set destFld = fso.CreateFolder(DestPath) End If ' Jump to the recursive routine to do the real job. DuplicateDirTreeSub sourceFld, destFld End Sub Private Sub DuplicateDirTreeSub(source As Folder, destination As Folder) Dim sourceFld As Scripting.Folder, destFld As Scripting.Folder For Each sourceFld In source.SubFolders ' Copy this subfolder into destination folder. Set destFld = destination.SubFolders.Add(sourceFld.Name) ' Then repeat the process recursively for all ' the subfolders of the folder just considered. DuplicateDirTreeSub sourceFld, destFld Next End Sub
»yªk | »¡©ú | À³¥Î¨ì |
---|---|---|
Copy Destination, [OverWriteFiles] | ½Æ»s¥Ø«eªºÀɮשθê®Æ§¨ª«¥ó¨ì¥t¤@Ó¸ô®|¡FÃþ¦üFSOªºCopyFolder©MCopyFile¤èªk¡A¤]¯à°÷½Æ»s¦PÓ°Ï°ì¦h¼Æªºª«¥ó¡C | ¸ê®Æ§¨©MÀÉ®× |
CreateTextFile(FileName, [Overwrite], [Unicode]) As TextStream | ¦b¥Ø«eªº¸ê®Æ§¨¤¤«Ø¥ß¯Â¤å¦rÀÉ¥B¦^¶Ç²Å¦XªºTextStreamª«¥ó¡C¸Ô¨£¬ÛÃöªºFSO¤èªk¤¤ªº°Ñ¼Æ»¡©ú¡C | ¸ê®Æ§¨±MÄÝ |
Delete [Force] | §R°£¸ÓÀɮשθê®Æ§¨ª«¥ó¡]¥]§t¸Ì±ªº¤l¥Ø¿ý©MÀɮס^¡CÃþ¦üFSOªºDeleteFile©MDeleteFolder¤èªk¡C | ¸ê®Æ§¨©MÀÉ®× |
Move DestinationPath | ²¾°Ê¸ÓÀɮשθê®Æ§¨¨ì¥t¤@Ó¸ô®|¡FÃþ¦üFSOªºMoveFile©MMoveFolder¤èªk¡C | ¸ê®Æ§¨©MÀÉ®× |
OpenAsTextStream([IOMode], [Format]) As TextStream | ¶}±Ò¯Â¤å¦rÀɪ«¥ó¨Ã¦^¶Ç¬ÛÃöªºTextStreamª«¥ó¡C | ¸ê®Æ§¨±MÄÝ |
ªí5-6 Folder©MFileª«¥óªº©Ò¦³¤èªk |
Fileª«¥ó
¡@
Fileª«¥ó¥NªíºÏºÐªº³æ¤@ÀɮסC¦³¨âÓ¤èªk¨Ï¥Î¦¹ª«¥ó¡GÂÇ¥ÑFSOª«¥óªºGetFile¤èªk©Î¤ÏÂШä쥻¸ê®Æ§¨ª«¥óªºFiles¶°¦X¡C¾¨ºÞ¨âªÌ¦³¤£¦Pªº¯SÂI¡AFile©MFolderª«¥ó³£¾Ö¦³«Ü¦hªº¦@¦PÄÝ©Ê©Mª«¥ó¡A¦]¦¹´N¤£«½Æ±Ôzªí5-5©M5-6¤F¡C
FSO¶¥¼hªº¨î¦b©ó¨S¦³¨Ï¥Î¸U¥Î¦r¤¸ªºª½±µ¤èªk¨Ó¹LÂoÀɮצWºÙ¡A¦]¦¹¥i¥H¸ÕµÛ©MDir$ ¨ç¦¡¤@°_¨Ï¥Î¡C©Ò¯à°µªº´N¬O¤ÏÂиê®Æ§¨ªºFiles¶°¦X©M´ú¸ÕÀɮתº¦WºÙ¡B©µ¦ù©Î¨ä¥LªºÄÝ©Ê¡A¦p¤U©Ò¥Ü¡C
' List all the DLL files in the C:\WINDOWS\SYSTEM directory. Dim fso As New Scripting.FileSystemObject, fil As Scripting.File For Each fil In fso.GetSpecialFolder(SystemFolder).Files If UCase$(fso.GetExtensionName(fil.Path)) = "DLL" Then Print fil.Name End If Next
FileSystemObject¶¥¼h¤£¤¹³\Àɮפ豪º³\¦h¾Þ§@¡C¤×¨ä¦b¦C¥X¥¦ÌªºÄÝ©Ê¡]¥]¬A¦b쥻VBAÀɮר禡¥\¯àªº³\¦h¯S©Ê¡^®É¡A¶È¯à°÷±NÀÉ®×¥H¤å¦r¼Ò¦¡¶}±Ò¡A¦p¦P¤UÓ³¹¸`©Ò¸ÑÄÀ¡C
TextStreamª«¥ó
¡@
TextStreamª«¥ó¥Nªí¥H¤å¦r¼Ò¦¡¶}±ÒªºÀɮסC¥i¥H¥Î¤U¦C¤èªkÀò±o¡GÂÇ¥ÑFSOª«¥óªºCreateTextFile©ÎOpenTextFile¤èªk¡BÂǥѸê®Æ§¨ª«¥óªºCreateTextFile¤èªk©Î¸g¥Ñ¨Ï¥ÎÀɮת«¥óªºOpenAsTextStream¤èªk¡CTextStreamª«¥ó¾Ö¦³¤@¨Ç¤èªk©M°ßŪªºÄÝ©Ê¡A¨ä¥þ³¡¤¶²Ð¦bªí5-7¡CTextStreamª«¥ó´£¨Ñ¤@¨ÇÀɮ׫ü¥O—Á|¨Ò¨Ó»¡¡A·íŪ¼g¤å¦rÀɮɡApºâ¦æ»P¦C¡C¦¹¯S©Êªº¦n³B¦b©ó«½Æ±½´y¥Ø¿ý¤¤©Ò¦³TXTÀɸ̱§t¦³ªº¦r¦ê¡AµM«á¦^¶Ç¥]§t©Ò¦³¾Ö¦³¦¹¦r¦êªºÀɮצWºÙ¡A¥H¤Î¦r¦ê©Ò¦b¦æ¦Cªºµ²ªG°}¦C¡]¹ê»Ú¤W¬O°}¦C¤¤ªº°}¦C¡^¡G
' For each TXT file that contains the search string, the function ' returns a Variant element that contains a 3-item array that holds ' the filename, the line number, and the column number. ' NOTE: all searches are case insensitive. Function SearchTextFiles(path As String, search As String) As Variant() Dim fso As New Scripting.FileSystemObject Dim fil As Scripting.File, ts As Scripting.TextStream Dim pos As Long, count As Long ReDim result(50) As Variant ' Search for all the TXT files in the directory. For Each fil In fso.GetFolder(path).Files If UCase$(fso.GetExtensionName(fil.path)) = "TXT" Then ' Get the corresponding TextStream object. Set ts = fil.OpenAsTextStream(ForReading) ' Read its contents, search the string, close it. pos = InStr(1, ts.ReadAll, search, vbTextCompare) ts.Close If pos > 0 Then ' If the string has been found, reopen the file ' to determine string position in terms of (line,column). Set ts = fil.OpenAsTextStream(ForReading) ' Skip all preceding characters to get where ' the search string is. ts.Skip pos _ 1 ' Fill the result array, make room if necessary. count = count + 1 If count > UBound(result) Then ReDim Preserve result(UBound(result) + 50) As Variant End If ' Each result item is a 3-element array. result(count) = Array(fil.path, ts.Line, ts.Column) ' Now we can close the TextStream. ts.Close End If End If Next ' Resize the result array to indicate number of matches. ReDim Preserve result(0 To count) As Variant SearchTextFiles = result End Function ' An example that uses the above routine: search for a name in all ' the TXT files in E:\DOCS directory, show the results in ' the lstResults ListBox, in the format "filename [line, column]". Dim v() As Variant, i As Long v() = SearchTextFiles("E:\docs", "Francesco Balena") For i = 1 To UBound(v) lstResults.AddItem v(i)(0) & " [" & v(i)(1) & "," & v(i)(2) & "]" Next
ÄݩʩΤèªk | »yªk | »¡©ú |
---|---|---|
ÄÝ©Ê | AtEndOfLine | ¦pªGÀɮ׫ü¼Ð¦b³Ì«á¤@¦æ«h¬°True¡C |
ÄÝ©Ê | AtEndOfFile | ¦pªGÀɮ׫ü¼Ð¦bÀɮתºµ²§À«h¬°True¡]Ãþ¦üVBAªºEOF¨ç¦¡¡^¡C |
¤èªk | Close | Ãö³¬Àɮס]Ãþ¦üVBAªºClose±Ôz¡^¡C |
ÄÝ©Ê | Column | ¥Ø«e©Ò¦bªº¦C¼Æ¡C |
ÄÝ©Ê | Line | ¥Ø«eªº¦æ¼Æ¡C |
¤èªk | Read(Characters) | Ū¨ú¦r¤¸ªº¼Æ¥Ø¨Ã¦^¶Ç¦r¦ê¡]Ãþ¦üVBAªº Input$ ¨ç¦¡¡^¡C |
¤èªk | ReadAll() | Ū¨ú¥þ³¡ªºÀɮרì¦r¦ê¤¤¡]Ãþ¦üVBA¤¤©M LOF¨ç¦¡¤@°_¨Ï¥ÎªºInput$ ¨ç¦¡¡^¡C |
¤èªk | ReadLine() | Ū¨ú¤U¤@¦æªº¤å¦r¨Ã¦^¶Ç¦r¦ê¡]Ãþ¦üVBAªº Line Input±Ôz¡^ |
¤èªk | Skip Characters | ²¤¹L¯S©wªº¦r¤¸¼Æ¡C |
¤èªk | SkipLine | ²¤¹L¤@¦æ¤å¦r¡C |
¤èªk | Write Text | ¼g¤J¤@¦ê¦r¤¸¡A¤£¥]§tNewline¦r¤¸¡]Ãþ¦ü¥]§t¤À¸¹ªºPrint$ «ü¥O¡^¡C |
¤èªk | WriteBlankLines Lines | ¼g¤J«ü©wªºªÅ¥Õ¦æ¼Æ¡]Ãþ¦ü¨S¦³°Ñ¼Æªº¤@өΦhÓPrint# «ü¥O¡^¡C |
¤èªk | WriteLine [Text] | ¼g¤J¤@¦ê¦r¤¸¡A¥]§tNewline¦r¤¸¡]Ãþ¦ü¤£¥]§t¤À¸¹ªºPrint$ «ü¥O¡^¡C |
ªí5-7 TextStreamª«¥óªº©Ò¦³ÄÝ©Ê©M¤èªk¡C |
©Mµøµ¡ªº¤¬°Ê
¡@
¦Ü¥Ø«e¬°¤î¡A§Ú̶°¤¤¦b¤¶²Ð¿W¥ßªºÀ³¥Îµ{¦¡¤W¦Ó¨S©M¥~¬É±µÄ²¡C¦ý¦b³\¦h®É¨è¡A·|»ÝnÅýÀ³¥Îµ{¦¡©M¥~¦bÀô¹Ò°µ¤¬°Ê¡A¥]¬A©M¨ä¥LªºÀ³¥Îµ{¦¡¤@¦P°õ¦æ¡C¦¹¸`¾É¤J¦¹¥DÃD¨Ã¤¶²Ð¤@¨Ç§Þ¥©¨ÓºÞ²z¤¬°Êªº±¡§Î¡C
Appª«¥ó
¡@
Appª«¥ó¥ÑVisual Basic¨ç¦¡®w©Ò´£¨Ñ¡A¥B¥Nªí¥¿¦b°õ¦æªºÀ³¥Îµ{¦¡¡CAppª«¥ó¾Ö¦³³\¦hÄÝ©Ê©M¤èªk¡A³\¦h³£¬O»á¬°¶i¶¥¡A¦b¦¹®Ñ«áÄò³¹¸`·|¦³»¡©ú¡C
EXEName©MPathÄݩʦ^¶Ç¥i°õ¦æÀɮתº¦WºÙ»P¸ô®|¡]¦pªG¥u¬O³æ¿W°õ¦æªºEXEÀÉ¡^©Îª«¥ó¦WºÙ¡]Y¦b¥~¦bÀô¹Ò¤U°õ¦æ¡^¡C³o¨ÇÄݩʳq±`¦P®É³Q¨Ï¥Î - Á|Ó¨Ò¤l¡An§ä¨ì¸ò¥i°õ¦æÀɦs©ñ¦b¦P¼Ë¥Ø¿ý¥B¾Ö¦³¦P¼Ë¥DÀɦWªºINIÀÉ¡G
IniFile = App.Path & IIf(Right$(App.Path, 1) <> "\", "\", "") _ & App.EXEName & ".INI" Open IniFile For Input As #1 ' and so on.
App.PathÄݩʪº¥t¤@Ó±`¥Îªk«h¬O³]©w¥Ø«eªº¥Ø¿ý¬°À³¥Îµ{¦¡ªº¥Ø¿ý¡A¥H«K©óÅý¥¦ªº±qÄÝÀɮׯà³Q§ä¨ì¦Ó¤£»Ýn«ü©w¨ä§¹¾ã¸ô®|¡G
' Let the application's directory be the current directory. On Error Resume Next ChDrive App.Path: ChDir App.Path
ª`·N
¥ý«eªº¤ù¬qµ{¦¡¦b¬Y¨Ç±¡ªp¤U¥i¯à·|¿ù»~¡A¤×¨ä¦b·íVisual BasicªºÀ³¥Îµ{¦¡¦b»·ºÝºô¸ô¦øªA¾¹¤W±Ò°Ê®É¡Cµo¥Íªºì¦]¦b©óApp.PathÄÝ©Ê·|¦^¶ÇUNC¸ô®|¡]¨Ò¦p¡A\\servername\dirname\...¡^¡A¦ÓChDrive«ü¥O¤£·|³B²z³oÃþªº¸ô®|¡C°ò©ó³oÓì¦]¡AÀ³¸Ó¨¾¤î¦¹µ{¦¡¤Þµo·N¥~ªº¿ù»~¡A¥BÁÙn´£¨Ñ¨Ï¥ÎªÌ§Oªº¤èªk¨ÓÅýÀ³¥Îµ{¦¡¥u¨ì¦Û¤vªº¥Ø¿ý¡]¨Ò¦p¡AÂǥѦb¨t²ÎRegistry³]©w¤@ÁäÈ¡^¡C
PrevInstanceÄݩʯà°÷§PÂ_¬O§_¬OÀ³¥Îµ{¦¡ªº¨ä¥L¡]³Q½sĶªº¡^¹êÅé¹B§@©ó¨t²Î¤W¡CYn¹w¨¾¨Ï¥ÎªÌ·N¥~¦a°õ¦æ¨â¦¸À³¥Îµ{¦¡®É«Ü¦³®Ä¡G
Private Sub Form_Load() If App.PrevInstance Then ' Another instance of this application is running. Dim saveCaption As String saveCaption = Caption ' Modify this form's caption so that it isn't traced by ' the AppActivate command. Caption = Caption & Space$(5) On Error Resume Next AppActivate saveCaption ' Restore the Caption, in case AppActivate failed. Caption = saveCaption If Err = 0 Then Unload Me End If End Sub
¦³¤@¹ïÄݩʬO¥iŪªº¡A¥B©ó°õ¦æ®É´Á¥i³Qקï¡CTaskVisible BooleanÄݩʨM©wÀ³¥Îµ{¦¡¬O§_nÅã¥Ü¦b¤u§@¦C¤W¡CTitleÄÝ©Ê«h¬O©w¸qÀ³¥Îµ{¦¡¦bWindows¤u§@¦Cªº¦r¦ê¡C¨äªì©lȬO¦b³]p®É´Á±M®×Äݩʵøµ¡ªº»s¦¨¶ÅÒ¤¤©Ò¿é¤Jªº¦r¦ê¡C
Appª«¥óªº¨ä¥LÄݩʶǦ^©ó³]p®É´Á¦b±M®×Äݩʵøµ¡ªº¤@¯ë©M»s¦¨¶ÅÒ¤º©Ò¿é¤Jªº¤å¦r¡C¡]¨£¹Ï5-2¡^¡C¨Ò¦p¡AHelpFileÄݩʬO¬ÛÃöªº¨D§UÀɮצWºÙ¡A¦pªG¦³ªº¸Ü¡CUnattendedApp©MRetainedProjectÄݩʹïÀ³¨ì¹ï¸Üµøµ¡¤@¯ë¶ÅÒªº¬ÛÃö®Ö¿ï¶s¡C¡]¦ý¨ä·N¸q¤À§O¦b
16 ©M 20 ³¹¤¤·|°µ»¡©ú¡^¡CMajor¡BMinor©MRevisionÄÝ©Ê·|¦^¶ÇÃö©ó°õ¦æµ{¦¡ªºª©¥»¸ê°T¡CComments¡BCompanyName¡BFileDescription¡BLegalCopyright¡BLegalTrademark©MProductNameÄÝ©Ê¥iÅý§A©ó°õ¦æ®É´Á¬d¸ß¦b±M®×Äݩʵøµ¡ªº»s¦¨¶ÅÒ¤¤©Ò¿é¤Jªº¸ê®Æ¡C¥¦Ì³q±`¦b«Ø¥ßÃö©ó¹ï¸Ü¤è¶ô©Î¶}ÀYµe±®É¯S§O¦³¥Î¡C
¡@
¹Ï5-2 ±M®×Äݩʵøµ¡ªº¤@¯ë©M»s¦¨¶ÅÒ |
Clipboardª«¥ó
¡@
¦bWindows 9x©MWindows NTªº32¦ì¤¸¥@¬É¤¤¡A¸g¥Ñ¨t²Îªº°Å¶Kï¸ò¨ä¥LªºÀ³¥Îµ{¦¡¥æ´«¸ê®Æ¦ü¥GÅã±o¦³ÂI¹L®É¡A¦ý¨Æ¹ê¤W¹ï©ó¨Ï¥ÎªÌ¦Ó¨¥¡A°Å¶Kï¨ÌµM¬O³Ì²³æ¤]³Ì¦³®Ä¥Î¨Ó¦bÀ³¥Îµ{¦¡¶¡§Ö³t½Æ»s¸ê®Æªº¤èªk¡CVisual Basic¯à¥ÎClipboard¼s°ìª«¥ó±±¨î¨t²Îªº°Å¶Kï¡C¬Û¹ï©ó¨ä¥LªºVisual Basicª«¥ó¡A¥¦ºâ¬O¬Û·í³æ¯Âªº¡A¥u¦³¤»Ó¤èªk¥B¨S¦³ÄÝ©Ê¡C
½Æ»s©M¶K¤W¤å¦r
¡@
¨ú¥N°Å¶K襤¤@¬q¤å¦r¡A¨Ï¥ÎSetText¤èªk¡G
Clipboard.SetText Text, [Format]
¨ä¤¤format¥i¬°1-vbCFText¡]¯Â¤å¦r¡A¹w³]È¡^¡B&HBF01-vbCFRTF¡]RTF¤å¦r®æ¦¡¡^©Î&HBF00-vbCFLink¡]DDE¸ê°T¡^¡C¦¹°Ñ¼Æ¬O¥²»Ýªº¡A¦]¬°°Å¶Kï¥i¥H¦UºØ®æ¦¡¨Ó¤ù¬q¸ê°T¡CÁ|Ó¨Ò¤l¡A¦pªG¦³ÓRichTextBox±±¨î¶µ¡]¤@ºØMicrosoft ActiveX±±¨î¶µ¡A ²Ä12³¹ ·|¦³»¡©ú¡^¡A«h¥i±N©Ò¿ï¨úªº¤å¦rÀx¦s¦¨vbCFText©ÎvbCFRTF®æ¦¡¡A¥BÅý¨Ï¥ÎªÌ±N³o¨Ç¤å¦r¥Î«ê·íªº®æ¦¡¶K¦b¦X¾Aªº¥Ø¼Ð±±¨î¶µ¤º¡C
Clipboard.Clear Clipboard.SetText RichTextBox1.SelText ' vbCFText is the default. Clipboard.SetText RichTextBox1.SelRTF, vbCFRTF
ª`·N
¦b¬Y¨Ç±¡ªp»P¥~³¡À³¥Îµ{¦¡±¡ªp¤U¡A¨ú¥N°Å¶K襤ªº¤å¦r¤£·|¥¿±`¹B§@¡Aª½¨ì¨Ï¥ÎClear¤èªk«¸mClipboardª«¥ó¡A¦p¥ý«eµ{¦¡©Ò¥Ü¡C
¨Ï¥ÎGetText¤èªk¥i¥H¨ú±o°Å¶K襤ªº¤å¦r¡C¥i¨Ï¥Î¤U¦C»yªk«ü©w©ÒnÀò±oªº®æ¦¡¡G
' For a regular TextBox control Text1.SelText = Clipboard.GetText() ' You can omit vbCFText. ' For a RichTextBox control RichTextBox1.SelRTF = Clipboard.GetText(vbCFRTF)
¤@¯ë¨Ó»¡¡A¤£ª¾¹D°Å¶Kï¬O§_¯uÀx¦sRTF®æ¦¡ªº¤å¦r¡A©Ò¥HÀ³n¨Ï¥ÎGetFormat¤èªk´ú¸Õ¥¦Ìªº¤º®e¡A¦¹¨ç¼Æ»Ýn¤@Ӯ榡§@¬°°Ñ¼Æ¡A¨Ã¦^¶ÇBooleanȨӪí¥Ü°Å¶K諸®æ¦¡¬O§_¬°¸Ó¶Ç¤J®æ¦¡¡G
If Clipboard.GetFormat(vbCFRTF) Then ' The Clipboard contains data in RTF format. End If
FormatªºÈ¥i¥H¬O1-vbCFText¡]¯Â¤å¦r¡^¡B2-vbCFBitmap¡]ÂI°}¹Ï¡^¡B3-vbCFMetafile¡]metafile¡^¡B8-vbCFDIB¡]»P³]³ÆµLÃöªºÂI°}¹Ï¡^¡B9-vbCFPalette¡]¦â±m½d¥»¡^¡B&HBF01-vbCFRTF¡]RTF¤å¦r®æ¦¡¡^©Î&HBF00-vbCFLink¡]DDE·¾³q¸ê®Æ¡^¡C¦¹¬°±N¤å¦r¶K¤JRichTextBox±±¨î¶µªº¥¿½T¤èªk¡G
If Clipboard.GetFormat(vbCFRTF) Then RichTextBox1.SelRTF = Clipboard.GetText(vbCFRTF) ElseIf Clipboard.GetFormat(vbCFText) Then RichTextBox1.SelText = Clipboard.GetText() End If
½Æ»s©M¶K¤W¹Ï¹³
¡@
·í¨Ï¥ÎPictureBox©MImage±±¨î¶µ®É¡A¥i¥ÎGetData¤èªk¨ú±oÀx¦s¦b°Å¶K諸¹Ï¹³¡A¦¹¤èªk¤]»Ýn®æ¦¡°Ñ¼Æ¡]vbCFBitmap¡BvbCFMetafile¡BvbCFDIB©ÎvbCFPalette¡Bmage±±¨î¶µ¡A¥u¯à¨Ï¥ÎvbCFBitmap¡^¡C¥¿½Tªº¤èªk¬°¡G
Dim frmt As Variant For Each frmt In Array(vbCFBitmap, vbCFMetafile, _ vbCFDIB, vbCFPalette) If Clipboard.GetFormat(frmt) Then Set Picture1.Picture = Clipboard.GetData(frmt) Exit For End If Next
¨Ï¥ÎSetData¤èªk¥i½Æ»sPictureBox©ÎImage±±¨î¶µªº¤º®e¨ì°Å¶K襤¡G
Clipboard.SetData Picture1.Picture ' You can also load an image from disk onto the clipboard. Clipboard.SetData LoadPicture("c:\myimage.bmp")
³q¥Îªº½s¿è¿ï³æ
¡@
¦b³\¦hWindowsÀ³¥Îµ{¦¡¤¤¡A©Ò¦³°Å¶Kï«ü¥O¤j³£¶°¦X³£¦bEdit¿ï³æ¤¤¡C¨Ì·ÓþÓ±±¨î¶µ¬°§@¥Î¤¤¨Ó¨M©wþ¨Ç«ü¥O¯àÅý¨Ï¥ÎªÌ¨Ï¥Î¡]¥H¤Îµ{¦¡¥i³B²z¥¦Ì¡^¡C³o¦³¨âÓ°ÝÃD©|«Ý¸Ñ¨M¡G°ò©ó¤Íµ½ªº¨Ï¥ÎªÌ¬É±¡AÀ³¸Ón¨ú®ø©Ò¦³µLªkÀ³¥Î¨ì²{¦æ±±¨î¶µ»P°Å¶K諸¥Ø«e¤º®eªº¿ï¶µ¡A¥B¥²¶·³]p¯à¦b©Ò¦³±¡ªp¤U¶i¦æ°Å¤U-½Æ»s-¶K¤Wªº¾÷¨î¡C
·í¨Ï¥Î¦h«ªº±±¨î¶µ¦bªí³æ¤W®É¡A¨Æ±¡´N«Ü§Ö¦aÅý¤H·P¨ì§x´b¡A¦]¬°¥²¶·¦Ò¼{¨ì¤@¨Ç¼ç¦b©Êªº°ÝÃD¡C³o¦³Ó²³æ¦ý§¹¾ãªº½d¨Òµ{¦¡¡]¨£¹Ï5-3¡^¡C¬°¤FÅý±z¯à®e©ö¦a¦bÀ³¥Îµ{¦¡¤¤«¥Î¡A©Ò¦³¹ï±±¨î¶µªº¤Þ¥Î¬Ò³z¹Lªí³æªºActiveControlÄÝ©Ê¡C°£¤F¨Ï¥ÎTypeOf©ÎTypeNameÃöÁä¦r´ú¸Õ±±¨î¶µ§ÎºA¥~¡A¦¹µ{¦¡¨Ï¥ÎOn Error Resume Next±Ôz¶¡±µ¦a´ú¸Õ¬YÄݩʬO§_¤ä´©¡]¨£¤U¦Cµ{¦¡ªº²ÊÅ鳡¥÷¡C¡^³o¤èªk¯à³B²z¥ô¦ó§ÎºAªº±±¨î¶µ¡A¥]¬A¨ó¤O¼t°ÓªºActiveX±±¨î¶µ¡A¦Ó·í¼W¥[·sªº±±¨î¶µ¨ì¤u¨ã½c®É¤£¥Îקïµ{¦¡¡C
¡@
¹Ï5-3 Clipbord.vbp¥Ü½d±M®×Åã¥Ü¦p¦ó¨Ï¥ÎTextBox¡BRTF TextBox©MPictureBox±±¨î¶µ«Ø¥ß³q¥Î½s¿è¿ï³æ |
' Items in Edit menu belong to a control array. These are their indices. Const MNU_EDITCUT = 2, MNU_EDITCOPY = 3 Const MNU_EDITPASTE = 4, MNU_EDITCLEAR = 6, MNU_EDITSELECTALL = 7 ' Enable/disable items in the Edit menu. Private Sub mnuEdit_Click() Dim supSelText As Boolean, supPicture As Boolean ' Check which properties are supported by the active control. On Error Resume Next ' These expressions return False only if the property isn't supported. supSelText = Len(ActiveControl.SelText) Or True supPicture = (ActiveControl.Picture Is Nothing) Or True If supSelText Then mnuEditItem(MNU_EDITCUT).Enabled = Len(ActiveControl.SelText) mnuEditItem(MNU_EDITPASTE).Enabled = Clipboard.GetFormat(vbCFText) mnuEditItem(MNU_EDITCLEAR).Enabled = Len(ActiveControl.SelText) mnuEditItem(MNU_EDITSELECTALL).Enabled = Len(ActiveControl.Text) ElseIf supPicture Then mnuEditItem(MNU_EDITCUT).Enabled = Not (ActiveControl.Picture _ Is Nothing) mnuEditItem(MNU_EDITPASTE).Enabled = Clipboard.GetFormat( _ vbCFBitmap) Or Clipboard.GetFormat(vbCFMetafile) mnuEditItem(MNU_EDITCLEAR).Enabled = _ Not (ActiveControl.Picture Is Nothing) Else ' Neither a text- nor a picture-based control mnuEditItem(MNU_EDITCUT).Enabled = False mnuEditItem(MNU_EDITPASTE).Enabled = False mnuEditItem(MNU_EDITCLEAR).Enabled = False mnuEditItem(MNU_EDITSELECTALL).Enabled = False End If ' The Copy menu command always has the same state as the Cut command. mnuEditItem(MNU_EDITCOPY).Enabled = mnuEditItem(MNU_EDITCUT).Enabled End Sub ' Actually perform copy-cut-paste commands. Private Sub mnuEditItem_Click(Index As Integer) Dim supSelText As Boolean, supSelRTF As Boolean, supPicture As Boolean ' Check which properties are supported by the active control. On Error Resume Next supSelText = Len(ActiveControl.SelText) >= 0 supSelRTF = Len(ActiveControl.SelRTF) >= 0 supPicture = (ActiveControl.Picture Is Nothing) Or True Err.Clear Select Case Index Case MNU_EDITCUT If supSelRTF Then Clipboard.Clear Clipboard.SetText ActiveControl.SelRTF, vbCFRTF ActiveControl.SelRTF = "" ElseIf supSelText Then Clipboard.Clear Clipboard.SetText ActiveControl.SelText ActiveControl.SelText = "" Else Clipboard.SetData ActiveControl.Picture Set ActiveControl.Picture = Nothing End If Case MNU_EDITCOPY ' Similar to Cut, but the current selection isn't deleted. If supSelRTF Then Clipboard.Clear Clipboard.SetText ActiveControl.SelRTF, vbCFRTF ElseIf supSelText Then Clipboard.Clear Clipboard.SetText ActiveControl.SelText Else Clipboard.SetData ActiveControl.Picture End If Case MNU_EDITPASTE If supSelRTF And Clipboard.GetFormat(vbCFRTF) Then ' Paste RTF text if possible. ActiveControl.SelRTF = Clipboard.GetText(vbCFText) ElseIf supSelText Then ' Else, paste regular text. ActiveControl.SelText = Clipboard.GetText(vbCFText) ElseIf Clipboard.GetFormat(vbCFBitmap) Then ' First, try with bitmap data. Set ActiveControl.Picture = _ Clipboard.GetData(vbCFBitmap) Else ' Else, try with metafile data. Set ActiveControl.Picture = _ Clipboard.GetData(vbCFMetafile) End If Case MNU_EDITCLEAR If supSelText Then ActiveControl.SelText = "" Else Set ActiveControl.Picture = Nothing End If Case MNU_EDITSELECTALL If supSelText Then ActiveControl.SelStart = 0 ActiveControl.SelLength = Len(ActiveControl.Text) End If End Select End Sub
Printerª«¥ó
¡@
³\¦hÀ³¥Îµ{¦¡»Ýn¥h¦C¦L¨äµ²ªG¦b¯È¤W¡CVisual Basic´£¨Ñ¤@ÓPrinterª«¥ó¡A¨ä¥]§t¤@¨ÇÄÝ©Ê©M¤èªk¥i§¹¬ü¦a±±¨î¦C¦L¤å¥óªº®ÄªG¡C
Visual Basic¨ç¦¡®w¤]¦³Ó©MPrinters¶°¦X¡A¯à¦¬¶°©Ò¦³¦w¸Ë¦b¨t²Î¤Wªº¦Lªí¾÷¸ê°T¡C¨CÓ¶µ¥Ø³£¬OÓ¦Lªí¾÷ª«¥ó¡A¥B©Ò¦³ªºÄݩʳ£¬O°ßŪªº¡C´«¥y¸Ü»¡¡A¶È¯àŪ¥X©Ò¦³¤w¦w¸Ë¦Lªí¾÷ªº¯S©Ê¦ý¤£¯àª½±µ¦aקï¥LÌ¡C¦pªG»Ýn§ó§ï¦Lªí¾÷ªº¯S©Ê¡A«hº¥ý¥²¶·±q¶°¦X¤§¤¤±N©Ò¿ï¾Üªº¦Lªí¾÷«ü©wµ¹Printerª«¥ó¡AµM«á¦A§ïÅÜ¥¦ªºÄÝ©Ê¡C
¨ú±o¤w¦w¸Ë¦Lªí¾÷ªº¸ê°T
¡@
Printerª«¥ó¾Ö¦³³\¦hªºÄÝ©ÊÅý¤H±oª¾¥i¥Î¦Lªí¾÷ªº¯S©Ê»P¨äÅX°Êµ{¦¡¡CÁ|¨Ò¨Ó»¡¡ADeviceNameÄÝ©Ê·|¦^¶Ç¥X²{¦b±±¨î¥x¤Wªº¦Lªí¾÷¦WºÙ¡A¥BDriverName·|¦^¶Ç¨ä©Ò¨Ï¥ÎªºÅX°Êµ{¦¡¦WºÙ¡C¥i±N³o¨Ç¸ê°T¶ñ¤JListBox©MComboBox±±¨î¶µ¡G
For i = 0 To Printers.Count _ 1 cboPrinters.AddItem Printers(i).DeviceName & " [" & _ Printers(i).DriverName & "]" Next
PortÄÝ©Ê·|¦^¶Ç¦Lªí¾÷ªº³s±µ°ð¡]¦pLPT1:¡^¡CColorModeÄݩʧPÂ_¦Lªí¾÷¯à§_¦C¦L±m¦â¤å¥ó¡C¡]¥i¥H¬O1-vbPRCMMonochrome©Î2-vbPRCMColor¡^¡C OrientationÄݩʪí¥Ü¦C¦L¶ªº¤è¦V¡C¡]¥i¥H¬O1-vbPRORPortrait¡B2-vbPRORLandscape¡^¡CPrinterQualityÄݩʦ^¶Ç¥Ø«eªº¸ÑªR«×¡C¡]¥i¥H¬O1-vbPRPQDraft¡B2-vbPRPQLow¡B3-vbPRPQMedium©Î4-vbPRPQHigh¡C¡^
¨ä¥LÄÝ©ÊÁÙ¦³PaperSize¡]¯È±i¤j¤p¡^¡BPaperBin¡]¯È±i¨Ó·½¡^¡BDuplex¡]¯È±iÃä¬É¡^¡BCopies¡]½Æ»s¥÷¼Æ¡^©MZoom¡]©ñ¤j¿²v¡^¡CÃö©ó³o¨ÇÄݩʪº¸ê°T¡A½Ð¨£Visual Basic»¡©ú¤å¥ó¡C¦bªþÄÝ¥úºÐ¤¤¡A¥i¥H§ä¨ì½d¨Òµ{¦¡¡]¦p¹Ï5-4©Ò¥Ü¡^¦CÁ|¥X¨t²Î¤¤ªº©Ò¦³¦Lªí¾÷¡BÂsÄý¨äÄÝ©Ê»P¨C¥x¦U¦L¤@±i´ú¸Õ¡C
¡@
¹Ï5-4 °õ¦æ®i¥Üµ{¦¡¨Ó¹î¬ÝPrinters¶°¦X©M¹B§@¤¤ªºPrinterª«¥ó |
²{¦æ¦Lªí¾÷ªº¹B§@
¡@
¤@Ó·s¼éªºÀ³¥Îµ{¦¡À³¸ÓÅý¨Ï¥ÎªÌ¾Ö¦³©M¦w¸Ë¦b¨t²Î¤¤ªº¦Lªí¾÷¤@¦P¹B§@ªº¯à¤O¡C¦bVisual Basic¤¤¡AÂǥѦLªí¾÷¶°¦X¤¤ªº¤¸¥ó±Ôz©Ò¿ï¨úªº¦Lªí¾÷´N¥i°µ¨ì¡CÁ|¨Ò¨Ó»¡¡A¦pªG§A¤w¸g¦bComboBox±±¨î¶µ¤¤¶ñº¡©Ò¦³¤w¦w¸Ëªº¦Lªí¾÷¡A«h¥i¥HÅý¨Ï¥ÎªÌÂÇ¥ÑÂI¿ïMake Current«ö¶s¨Ó¿ï¾Ü¨ä¤¤¤§¤@¡G
Private Sub cmdMakeCurrent_Click() Set Printer = Printers(cboPrinters.ListIndex) End Sub
¬Û¹ï©óPrinters¶°¦X¤ºªºPrinterª«¥óªºÄݩʬO°ßŪªº¡A§A«o¥iקïPrinterª«¥óªºÄÝ©Ê¡C²z½×¤W¡A¨ì¥Ø«e¬°¤î©Ò¤¶²Ðªº©Ò¦³Äݩʳ£¥i¥H³Q¼g¤J¡A°£¤FDeviceName¡BDriverName©MPort¡CµM¦Ó¡A¹ê»Ú¤W¯à¼g¤JªºÄݩʺݵø¦Lªí¾÷©MÅX°Êµ{¦¡¦Ó©w¡CÁ|¨Ò¨Ó»¡¡A¦pªG¥Ø«eªº¦Lªí¾÷¬O³æ¦âªº¡A«hµLªk¦bColorModeÄݩʤ¤¨Ï¥Î2-vbPRCMColorÈ¡CY³o¼Ë°µªº¸Ü¡A¦Lªí¾÷¥i¯à·|¤£²z·|©Î²£¥Í¿ù»~¡C¤@¯ë¨Ó»¡¦pªGÄݩʤ£¤ä´©¡A·|¦^¶Ç0¡C
¦³®ÉÔ¥i¯à»Ýn¥hPrinterª«¥ó¹ïÀ³¨ìPrinters¶°¦XªºþÓ¶µ¥Ø¡A¨Ò¦pn¼È®É¥Î¥t¤@¥x¦Lªí¾÷¦C¦L®É¡AµM«á¤SnÁÙì¦^ì¥ýªº¦Lªí¾÷¡C¸g¥Ñ¤ñ¸ûPrinterª«¥óDeviceNameÄÝ©Ê»PPrinters¶°¦Xªº¨CÓ¶µ¥Ø©Ò¦^¶ÇªºÈ´N¥i¥H°µ¨ì¡G
' Determine the index of the Printer object in the Printers collection. For i = 0 To Printers.Count _ 1 If Printer.DeviceName = Printers(i).DeviceName Then PrinterIndex = i: Exit For End If Next ' Prepare to output to the printer selected by the user. Set Printer = Printers(cboPrinters.ListIndex) ' ... ' Restore the original printer. Set Printer = Printers(PrinterIndex)
¥t¤@ÓÅý¨Ï¥ÎªÌ¥Î¥LÌ©Ò¿ï¾Üªº¦Lªí¾÷¦C¦Lªº¤èªk¬O³]©w¦Lªí¾÷ªºTrackDefaultÄݩʬ°True¡C³o¼Ë¤l°µ¡A¦Lªí¾÷ª«¥ó·|¦Û°Ê«ü¦V©ó±±¨î¥x¤¤©Ò¿ï¾Üªº¦Lªí¾÷¡C
¿é¥X¸ê®Æ¨ìPrinterª«¥ó
¡@
¶Ç°eµ²ªGµ¹Printerª«¥ó¤£Ãø¡A¦]¬°¦¹ª«¥ó¤ä´©ªí³æ©MPictureBoxª«¥ó¤¤©Ò¦³ªº¹Ï§Î¤èªk¡A¥]¬A¦³Print¡BPset¡BLine¡BCircle©MPaintPicture¡C¤]¥i¥Î¼Ð·ÇªºÄÝ©Ê¡A¦pFontª«¥ó¡B¿W¥ßªºFontxxxxÄÝ©Ê¡BCurrentX©MCurrentYÄÝ©Ê¡A¨Ó±±¨î¥~Æ[¡C
Printerª«¥ó¦³¤TÓ¿W¯Sªº¤èªk¡CEndDoc¤èªk³qª¾Printerª«¥ó©Ò¦³ªº¸ê®Æ¤w¶Ç°e¡A¥i¶}©l¦C¦L¡CKillDoc¤èªk«h¦b¶Ç°e¥ô¦ó¸ê®Æµ¹¦Lªí¾÷¸Ë¸m«e²×¤î¥Ø«eªº¦C¦L¤u§@¡C³Ì«áNewPage¤èªk¶Ç°e¥Ø«eªº¶±µ¹¦Lªí¾÷¡]©Î¦Lªí¤u§@¦C¡^¡A¨Ã«e¶i¨ì¤U¤@¶¡C¥¦¤]¥i¥H«³]¦C¦L½d³òªº¥ª¤W¨¤¦ì¸m©M¼W¥[¶¼Æ¡C¥Ø«eªº¶¼Æ¥i¥ÑPageÄݩʨú±o¡C³o¸Ì¦³Ó¦C¦L¨â¶¤å¥óªº¨Ò¤l¡G
Printer.Print "Page One" Printer.NewPage Printer.Print "Page Two" Printer.EndDoc
Printerª«¥ó¤]¤ä´©¼Ð·ÇªºScaleLeft¡BScaleTop¡BScaleWidth©MScaleHeightÄÝ©Ê¡A¨ä«×¶q³æ¦ì¥ÑScaleModeÄÝ©Ê©Ò¨M©w¡]³q±`¬Otwips¡^¡C¹w³]±¡ªp¬OScaleLeft©MScaleTopÄݩʦ^¶Ç0¡Aªí¥Ü¥i¦C¦L½d³òªº¥ª¤W¨¤°Ï°ì¡C ScaleWidth©MScaleHeightÄÝ©Ê·|¦^¶Ç¥i¦C¦L½d³òªº¥k¤U¨¤®y¼Ð¡C
°õ¦æ¨ä¥LªºÀ³¥Îµ{¦¡
¡@
Visual Basic¯à¥ÎShell«ü¥O°õ¦æ¨ä¥LªºWindowsÀ³¥Îµ{¦¡¡A¨ä»yªk¬°¡G
TaskId = Shell(PathName, [WindowStyle])
PathName¥i¥]§t¤@Ó©R¥O¦æ¡CWindowStyle¬°¥H¤U¨ä¤¤¤@Ó±`¼Æ¡G0-vbHide¡]ÁôÂõøµ¡¥B¥¢¥hµJÂI¡^¡B1-vbNormalFocus¡]µøµ¡¬°§@¥Î¤¤¨ÃÁÙì·Lì¥ýªº¤j¤p©M¦ì¸m¡^¡B2-vbMinimizedFocus¡]µøµ¡¥H¹Ï¥ÜÅã¥Ü¨Ã¬°§@¥Î¤¤¡Ð¦¹¬°¹w³]È¡^¡B3-vbMaximizedFocus¡]µøµ¡¬°³Ì¤j¤Æ¥B¬°§@¥Î¤¤¡^¡B4-vbNormalNoFocus¡]µøµ¡¤wÁÙì¦ý¥¢¥hµJÂI¡^©Î6-vbMinimizedNoFocus¡]µøµ¡³Ì¤p¤Æ¥BµJÂI°±¦bì§@¥Î¤¤µøµ¡¡^¡C¬Ý¬Ý¨Ò¤l¡A¦p¦ó°õ¦æ°O¨Æ¥»¨ÃŪ¨ú¤@ÓÀɮסG
' No need to provide a path if Notepad.Exe is on the system path. Shell "notepad c:\bootlog.txt", vbNormalFocus
Shell¨ç¦¡¥H«D¦P¨B¨Ó°õ¦æ¥~³¡µ{¦¡¡C³oªí¥Ü±±¨îÅv«Ü§Ö¦aªð¦^Visual BasicÀ³¥Îµ{¦¡¤¤¡A¦p¦¹¥i¥HÄ~Äò°õ¦æ¥¦ì¥»ªºµ{¦¡¡C¦b¤j³¡¤Àªº±¡ªp¤U¡A³oºØ¦æ¬°¬O¥¿±`ªº¡A¦]¬°³o¥¿¬OWindows¦h¤uªº¯S©Ê¡C¦ý¦³®ÉÔ¤´»Ýn¥hµ¥«Ý¥~³¡µ{¦¡°õ¦æµ²§ô¡]Á|Ó¨Ò¤l¡A¦pªG»Ýn°õ¦æªºµ²ªG¡^¡A©Î¥u¬On´ú¸Õµ{¦¡¬O§_¤´¦b°õ¦æ¡CVisual Basic¥»¨¨S¦³¨ç¼Æ¨ÓÀò±o³o¨Ç¸ê°T¡A¦ý¬O¥i¥Î¤@¨ÇWuindows API¨Ó¹F¦¨³o¶µ¤u§@¡C§Ú·Ç³Æ¤F¤@Ó¦h¥Î³~ªº¨ç¦¡¨Ó´ú¸Õ¦@¨Éªºµ{¦¡¬O§_¤´¦b°õ¦æ¡Aµ¥«Ýªº®É¶¡¥i¦Û¦æ½Õ¾ã¡]¬Ù²¤¨ä°Ñ¼Æ·|«ùÄòµ¥«Ý¡^¡AµM«áYµ{¦¡¤´¦b°õ¦æ«h¦^¶ÇTrue¡G
' API declarations Private Declare Function WaitForSingleObject Lib "kernel32" _ (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwAccess As _ Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long ' Wait for a number of milliseconds, and return the running status of a ' process. If argument is omitted, wait until the process terminates. Function WaitForProcess(taskId As Long, Optional msecs As Long = -1) _ As Boolean Dim procHandle As Long ' Get the process handle. procHandle = OpenProcess(&H100000, True, taskId) ' Check for its signaled status; return to caller. WaitForProcess = WaitForSingleObject(procHandle, msecs) <> -1 ' Close the handle. CloseHandle procHandle End Function
¶Ç°e¨ì¦¹½d¨Òªº°Ñ¼Æ¬°Shell¨ç¦¡¦^¶ÇªºÈ¡G
' Run Notepad, and wait until it is closed. WaitForProcess Shell("notepad c:\bootlog.txt", vbNormalFocus)
¦³´XÓ¤èªk¯à»P°õ¦æªºµ{¦¡¤¬°Ê¡C¦b ²Ä16³¹ ¤¤¡A·|´£¨ì¦p¦ó¥HCOM±±¨î¤@ÓÀ³¥Îµ{¦¡¡A¦ý¨Ã«D©Ò¦³ªºÀ³¥Îµ{¦¡¬Ò¯à¥Î¦¹¤èªk¨Ó±±¨î¡C¦Ó¥B§Y¨Ï¥i¥H¡A¦³®ÉԨ䵲ªG¨Ã¤£È±o³o¼Ë°µ¡C¦b¸û¤Ön¨Dªº±¡ªp¤U¡A¥i¥Î¸û¬°Â²³æ¡A«Øºc¦bAppActivate©MSendKeys«ü¥Oªº¤èªk¨ÓÀò±o¡CAppActivate«ü¥O·|Âಾ¿é¤JµJÂI¨ì²Å¦X²Ä¤@ӰѼƪºÀ³¥Îµ{¦¡¡G
AppActivate WindowTitle [,wait]
WindowTitle¥i¥H¬OÓ¦r¦ê©ÎShell¨ç¦¡ªº¦^¶ÇÈ¡FY¬°«eªÌ¡AVisual Basic·|¤ñ¸û¦¹È»P¨t²Î¤¤©Ò¦³±Ò°Ê¤¤µøµ¡ªº¼ÐÃD¡C¦pªG¨S¦³§¹¥þ²Å¦X¡AVisual Basic·|«½Æ´M§ä¼ÐÃD¶}ÀY²Å¦X¶Ç¤J¦r¦ê°Ñ¼Æªºµøµ¡¡CY¶Çµ¹Shell¨ç¦¡ªºtaskid¦^¶ÇÈ¡A«h¤£»Ýn²Ä¤G¦¸ªº¶Ç°e¡A¦]¬°taskid°ß¤@«ü¨ì¤@°õ¦æªºµ{§Ç¡C¦pªGVisual BasicµLªk§ä¨ì²Å¦Xªºµøµ¡¡A«h·|²£¥Í°õ¦æ®É´Á¿ù»~¡CWait¬O«D¥²nªº°Ñ¼Æ¡A¥Î¨Ó«ü¥ÜVisual BasicÀ³¸Óµ¥¨ì¦b¶Ç°e¨ì¨ä¥Lµ{¦¡«e¡A¥Ø«eªºÀ³¥Îµ{¦¡¤wÀò±oµJÂI¡]Wait=True¡^©Î¬O§_«ü¥O¥²¶·ª½±µ°õ¦æ¡]Wait=False¡A¹w³]¦æ¬°¡^¡C
SendKeys±Ôz·|¶Ç°e¤@өΦhÓÁäµ¹¥¿µ¥«Ý¿é¤JªºÀ³¥Îµ{¦¡¡C¦¹±Ôz¦³µÛµy·L½ÆÂøªº»yªk¡AÅý¤H«ü¬£¦pCtrl¡BAlt©MShiftªº±±¨îÁä¡B²¾°Ê´å¼Ð¡B¯S®í¥\¯àÁäµ¥µ¥¡C¡]¸Ô¨£Visual Basicªº»¡©ú¤å¥ó¡^¡C³o¬qµ{¦¡·|°õ¦æ°O¨Æ¥»¡A¥B¦b¥¦ªºµøµ¡¤¤¶K¤J°Å¶K襤ªº¤º®e¡G
TaskId = Shell("Notepad", vbMaximizedFocus) AppActivate TaskId SendKeys "^V" ' ctrl-V
²{¦b§A¥i¥H°õ¦æ¥~³¡µ{¦¡¡A»P¥L¤¬°Ê¡Aª¾¹D¥¦¦ó®É§¹¦¨°õ¦æ®É¡C³o¸Ì¦³Ó½d¨Òµ{¦¡¯à¹F¦¨³o¥\¯à¡A¨ÃÅý§A¤F¸Ñ¤@¨Ç¤£¦Pªº³]©w¡]¨£¹Ï5-5¡^¡C¦bªþÄÝ¥úºÐ¤¤¦³§¹¾ãªºµ{¦¡½X¡C
¡@
¹Ï5-5 »¡©ú¦p¦ó¨Ï¥ÎShell¡BAppActivate©MSendKeys±Ôzªº½d¨Òµ{¦¡ |
Åã¥ÜHelp
¡@
¤@Ó¦¨¥\ªºWindowsÀ³¥Îµ{¦¡À³¸Ó·|´£¨Ñ¨Ï¥Î¤â¥Uµ¹ªì¾ÇªÌ¡A³Ì¨å«¬ªº¬O¥HHelpÀɮתº§Î¦¡ªí²{¡CVisual Basic¤ä´©¨âºØ¤£¦Pªº¤èªk¨ÓÅã¥Ü³o¨Ç¨Ï¥ÎªÌ¸ê°T¡A¨âªÌ¬Ò¨Ï¥ÎHelpÀɮסC
¼¶¼gHelpÀÉ®×
¡@
³o¨âºØ±¡ªp¬Ò¥²¶·¥ý«Ø¥ßHelpÀɮסC¬°¤F¦p¦¹¡A»Ýn¤@Ó¯à°÷«Ø¥ßRTFÀɮ׮榡ªº¤å¦r³B²z¸Ë¸m¡]¦pMicrosoft Word¡^¥H¤Î¤@ÓHelp½sĶ¾¹¡C¦bVisual Basic 6¥úºÐ¥i§ä¨ìMicrosoft Help Workshop¡A¦p¹Ï5-6©Ò¥Ü¡A¨ä¥iÅý±z²Õ¦X©Ò¦³·Ç³Æ¦nªº¤å¥ó©MÂI°}¹Ï¡A¨Ã±N¤§½sĶ¦¨HLPÀÉ¡C
¼¶¼gHelpÀɬO½ÆÂøªº¨Æ±¡¡A¶W¥X¦¹®Ñ½d³ò¡CMicrosoft Help Workshop¤¤ªº»¡©ú¤å¥ó¦³Ãö©ó¦¹ªº¸ê°T¡CµM¦Ó¨Ìµ§ªÌªº¬Ýªk¡A³Ì¦³®Äªº¤èªk¬O¨Ï¥Î¨ó¤O¼t°Óªº¦@¨É³nÅé©Î°Ó·~µ{¦¡¡A¦pBlue Sky SoftwareªºRoboHelp©ÎWexTechªºDoc-to-Help¡A¨äÅý«Ø¥ßHelpÀÉ®×Åܱo¸û²³æ¨Ç¡C
¡@
¹Ï5-6 Help Workshop¤u¨ã©ñ¦bVisual Basic¥úºÐ¤ù¤¤¦ý¶·¥ý¦w¸Ë |
¤@¥¹¤w«Ø¥ßHLPÀɮסA«h¥i¥H¦bVisual BasicÀ³¥Îµ{¦¡¤¤¤Þ¥Î¥¦¡C°£¤F¦b³]p®É´ÁÂǥѦb±M®×Äݩʵøµ¡ªº¤@¯ë¶ÅÒ¤¤¿é¤JÀɮצWºÙ¥~¡AÁÙ¥i¥H¦b°õ¦æ®É´ÁÂǥѫü©wApp.HelpFileÄݩʨӹF¦¨¡C«áªÌ¥Î©ó¤£½T©wHelpÀɮצw¸Ë©ó¦ó³B®É¨Ï¥Î¡CÁ|Ó¨Ò¤l¡A¥i¥H³]©w¦¹¸ô®|¦bÀ³¥Îµ{¦¡ªº¥Dn¸ê®Æ§¨¤U¡G
' If this file reference is incorrect, Visual Basic raises an error ' when you later try to access this file. App.HelpFile = App.Path & "\Help\MyApplication.Hlp"
¼Ð·Çµøµ¡ªºHelp
¡@
´£¨Ñ¬ÛÃö¤å³¹¨D§Uªº²Ä¤@Ó¤èªk¬OF1Áä¡C³oºØ¨D§U¤èªk¨Ï¥ÎHelpContextIDÄÝ©Ê¡A¨ä¤ä´©Visual Basic©Ò¦³¥iµøª«¥ó¡A¥]¬Aªí³æ¡B¤º«Ø±±¨î¶µ©MÃB¥~ªºActiveX±±¨î¶µ¡C¤]¥i¦b³]p®É´Á©ó±M®×Äݩʵøµ¡¤¤¿é¤JÄÝ©óÀ³¥Îµ{¦¡¼h¯Åªºhelp context ID¡C¡]Appª«¥ó¦b°õ¦æ®É´Á¨Ã¥¼¥]§t¦Pµ¥ªºÄÝ©Ê¡C¡^
·í¨Ï¥ÎªÌ«ö¤UF1®É¡AVisual Basic·|®Ö¹ï§@¥Î¤¤±±¨î¶µªºHelpContextIDÄݩʬO§_¬°«D¹sÈ¡GY¬Oªº¸Ü¡A·|Åã¥ÜÃö©ó¸ÓIDªº¨D§U¶¡C§_«h¡AVisual Basic·|Àˬd¥Dªí³æ¬O§_¾Ö¦³¤@«D¹sȪºHelpContextIDÄÝ©Ê¡AµM«áÅã¥Ü¬ÛÃöªº¨D§U¶¡C¦pªG±±¨î¶µ©Mªí³æªºHelpContextIDÄݩʬҬ°0¡AVisual Basic´N·|Åã¥Ü¹ïÀ³¨ì±M®×help context ID¬ÛÃöªº¨D§U¶¡C
"What's This"Help
¡@
Visual Basic¤]¤ä´©¥t¥~¤@ÓÅã¥ÜHelp¤èªk¡AºÙ¤§¬°"What's This"Help¡CÂǥѦbªí³æ¥k¤W¨¤Åã¥ÜWhat's This«ö¶s¨Ó¤ä´©¦¹»¡©ú¼Ò¦¡¡A¦p¹Ï5-7©Ò¥Ü¡C·í¨Ï¥ÎªÌÂI¿ï¸Ó«ö¶s®É¡A·Æ¹««ü¼Ð·|§ïÅܬ°¤@Ó½bÀY©M°Ý¸¹ªº¹Ï§Î¡AµM«á¨Ï¥ÎªÌ¥i¥HÂI¿ïªí³æ¤Wªº¥ô·N±±¨î¶µ¨Ó§Ö³tÀò±o¸Ó±±¨î¶µªº»¡©ú¡C
¡@
¹Ï5-7 ªí³æ³Ì¥k¤W¨¤ªºWhat's This«ö¶s³QÂI¿ï |
n¦bµ{¦¡¤¤¨Ï¥Î¦¹¯SÂI¡A¥²¶·³]©wªí³æªºWhatsThisButtonÄݩʬ°True¡A¨Ï±oWhat's This«ö¶sÅã¥Ü¦bªí³æ¼ÐÃD¦C¡C¦¹Äݩʦb°õ¦æ®É´Á¬O°ßŪªº¡A©Ò¥H¶È¯à¦b³]p®É´ÁªºÄݩʵøµ¡¤¤³]©w¡C¦¹¥~¡A¬°¤FÅýWhat's This«ö¶sÅã²{¡AÁÙ¥²¶·³]©wBorderStyleÄݩʬ°1-Fixed Single©Î3-Fixed Dialog¡A©Î¥²¶·³]©wMaxButton©MMinButtonÄݩʬ°False¡C
¦pªG¨S¦³²Å¦X¦¹n¨D¡A«hµLªkÅã¥ÜWhat's This«ö¶s¡C¦ý¥iÂǥѫö¶s©Î¿ï³æ¿ï¶µ¨Ó¶i¤J¦¹¼Ò¦¡¡AÂǥѰõ¦æªí³æªºWhatsThisMode¤èªk¡G
Private Sub cmdWhatsThis_Click() ' Enter What's This mode and change mouse cursor shape. WhatsThisMode End Sub
ªí³æ¤Wªº¨CÓ±±¨î¶µ¡]¤£¬Oªí³æ¥»¨¡^¾Ö¦³WhastThisHelpIDÄÝ©Ê¡C§â¦¹Äݩʳ]©w¬°nÅã¥Ü»¡©ú¶ªºhelp context ID¡A·í¦bWhat's This¼Ò¦¡¤U¥B¨Ï¥ÎªÌÂI¿ï¦¹±±¨î¶µ®É¡C
³Ì«áªí³æªºWhatsThisHelpÄÝ©Ê¥²¶·³]¬°True¨Ó±Ò°ÊWhat's This Help¡C¦pªG¦¹Äݩʳ]¬°False¡A«hVisual Basic·|¦^´_¨ì¼Ð·ÇªºF1Áä»PHelpContextIDÄݩʧ@¬°°ò·Çªº»¡©ú¾÷¨î¡CWhatThisHelpÄݩʶȯà¦b³]p®É´Á³]©w¡CÃö©ó³o¤è±¡A¦³¤TÓ¤èªk¨ÓÅã¥ÜWhat's This? »¡©ú¥DÃD¡G
¤£½×¥ÎþºØ¤èªk¡A§O§Ñ¤F¥²¶··Ç³Æ¦nÀ³¥Îµ{¦¡¤º¨CÓªí³æ¤¤¨CÓ±±¨î¶µªº»¡©ú¤å¥ó¡C¦h¼Æ±±¨î¶µ¦@¥Î¬Û¦Pªº»¡©ú¤å¥ó¬O¥i¦æªº¡A¦ý³o¼Ë·|±aµ¹¨Ï¥ÎªÌ¬Û·íªº§x´b¡C¦]¦¹¤@¯ë¹ï©ó¨CÓ±±¨î¶µ¬Ò·|¦³¤£¦Pªº»¡©ú¶¡C
¦b«e¤³¹¡A§Ú¤w®i¥ÜÃö©óVisual BasicÀô¹Ò©MVBA»y¨¥ªº³Ì¦h¨Æ¶µ¡C²{¦b¡A§A¤w¸g¦³¨¬°÷ªº¸ê°T¼g¥X¤£¿ùªºµ{¦¡¤F¡CµM¦Ó¡A³o¥»®ÑµÛ«©óª«¥ó¾É¦Vªºµ{¦¡¡A¦b¤U¨âÓ³¹¸`¤¤¡A§Ú§Æ±æÅý±z¬Û«H¦³¦h»ÝnOOP¨Ó«Ø¥ß½ÆÂø¹B§@ªºÀ³¥Îµ{¦¡¡C
¡@