第四章VC编程中关于键盘和鼠标消息的响应-ppt课件_第1页
第四章VC编程中关于键盘和鼠标消息的响应-ppt课件_第2页
第四章VC编程中关于键盘和鼠标消息的响应-ppt课件_第3页
第四章VC编程中关于键盘和鼠标消息的响应-ppt课件_第4页
第四章VC编程中关于键盘和鼠标消息的响应-ppt课件_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Visual C+ Visual C+ 与面向对象程序设计与面向对象程序设计第第4 4章章 VC+VC+编程中关于键盘编程中关于键盘与鼠标消息的响应与鼠标消息的响应德州学院德州学院 计算机系计算机系主讲教师主讲教师 韩金姝韩金姝 本章主要内容本章主要内容v 键盘在应用程序中的应用键盘在应用程序中的应用v 键盘操作应用举例键盘操作应用举例v 鼠标在应用程序中的应用鼠标在应用程序中的应用v 鼠标应用举例鼠标应用举例v 例题和习题中涉及的函数例题和习题中涉及的函数 键盘在应用程序中的应用键盘在应用程序中的应用v键盘消息的生成键盘消息的生成用户按键用户按键键盘中断处理程序对所击键编码扫描码键盘中断处理

2、程序对所击键编码扫描码-虚虚拟码,以实现设备无关性)拟码,以实现设备无关性)调用调用Windows的的USER.EXE程序生成键盘消息程序生成键盘消息到消息队列等候处理到消息队列等候处理 键盘在应用程序中的应用键盘在应用程序中的应用v 键盘消息分类键盘消息分类v按键消息按键消息v字符消息字符消息v 输入焦点和插字符输入焦点和插字符按键消息按键消息v 按键消息分类按键消息分类v系统按键消息:系统按键消息:v AltAlt键键+ +相关输入键组合产生的消息。由相关输入键组合产生的消息。由WindowsWindows系统内部处理。系统内部处理。WM_SYSKEYDOWN/ WM_SYSKEYDOWN

3、/ WM_SYSKEYUPWM_SYSKEYUPv非系统按键消息非系统按键消息: :v 不使用不使用AltAlt键组合的消息。键组合的消息。WM_KEYDOWN/ WM_KEYDOWN/ WM_KEYUPWM_KEYUP按键消息按键消息v 按键消息的构成:按键消息的构成:v 包含扫描码、虚拟码以及其他与击键包含扫描码、虚拟码以及其他与击键有关的消息。放在有关的消息。放在wParamwParam和和lParamlParam中中v v 虚拟码是与设备无关的键盘编码,其虚拟码是与设备无关的键盘编码,其值存放在按键消息的值存放在按键消息的wParamwParam参数中表参数中表4-1)4-1)v lP

4、aram lParam不同位数的含义不同位数的含义( (表表4-2)4-2) 字符消息字符消息v WinMain WinMain函数的消息循环中函数的消息循环中TranslateMessageTranslateMessage函数把按键消息函数把按键消息转化为字符消息,当键盘驱动程序转化为字符消息,当键盘驱动程序把键盘字符映射成把键盘字符映射成ASCIIASCII码后,产生码后,产生字符消息。字符消息。( (表表4-3)4-3) 输入焦点和插字符输入焦点和插字符v 输入焦点输入焦点v 应用程序的众多窗口共享键盘,仅有应用程序的众多窗口共享键盘,仅有一个窗口过程能接收键盘消息,称为有一个窗口过程能

5、接收键盘消息,称为有输入焦点的窗口。窗口函数通过捕获输入焦点的窗口。窗口函数通过捕获WM_SETFOCUS/ WM_KILLFOCUSWM_SETFOCUS/ WM_KILLFOCUS消息以确定消息以确定当前窗口是否具有输入焦点。当前窗口是否具有输入焦点。v 插字符插字符v WindowsWindows操作系统用插字符指示当前操作系统用插字符指示当前正文位置。插字符是应用程序共享的系正文位置。插字符是应用程序共享的系统资源。统资源。v 只有拥有只有拥有“输入焦点的窗口才能输入焦点的窗口才能拥有插字符。拥有插字符。 键盘操作应用举例键盘操作应用举例v 例例4 41 1P51P51):键盘消息处理

6、程序):键盘消息处理程序v 单击一个键后,在窗口中依次显示单击一个键后,在窗口中依次显示出:按键消息、参数出:按键消息、参数wParamwParam的值,若的值,若为字符消息时,还显示出相应字母、为字符消息时,还显示出相应字母、重复记位数、重复记位数、OEMOEM扫描码、扩展键标扫描码、扩展键标志、志、AltAlt键按下标志、按键的先前状键按下标志、按键的先前状态和转换状态等内容。态和转换状态等内容。演示程序演示程序1 1键盘操作应用举例键盘操作应用举例v 习题习题4.24.2P69):P69):v 设计一个窗口,在该窗口中练习键盘设计一个窗口,在该窗口中练习键盘的响应,要求如下:的响应,要求

7、如下:v (1 1单击键盘的向上箭头时,窗口中单击键盘的向上箭头时,窗口中显示显示You had hitted the up key”You had hitted the up key”v (2 2单击单击键时,窗口中显示键时,窗口中显示You had hitted the SHIFT key”You had hitted the SHIFT key”v (3 3单击单击键时,窗口中显示键时,窗口中显示You had hitted the CTRL key”You had hitted the CTRL key”演示程序演示程序2 2 鼠标在应用程序中的应用鼠标在应用程序中的应用v鼠标是一种定

8、位输入设备,有单击、鼠标是一种定位输入设备,有单击、双击和拖动功能。双击和拖动功能。vWINDOWSWINDOWS中通过光标指示当前鼠标位中通过光标指示当前鼠标位置。系统预定义光标形式见表置。系统预定义光标形式见表4 44 4v应用程序加载光标资源:应用程序加载光标资源:v 在定义窗口类或者在程序中调用在定义窗口类或者在程序中调用LoadCursor(hThisInst,lpszCursorLoadCursor(hThisInst,lpszCursorname)name)鼠标在应用程序中的应用鼠标在应用程序中的应用v 鼠标消息的产生:鼠标消息的产生:v WindowsWindows操作系统通过

9、鼠标设备驱操作系统通过鼠标设备驱动程序接收鼠标输入。鼠标设备驱动程序接收鼠标输入。鼠标设备驱动程序在启动动程序在启动WindowsWindows时装入。时装入。v 在窗口内有鼠标事件发生在窗口内有鼠标事件发生v 窗口接收一个鼠标事件窗口接收一个鼠标事件v 产生用户区鼠标消息产生用户区鼠标消息v常用的鼠标消息:表常用的鼠标消息:表4 45 5v 鼠标在应用程序中的应用鼠标在应用程序中的应用v 鼠标消息的构成:鼠标消息的构成:v 通过通过lParamlParam和和wParamwParam,可以确定,可以确定鼠标的位置和鼠标键的状态。鼠标的位置和鼠标键的状态。v lParam:lParam:包含鼠

10、标坐标位置。包含鼠标坐标位置。v 低位低位X;X;高位高位Y Y。v wParam:wParam:指示各种虚键状态的值。指示各种虚键状态的值。表表4-64-6。鼠标在应用程序中的应用鼠标在应用程序中的应用v鼠标消息的处理:鼠标消息的处理:v 1.1.非用户区鼠标消息非用户区鼠标消息: :v 当在一个窗口的用户区以外的地当在一个窗口的用户区以外的地方例如在窗框的菜单、滚动条、方例如在窗框的菜单、滚动条、工具条和标题条等处产生一个鼠工具条和标题条等处产生一个鼠标事件,就将产生一个非用户区鼠标事件,就将产生一个非用户区鼠标消息。标消息。v 非用户区鼠标消息,不由应用程非用户区鼠标消息,不由应用程序具

11、体处理,而是送往序具体处理,而是送往DefWindowProc( )DefWindowProc( )。鼠标在应用程序中的应用鼠标在应用程序中的应用v鼠标消息的处理:鼠标消息的处理:v 2. 2. 用户区鼠标消息用户区鼠标消息: :v 处理时要考虑,鼠标左键按下同处理时要考虑,鼠标左键按下同时检测时检测ShiftShift和和CtrlCtrl键:键:v Case WM_LBUTTONDOWN:Case WM_LBUTTONDOWN:v if if (wParam&MK_CONTROL)&(wParam&MK(wParam&MK_CONTROL)&(wPar

12、am&MK_SHIFT)_SHIFT)v . .v break; break;v Case WM_LBUTTONUP: Case WM_LBUTTONUP: v . .v break; break;鼠标在应用程序中的应用鼠标在应用程序中的应用v鼠标双击消息的处理:鼠标双击消息的处理:v若要使窗口函数能接收鼠标双击消若要使窗口函数能接收鼠标双击消息,在注册窗口时必须注明该窗口息,在注册窗口时必须注明该窗口类具有类具有CS_DBLCLKSCS_DBLCLKS属性。否则是两属性。否则是两次双击次双击vwndclass.style=CS_HEADRAW|CS_VEwndclass.style=

13、CS_HEADRAW|CS_VERDARW|CS_DBLCLKSRDARW|CS_DBLCLKSv双击的时间间隔:默认双击的时间间隔:默认0.5s0.5svSetDoubleClickTimeSetDoubleClickTime()重新设定()重新设定鼠标在应用程序中的应用鼠标在应用程序中的应用v 窗口对鼠标消息的捕获:窗口对鼠标消息的捕获:v 1.SetCapture(hWnd)1.SetCapture(hWnd):可以使:可以使WindowsWindows发送的所有鼠标消息均发送的所有鼠标消息均定向到某一窗口,而不管鼠标的定向到某一窗口,而不管鼠标的光标处于何处。光标处于何处。v 2. R

14、eleaseCapture( )2. ReleaseCapture( ):释放:释放鼠标,以使其它窗口正常接收鼠鼠标,以使其它窗口正常接收鼠标消息。因为:一旦从窗口捕获标消息。因为:一旦从窗口捕获了鼠标,系统的键盘功能就暂时了鼠标,系统的键盘功能就暂时失效,其它窗口也无法得到鼠标失效,其它窗口也无法得到鼠标消息。消息。鼠标应用举例v例例4-34-3P65P65):鼠标输入示范程序):鼠标输入示范程序v 通过本程序,学习如何相应鼠标通过本程序,学习如何相应鼠标信息、改变光标形状等用法,如用信息、改变光标形状等用法,如用户在窗口的不同区域移动鼠标时,户在窗口的不同区域移动鼠标时,光标将显示不同的形

15、状,如光标将显示不同的形状,如“十十字形光标、字形光标、“水平双箭头光标、水平双箭头光标、”垂直双箭头垂直双箭头“光标、光标、”沙露沙露“光标光标等。等。演示程序演示程序3 3 例题和习题中涉及的函数v RECT RECTvThe RECT structure defines the The RECT structure defines the coordinates of the upper-left and coordinates of the upper-left and lower-right corners of a rectangle. lower-right corners of

16、 a rectangle. vtypedef struct _RECTtypedef struct _RECTv v LONG left; LONG top; LONG right; LONG left; LONG top; LONG right; LONG bottom; LONG bottom; v RECT, RECT, * *PRECT; PRECT; vMembersMembersvleft Specifies the x-coordinate of left Specifies the x-coordinate of the upper-left corner of the the

17、 upper-left corner of the rectangle. rectangle. vtop Specifies the y-coordinate of top Specifies the y-coordinate of the upper-left corner of the the upper-left corner of the rectangle. rectangle. vright Specifies the x-coordinate of right Specifies the x-coordinate of the lower-right corner of the

18、the lower-right corner of the rectangle. rectangle. vbottom Specifies the y-coordinate bottom Specifies the y-coordinate of the lower-right corner of the of the lower-right corner of the rectangle. rectangle. 例题和习题中涉及的函数ScrollWindow ScrollWindow function scrolls the contents of the function scrolls

19、the contents of the specified windows client area.specified windows client area.BOOLBOOL ScrollWindowScrollWindow( ( HWNDHWND hWnhWnd, d, intint XAmount, XAmount, intint YAmount, YAmount, const const RECTRECT * *lpRect, lpRect, const RECTconst RECT * *lpClipRect ); lpClipRect ); ParametersParameters

20、hWnd inhWnd in Handle to the window where the Handle to the window where the client area is to be scrolled. client area is to be scrolled. XAmount inXAmount in Specifies the amount, in Specifies the amount, in device units, of horizontal scrolling. device units, of horizontal scrolling. If the windo

21、w being scrolled has the If the window being scrolled has the CS_OWNDC or CS_CLASSDC style, then this CS_OWNDC or CS_CLASSDC style, then this parameter uses logical units rather than parameter uses logical units rather than device units. This parameter must be a device units. This parameter must be

22、a negative value to scroll the content of negative value to scroll the content of the window to the left. the window to the left. YAmount inYAmount in Specifies the amount, in Specifies the amount, in device units, of vertical scrolling. If device units, of vertical scrolling. If the window being sc

23、rolled has the the window being scrolled has the CS_OWNDC or CS_CLASSDC style, then this CS_OWNDC or CS_CLASSDC style, then this parameter uses logical units rather than parameter uses logical units rather than device units. This parameter must be a device units. This parameter must be a negative va

24、lue to scroll the content of negative value to scroll the content of the window up. the window up. lpRect inlpRect in Pointer to the RECT structure Pointer to the RECT structure specifying the portion of the client specifying the portion of the client area to be scrolled. If this parameter area to b

25、e scrolled. If this parameter is NULL, the entire client area is is NULL, the entire client area is scrolled. scrolled. lpClipRect inlpClipRect in Pointer to the RECT Pointer to the RECT structure containing the coordinates of structure containing the coordinates of the clipping rectangle. Only devi

26、ce bits the clipping rectangle. Only device bits within the clipping rectangle are within the clipping rectangle are affected. Bits scrolled from the outside affected. Bits scrolled from the outside of the rectangle to the inside are of the rectangle to the inside are painted; bits scrolled from the

27、 inside painted; bits scrolled from the inside of the rectangle to the outside are not of the rectangle to the outside are not painted. painted. 例题和习题中涉及的函数WsprintfWsprintf formats and stores a series of formats and stores a series of characters and values in a buffer. characters and values in a buf

28、fer. Any arguments are converted and Any arguments are converted and copied to the output buffer copied to the output buffer according to the corresponding according to the corresponding format specification in the format format specification in the format string. The function appends a string. The

29、function appends a terminating null character to the terminating null character to the characters it writes, but the characters it writes, but the return value does not include the return value does not include the terminating null character in its terminating null character in its character count.

30、character count. If the function succeeds, the If the function succeeds, the return value is the number of return value is the number of characters stored in the output characters stored in the output buffer, not counting the buffer, not counting the terminating null character. If the terminating nu

31、ll character. If the function fails, the return value is function fails, the return value is less than the length of the less than the length of the expected output. To get extended expected output. To get extended error information, call error information, call GetLastError.GetLastError. . ValidateRect ValidateRect validates the client area within a validates the client area within a rectangle b

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论