1 / 10

VB6 程序设计

VB6 程序设计. 第三部分 鼠标与键盘事件. ★ 产学研中心. 教学目标:   1. 掌握:鼠标键盘事件的基本用法 2. 理解:组合框、滚动条、记时器的基本用法 3. 了解:键盘事件综合示例 学习要点 :    控件概念及其属性、方法、事件的应用    在窗体上用于输入、输出信息的图形或文字符号称为控件。为了方便用户开发应用程序, vb 中提供了很多的控件,其中很多控件是 windows 本身的资源,如命令按钮、标签、文本框等,也有一些是为了增强用户界面,或是使用户界面美观实用而编制的 activex 控件。. 一、 鼠标事件

usoa
Télécharger la présentation

VB6 程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB6程序设计 第三部分 鼠标与键盘事件 ★产学研中心

  2. 教学目标:  1.掌握:鼠标键盘事件的基本用法教学目标:  1.掌握:鼠标键盘事件的基本用法 2.理解:组合框、滚动条、记时器的基本用法 3.了解:键盘事件综合示例 学习要点:    控件概念及其属性、方法、事件的应用    在窗体上用于输入、输出信息的图形或文字符号称为控件。为了方便用户开发应用程序,vb中提供了很多的控件,其中很多控件是windows本身的资源,如命令按钮、标签、文本框等,也有一些是为了增强用户界面,或是使用户界面美观实用而编制的activex控件。

  3. 一、鼠标事件 Cilck:用户单击鼠标键时发生 DbLClick:用户双击鼠标键时发生 MouseDown:用户释放鼠标键时发生 MouseMove:移动鼠标时发生

  4. 二、 键盘事件 KeyDown:在键按下时触发。(可获得一个KeyCode值) KeyUp:在键弹起时触发。(可获得一个KeyCode值) KeyPress:在键盘按下再弹起时发生。(它可获得KeyAscii值)

  5. 三、简单的鼠标事件介绍 可通过MouseDown、MouseUp、MouseMove 事件使应用程序对鼠标位置及状态的变化作出响应 MouseUp事件示例: Private Sub TxtContent_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) LblStart.Caption = "起始位置:" & TxtContent.SelStartLblLength.Caption = "文本长度:" & TxtContent.SelLengthEnd Sub

  6. 四、keydown,keyup事件说明 keydown ,keyup ‘按键(在显示出按键内容的同时)与提起按键时触发事件  参数:[index as integer ,] keycode as integert, shift as integer) a. index: 当控件是控件数组中的一个元素时,index代表在数组是索引值,即下标 b. keycode: 键盘扫描码。 ‘例:13对应回车 c. shift: 代表shift, ctrl ,alt 键的状态 ; 是一个3位的二进制位域 。         (111)=> (alt ,ctrl ,shift)        故 shift= 6 代表:110 => 同时按下alt,ctrl两键 以上可判定是否只按下某键; 但是如果需要了解是否按下了某键,则需要与特定数进行屏蔽  例:判定是否按下shift(不管是组合键或是单键) if shift and 1 >0 then msgbox “你按下了shift键 ”if shift and 1 >0 then msgbox “你按下了shift键 ”

  7. 五、keypress事件说明: Keypress事件:用于截获在文本框/ 组合框中输入的按键; Sub 控件名_keypress ([index as integer,] keyascii as integer )   参数:([index as integer ,] keyascii as integer) a. index: 当控件是控件数组中的一个元素时,index代表在数组是索引值,即下标 b. keyascii: 返回标准的ascii键码    可以用chr(keyascii) 将ascii 转化为相应的字符;或用 =asc(char) 将字符转为ascii码。(用于判定输入是否为某字符)    注意:keypress 事件不是指出所按字符在键盘上的物理位置,而是传送一个字 符,故它可以判别大小写。    相反,keyup/ keydown能识别的是所按字符在键盘上的物理位置,则大小写字符的keycode码是一样的。    (若要在keyup/ keydown 中判别字符的大小写,需判定是否为shift +该键)

  8. 六、鼠标事件综合示例 响 应SysTray 中 图 标 的 鼠 标 事 件: Private TrayIcon As NOTIFYICONDATA Private Sub bAdd-Click() ' 在SysTray中添加图标 TrayIcon.cbSize=Len(TrayIcon) TrayIcon.hwnd=Me.hwnd TrayIcon.uID=vbNull TrayIcon.uFlags=NIF-ICON Or NIF-MESSAGE Or NTF-TIP TrayIcon.uCallbackMessage=WM-MOUSEMOVE TrayIcon.hIcon=Me.Icon TrayIcon.szTip="Test of tray icon! "& vbNullChar xx%=Shell-NotifyIcon(NIM-ADD, TrayIcon) End Sub Private Sub Form-MouseMove (Button As Integer,Shift_ As Integer,X As Single,Y As Single) Select Case X Case WM-LBUTTONDBLCLK ‘在此添加响应代码 Case WM-LBUTTONDOWN ‘在此添加响应代码 ……’其他鼠标事件 End Select ……‘其他代码 End Sub

  9. 七、键盘事件综合示例 检验输入 Private Sub Command1_Click() num = Val(Text_day) If num > 31 Or num < 1 Then Beep : MsgBox “ 输入日期有误“ : Text_day.SetFocus : Exit Sub End If num = Val(Text_month.Text) If num > 12 Or num < 1 ThenBeep : MsgBox “输入月份有误“ : Text_month.SetFocus : Exit Sub End If num = Val(Text_year.Text) If num > 2001 Or num < 1900 Then Beep : MsgBox “输入年份有误“ : Text_year.SetFocus : Exit Sub End If End Sub Private Sub Text_day_KeyPress(KeyAscii As Integer) If KeyAscii < 48 And KeyAscii <> 8 Or KeyAscii > 57 Then Beep : MsgBox “请输入数字“ : KeyAscii = 0 End If End Sub 下一页

  10. 七、键盘事件综合示例 检验输入 接上页 Private Sub Text_month_KeyPress(KeyAscii As Integer) If KeyAscii < 48 And KeyAscii <> 8 Or KeyAscii > 57 Then Beep : MsgBox "请输入数字“ KeyAscii = 0End If End Sub Private Sub Text_passwd_KeyPress(KeyAscii As Integer) If KeyAscii < 48 And KeyAscii <> 8 Or KeyAscii > 57 Then Beep : MsgBox "请输入数字“ : KeyAscii = 0 End If End Sub Private Sub Text_year_KeyPress(KeyAscii As Integer) If KeyAscii < 48 And KeyAscii <> 8 Or KeyAscii > 57 Then Beep : MsgBox "请输入数字“ : KeyAscii = 0 End If ‘8代表退格符 End Sub Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 13 Then MsgBox “按下了回车符" If (Shift And 2) > 1 Then MsgBox “你按下了ctrl键" End Sub

More Related