⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vel23.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<FONT COLOR="#000080">1: Sub optAfter_Click ()2: ' After 5:00 pm so fix mouse over command3: cmdReport.MousePointer = ARROW4: End Sub5:6: Sub optBefore_Click ()7: ' Before 5:00 pm so negate mouse over command8: cmdReport.MousePointer = NO_DROP9: lblReport.Visible = False10: End Sub11:12: Sub cmdExit_Click ()13: End14: End Sub15:16: Sub cmdReport_Click ()17: ' Beep if it's before 5:0018: If optBefore.Value = True Then19: Beep20: lblReport.Visible = False21: Else22: lblReport.Visible = True23: End If24: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 23.4 shows what happens to the shape of the mouse cursor when the user moves the mouse cursor over the command button before 5:00 p.m.<BR><P><B> <A HREF="23vel04.gif">Figure 23.4. The mouse cursor tells the user that the command button won't </B><B>respond.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>When the program begins, the first option button named optBefore is set to True. Therefore, the application assumes that the time is before 5:00 p.m. and the command button's MousePointer value is set to NO_DROP as, shown in Figure 23.4.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The program could use the Time$() function to check the actual time to see whether the command button can be active or not. However, the option buttons offer you an easier method for practicing with the two mouse cursors; you don't have to wait until 5:00 p.m. to see the difference.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>As soon as the user clicks the After 5:00 p.m. option button named optAfter, the optAfter_Click() event procedure changes the command button's MousePointer property to ARROW in line 3.<BR><P>When the user clicks the Report command button, the command button's cmdReport_Click() event procedure executes (line 16). If the Before 5:00 pm option button is set, line 19 beeps. If the After 5:00 pm option button is set, a small label appears beneath the command button to inform you that the report can now be printed. (The label, named lblReport, is set to False when the program first begins.)<BR><BR><A NAME="E68E177"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Capturing Mouse Clicks and Movements</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The mouse supports three events that return movement and clicking information to the user through the form of event procedures. Unlike most event procedures, the mouse event procedures use arguments that return information such as the location of the mouse and the button that was clicked.<BR><P>Table 23.6 lists the events most often supported by applications that deal with the mouse. These events return events for which you can write event procedures to respond to the user's mouse commands.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 23.6. The events most often associated with the mouse.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Event</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>DblClick</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Generated when the user double-clicks a mouse button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MouseDown</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Generated when the user presses a mouse button</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MouseMove</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Generated when the user moves the mouse</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>MouseUp</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Generated when the user lets up on a mouse button</FONT></TABLE><P>The MouseDown, MouseMove, and MouseUp event procedures open with argument lists when you select them inside the Code window. Suppose that you wanted to respond to user clicks of the mouse when the user clicked the mouse over a form named frmApp. Here is the three event procedures' wrapper code that Visual Basic would open for you when you selected MouseDown, MouseMove, or MouseUp from the Code window's dropdown Proc listbox:<BR><PRE><FONT COLOR="#000080">Sub frmApp_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)End SubSub frmApp_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)End SubSub frmApp_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)End Sub</FONT></PRE><P>The argument lists for these procedures are so long that you may have to horizontally scroll the Code window to see the entire argument list.<BR><P>Table 23.7 lists the descriptions for these four argument values. The values returned in the arguments describe something about the user's mouse press or mouse release. Your code can check the argument values to determine what needs to be known. For example, if you want to know the location of the mouse when the user clicked the mouse on the form, the X and Y arguments describe the number of twips from the left and top edge of the form that the user clicked.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 23.7. The </B><B>MouseDown()</B><B> and </B><B>MouseUp()</B><B> arguments.</B></FONT></CENTER><BR><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Argument</I></FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Button</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds a number that represents the mouse button pressed. The value is 1 for the left button press, 2 for the right button press, and 4 for both or for the center button on a three-button mouse.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Shift</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Describes the shifting key (if any) pressed at the same time that the user clicked or moved the mouse button. Shift holds 1 if the user pressed Shift during the mouse click, 2 for the Ctrl key, and 4 for the Alt key. If Shift contains a number other than 1, 2, or 4, that number is the sum of two or more key presses just described. For example, if the user presses the Alt keys at the same time that the user pressed the mouse button, the value of Shift will be 5.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>X</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the horizontal twip form measurement where the user clicked or moved the mouse button.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Y</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the vertical twip form measurement where the user clicked or moved the mouse button.</FONT></TABLE><P>If the user double-clicks the mouse, Visual Basic generates both a MouseUp <I>and</I> a DblClick event. The MouseUp event procedure returns location and button information about the double-click (see Table 23.6), and the DblClick event should contain the code that you want executed upon the double-clicking of the mouse.<BR><P>The MouseUp event occurs every time that the user lets up on a mouse button, whether the user lets up from a single click or from a double-click. Therefore, if you want to respond to a single click's MouseUp event but not to a double-click's MouseUp event, you'll have to set a module variable inside the DblClick event procedure that you can test to see which kind of release, a click or double-click, generated the MouseUp event.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Depending on your computer, Visual Basic might generate a mouse movement event procedure every time that the user moves the mouse 10 to 15 twips across the form. Visual Basic doesn't generate a mouse movement event for <I>every</I> twip movement; most computers couldn't keep up with the events that would execute that fast.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 23.3 contains a sample MouseUp() event procedure that demonstrates how the code can use the arguments to the procedure to learn more about the mouse movement. The code prints mouse information. You could write a similar set of mouse event procedures for the other mouse events and get a printed log of the mouse actions as you moved, clicked, and double-clicked the mouse.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The mouse movement and clicking event procedures need additional information when a mouse event occurs. Through the argument list to the mouse event procedures, you can determine which button the user pressed, which key the user was pressing at the time of the click or mouse movement, and exactly where on the form the mouse was when the user generated the event.<BR><P><FONT COLOR="#000080"><B>Listing 23.3. Code that checks the mouse after a button release.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub frmApp_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)2: ' Display text on the printer that describes the3: ' mouse button press. The semicolon at the end of4: ' the Printer.Print statements forces Visual5: ' to keep the printer cursor on the same Print6: ' line.7:8: ' Tell the user about the button pressed9: Printer.Print &quot;Up: The button you released was the &quot;;10: Select Case Button11: Case 1: Printer.Print &quot;Left &quot;;12: Case 2: Printer.Print &quot;Right &quot;;13: Case 4: Printer.Print &quot;Middle &quot;;14: End Select15: Printer.Print &quot;button&quot;16:17: Printer.Print &quot;The mouse was at X position: &quot;;18: Printer.Print X;19: Printer.Print &quot;and Y position: &quot;;20: Printer.Print Y21: ' Print a blank line to separate for subsequent output22: Printer.Print23:24: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 collects the mouse button's release arguments. The argument tell the event procedure which button the user just released, which key, if any, was pressed at the time, and the exact twip coordinates of the mouse when the user released the mouse.<BR><P>The rest of the event procedure proceeds to print the mouse information gathered from the arguments. Keep in mind that this event procedure will execute every time the user releases either mouse button, so the printer's output will continue as the user continues clicking the mouse.<BR><BR><A NAME="E68E178"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E163"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>How many different scroll bar controls are there?<BR><BR><LI>How do the scroll bars eliminate the typing of specific entries?<BR><BR><LI>What do the LargeChange and SmallChange properties indicate?<BR><BR><LI>How does a user trigger a LargeChange change of a scroll bar's value?<BR><BR><LI>What do the Min and Max properties indicate?<BR><BR><LI>What are the preferred naming prefixes for scroll bar controls?<BR><BR><LI>True or false: You can initialize a scroll bar using a variable defined as an Integer data type.<BR><BR><LI>What must you do to add the grid control to the Toolbox window?<BR><BR><LI>What is the name of the custom control file that holds the grid control?<BR><BR><LI>True or false: The user can read, change, and enter new values into the grid.<BR><BR><LI>What is a <I>cell</I>?<BR><BR><LI>True or false: When you adjust the grid's physical size on the form, the grid's size must be large enough to display all of the grid's rows and columns.<BR><BR><LI>When can scroll bars appear on a grid?<BR><BR><LI>What two property values, available only at runtime, specify an individual cell that you want to change or initialize with code?<BR><BR><LI>What are the four property values that define selected cells?<BR><BR><LI>How many different shapes can the mouse appear as over a control?<BR><BR><LI>Which property describes the mouse cursor's appearance?<BR><BR><LI>Technically, what is the text cursor called?<BR><BR><LI>Technically, what is the mouse cursor called?<BR><BR><LI>When would the 0-Default and 1-Arrow MousePointer property values differ?<BR><BR><LI>Which mouse cursor might be useful for reminding the user to wait a few seconds until a calculation completes?<BR><BR><LI>How do the mouse movement events generate extra information that describe the buttons and location of the mouse?<BR><BR><LI>True or false: When the user double-clicks a mouse button, two events actually take place.<BR><BR></OL><BR><A NAME="E69E164"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><OL><LI>Kelly wants to use a grid control to hold a 5-by-5 table of weight readings that he needs for a chemistry experiment. After Kelly initializes the table, through code, and then presses a command button, he wants the code to fix the rows and columns for the entire table so that the user can't select additional cells. Sadly, Visual Basic imposes a limit that doesn't allow Kelly the freedom to fix all rows and columns. What is the maximum number of rows and columns that can be fixed in a table?<BR><BR></OL><BR><A NAME="E69E165"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Suppose that you were writing a program that needed the user to enter a temperature value from a range of 32 to 212 degrees. You want the user to enter the temperature reading using the relative position on a scroll bar. When the user clicks one of the scroll bar arrows, the scroll bar should adjust by 3 degrees. When the user clicks in the shaft on either side of the scroll bar, the scroll bar should adjust by 8 degrees. What would be appropriate Min, Max, SmallChange, and LargeChange properties?<BR><BR></OL><BR><A NAME="E69E166"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write a program that contains one command button labeled Change Mouse that displays a different mouse cursor every time the user clicks the button. Use a module variable to keep track of the current mouse pointer's value.<BR><P ALIGN=LEFT><A HREF="velp11.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="vel24.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 + -