📄 velp11.htm
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp11.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel23.htm"><LINK REL="Previous" HREF="vel22.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp11.htm</H2><P ALIGN=LEFT><A HREF="vel22.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel23.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E171" >Stop & Type</A><UL><LI><A HREF="#E69E160" >The Program's Description</A><LI><A HREF="#E69E161" >Descriptions</A><LI><A HREF="#E69E162" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E33"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 11</B></FONT></CENTER></H1><BR><A NAME="E67E36"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Spruce Up your Programs</B></FONT></CENTER></H2><BR><BR><A NAME="E68E171"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop & Type</B></FONT></CENTER></H3><BR><P>This lesson taught you how to use the printer for text reports and use the screen for fancy graphics. Although business and scientific data processing requires printed reports, this unit's real glamour appeared in <A HREF="vel22.htm">Unit 22</A> when you learned all about the line and shape controls.<BR><P>The <I>visual</I> in Visual Basic becomes most apparent when you add graphics that capture the user's eye. Perhaps the control that provides more different formats than any other is the shape control, which enables you to draw squares, circles, and ovals of all shapes, sizes, and colors. This unit takes the shape control to the max by demonstrating a fancy form that blows up in your face with more than 150 sets of colored circles, squares, and ovals.<BR><P>In this lesson, you saw the following:<BR><UL><LI>When to access the Printer object<BR><BR><LI>How to use the Print method to produce text on the printed page<BR><BR><LI>What shapes are available from the shape control<BR><BR><LI>How to draw lines on the form using the line control<BR><BR><LI>When to use the picture box and image controls to place file graphics on a form<BR><BR></UL><BR><A NAME="E69E160"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P11.1 shows the PROJEC11.MAK opening Form window. Wow! When you load and run the program, the program generates all kinds of circles on the form. The entire collection of circles, as well as the subsequent graphic collections that appear after the circles, all are different owing to the program's use of the Rnd() function introduced in <A HREF="velp10.htm">project 10</A>.<BR><P><B> <A HREF="P11vel01.gif">Figure P11.1. Project 11 runs circles around other applications.</A></B><BR><P>As you click the Next Shape command button, the program replaces the circles with a similar mosaic of squares. Pressing Next Shape again produces a series of ovals. You can continue cycling through the circles, squares, and ovals as often as you want until you press the Exit command button to terminate the program.<BR><P>The program contains a control array with 160 shape controls named shpShape. Figure P11.2 shows the form at design time. The location of the shape controls at design time is irrelevant to the program because the code relies heavily on the Rnd() function to display the shapes in random screen locations, with random colors, sizes, and fill patterns.<BR><P><B> <A HREF="P11vel02.gif">Figure P11.2. <A HREF="velp11.htm">project 11</A>'s design mode shows an </B><B>interesting placement of a control array containing shapes.</A></B><BR><P>The program depends so much on the Rnd() function that a special function subroutine is included that converts Rnd() into a random number, from 1 to whatever argument is passed to the function. The function procedure's name is RndNum(); you'll see the function in the code description that appears in Listing P11.1.<BR><P><FONT COLOR="#000080"><B>Listing P11.1. The code that generates all the graphics on the form.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Option Explicit2:3: Sub Form_Activate ()4: ' Get things started by displaying circles5: Call Circles6: End Sub7:8: Sub Circles ()9: ' Draws several circles of different10: ' colors and sizes on the form11: Dim Count As Integer12: lblShape.Caption = "Circles"13: For Count = 0 To 159 ' 160 total shapes14: shpShape(Count).Shape = SHAPE_CIRCLE15: shpShape(Count).Visible = False16: shpShape(Count).Top = RndNum(3000)17: shpShape(Count).Width = RndNum(5000)18: shpShape(Count).Height = RndNum(3000)19: shpShape(Count).Left = RndNum(3000)20: shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero21: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero22: shpShape(Count).Visible = True23: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust for zero24: Next Count25: End Sub26:27: Sub Ovals ()28: ' Draws several ovals of different29: ' colors and sizes on the form30: Dim Count As Integer31: lblShape.Caption = "Ovals"32: For Count = 0 To 159 ' 160 total shapes33: shpShape(Count).Shape = SHAPE_OVAL34: shpShape(Count).Visible = False35: shpShape(Count).Top = RndNum(3000)36: shpShape(Count).Width = RndNum(4000)37: shpShape(Count).Height = RndNum(3000)38: shpShape(Count).Left = RndNum(3000)39: shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero40: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero41: shpShape(Count).Visible = True42: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust for zero43: Next Count44: End Sub45:46: Sub Squares ()47: ' Draws several squares of different48: ' colors and sizes on the form49: Dim Count As Integer50: lblShape.Caption = "Squares"51: For Count = 0 To 159 ' 160 total shapes52: shpShape(Count).Shape = SHAPE_SQUARE53: shpShape(Count).Visible = False54: shpShape(Count).Top = RndNum(3000)55: shpShape(Count).Width = RndNum(5000)56: shpShape(Count).Height = RndNum(3000)57: shpShape(Count).Left = RndNum(3000)58: shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero59: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero60: shpShape(Count).Visible = True61: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust for zero62: Next Count63: End Sub64:65: Sub cmdShape_Click ()66: ' Check the caption of the description67: ' label to see what shapes to draw next68: Select Case Left$(lblShape.Caption, 1)69: Case "C":70: Call Squares71: Case "S"72: Call Ovals73: Case "O":74: Call Circles75: End Select76: End Sub77:78: Sub cmdExit_Click ()79: End80: End Sub81:82: Function RndNum (n)83: ' Returns a random number from 1 to n84: RndNum = Int(n * Rnd + 1)85: End Function</FONT></PRE><BR><A NAME="E69E161"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: Requires that all variables be defined before their use.<BR><P>2: A blank line that helps separate parts of the program.<BR><P>3: The code for the event procedure that executes as soon as the user sees the form for the first time.<BR><P>4: A remark that explains the procedure.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>4: Always remember to generate an initial form.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>5: Execute the subroutine procedure that draws the circles.<BR><P>6: Terminate the procedure.<BR><P>7: A blank line separates procedures.<BR><P>8: The subroutine procedure that draws circles.<BR><P>9: A remark that explains the procedure.<BR><P>10: The remark continues.<BR><P>11: Define a variable that will control the loop.<BR><P>12: Set the caption on the description label.<BR><P>13: Step through all 160 elements of the control array.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>13: There are 160 elements in the shape control array to display.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>14: Set the shape of each control to a circle.<BR><P>15: Hide the control, temporarily, until the properties are set.<BR><P>16: Set an appropriate placement from the top of the form.<BR><P>17: Set an appropriate width.<BR><P>18: Set an appropriate height.<BR><P>19: Set an appropriate placement from the left edge of the form.<BR><P>20: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><P>21: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><P>22: Display the control now that the size and placement properties are set.<BR><P>23: Fill the shape with a fill number from 0 to 8.<BR><P>24: Prepare for the next shape in the control array.<BR><P>25: Terminate the procedure.<BR><P>26: A blank line separates the procedures.<BR><P>27: The subroutine procedure that draws ovals.<BR><P>28: A remark that explains the procedure.<BR><P>29: The remark continues.<BR><P>30: Define a variable that will control the loop.<BR><P>31: Set the caption on the description label.<BR><P>32: Step through all 160 elements of the control array.<BR><P>33: Set the shape of each control to a oval.<BR><P>34: Hide the control, temporarily, until the properties are set.<BR><P>35: Set an appropriate placement from the top of the form.<BR><P>36: Set an appropriate width.<BR><P>37: Set an appropriate height.<BR><P>38: Set an appropriate placement from the left edge of the form.<BR><P>39: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>39: The QBColor() function accepts a value from 0 to 15 that represents a different color.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>40: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><P>41: Display the control now that the size and placement properties are set.<BR><P>42: Fill the shape with a fill number from 0 to 8.<BR><P>43: Prepare for the next shape in the control array.<BR><P>44: Terminate the procedure.<BR><P>45: A blank line separates the procedures.<BR><P>46: The subroutine procedure that draws squares.<BR><P>47: A remark that explains the procedure.<BR><P>48: The remark continues.<BR><P>49: Define a variable that will control the loop.<BR><P>50: Set the caption on the description label.<BR><P>51: Step through all 160 elements of the control array.<BR><P>52: Set the shape of each control to a square.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>52: The named constants in CONSTANT.TXT eliminate the need to remember numeric property equivalents.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>53: Hide the control, temporarily, until the properties are set.<BR><P>54: Set an appropriate placement from the top of the form.<BR><P>55: Set an appropriate width.<BR><P>56: Set an appropriate height.<BR><P>57: Set an appropriate placement from the left edge of the form.<BR><P>58: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><P>59: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.<BR><P>60: Display the control now that the size and placement properties are set.<BR><P>61: Fill the shape with a fill number from 0 to 8.<BR><P>62: Prepare for the next shape in the control array.<BR><P>63: Terminate the procedure.<BR><P>64: A blank line separates the procedures.<BR><P>65: A procedure that executes to display the next set of shapes when the user clicks the cmdShape command button.<BR><P>66: A remark that explains the procedure.<BR><P>67: The remark continues.<BR><P>68: Determine the next shape by looking at the label that describes the previously displayed shape.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>68: The code looks at the first character of the label to decide what shape to display next.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>69: If the label contains Circles...<BR><P>70: Change the display to random squares.<BR><P>71: If the label contains Squares...<BR><P>72: Change the display to random ovals.<BR><P>73: If the label contains Ovals...<BR><P>74: Change the display to random circles.<BR><P>75: Terminate the Select statement.<BR><P>76: Terminate the procedure.<BR><P>77: A blank line separates the procedures.<BR><P>78: A procedure that ends the program when the user clicks the cmdExit command button.<BR><P>79: Terminate the program's execution.<BR><P>80: Terminate the procedure.<BR><P>81: A blank line separates the procedures.<BR><P>82: A function procedure that accepts an integer argument and that returns a random number from 1 to the argument.<BR><P>83: A remark explains the function.<BR><P>84: Set the function's return value to the generated random number.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>84: A function procedure keeps you from having to repeat the same code throughout the program.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>85: Terminate the procedure.<BR><BR><A NAME="E69E162"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Close the Application</B></FONT></CENTER></H4><BR><P>You now can exit the application and exit Visual Basic. The next lesson teaches you how to use the remaining controls and Visual Basic's online debugging tool to help locate and correct errors within code.<P ALIGN=LEFT><A HREF="vel22.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel23.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -