Visual C+Visual C+与面向对象程序设计与面向对象程序设计第第4 4章章 VC+VC+编程中关于键盘编程中关于键盘与鼠标音讯的呼应与鼠标音讯的呼应德州学院德州学院 计算机系计算机系主讲教师主讲教师 韩金姝韩金姝 本章主要内容本章主要内容v 键盘在运用程序中的运用键盘在运用程序中的运用v 键盘操作运用举例键盘操作运用举例v 鼠标在运用程序中的运用鼠标在运用程序中的运用v 鼠标运用举例鼠标运用举例v 例题和习题中涉及的函数例题和习题中涉及的函数 键盘在运用程序中的运用键盘在运用程序中的运用v键盘音讯的生成键盘音讯的生成用户按键用户按键键盘中断处置程序对所击键编码扫描码键盘中断处置程序对所击键编码扫描码-虚虚拟码,以实现设备无关性拟码,以实现设备无关性调用调用Windows的的USER.EXE程序生成键盘音讯程序生成键盘音讯到音讯队列等候处置到音讯队列等候处置 键盘在运用程序中的运用键盘在运用程序中的运用v 键盘音讯分类键盘音讯分类v按键音讯按键音讯v字符音讯字符音讯v 输入焦点和插字符输入焦点和插字符按键音讯按键音讯v 按键音讯分类按键音讯分类v系统按键音讯:系统按键音讯:v AltAlt键键+相关输入键组合产生的音讯。
由相关输入键组合产生的音讯由WindowsWindows系统内部处置系统内部处置WM_SYSKEYDOWN/WM_SYSKEYDOWN/WM_SYSKEYUPWM_SYSKEYUPv非系统按键音讯非系统按键音讯:v 不运用不运用AltAlt键组合的音讯键组合的音讯WM_KEYDOWN/WM_KEYDOWN/WM_KEYUPWM_KEYUP按键音讯按键音讯v 按键音讯的构成:按键音讯的构成:v 包含扫描码、虚拟码以及其他与击键包含扫描码、虚拟码以及其他与击键有关的音讯放在有关的音讯放在wParamwParam和和lParamlParam中中v v 虚拟码是与设备无关的键盘编码,其虚拟码是与设备无关的键盘编码,其值存放在按键音讯的值存放在按键音讯的wParamwParam参数中表参数中表4-1)4-1)v lParam lParam不同位数的含义不同位数的含义(表表4-2)4-2)字符音讯字符音讯v WinMain WinMain函数的音讯循环中函数的音讯循环中TranslateMessageTranslateMessage函数把按键音讯函数把按键音讯转化为字符音讯,当键盘驱动程序转化为字符音讯,当键盘驱动程序把键盘字符映射成把键盘字符映射成ASCIIASCII码后,产生码后,产生字符音讯。
字符音讯表表4-3)4-3)输入焦点和插字符输入焦点和插字符v 输入焦点输入焦点v 运用程序的众多窗口共享键盘,仅有运用程序的众多窗口共享键盘,仅有一个窗口过程能接纳键盘音讯,称为有一个窗口过程能接纳键盘音讯,称为有输入焦点的窗口窗口函数经过捕获输入焦点的窗口窗口函数经过捕获WM_SETFOCUS/WM_KILLFOCUSWM_SETFOCUS/WM_KILLFOCUS音讯以确定音讯以确定当前窗口能否具有输入焦点当前窗口能否具有输入焦点v 插字符插字符v WindowsWindows操作系统用插字符指示当前操作系统用插字符指示当前正文位置插字符是运用程序共享的系正文位置插字符是运用程序共享的系统资源v 只需拥有只需拥有“输入焦点的窗口才干输入焦点的窗口才干拥有插字符拥有插字符键盘操作运用举例键盘操作运用举例v 例例4 41 1P51P51:键盘音讯处置程序:键盘音讯处置程序v 单击一个键后,在窗口中依次显示单击一个键后,在窗口中依次显示出:按键音讯、参数出:按键音讯、参数wParamwParam的值,假的值,假设为字符音讯时,还显示出相应字母、设为字符音讯时,还显示出相应字母、反复记位数、反复记位数、OEMOEM扫描码、扩展键标扫描码、扩展键标志、志、AltAlt键按下标志、按键的先前形键按下标志、按键的先前形状和转换形状等内容。
状和转换形状等内容演示程序演示程序1 1键盘操作运用举例键盘操作运用举例v 习题习题4.24.2P69):P69):v 设计一个窗口,在该窗口中练习键盘设计一个窗口,在该窗口中练习键盘的呼应,要求如下:的呼应,要求如下:v 1 1单击键盘的向上箭头时,窗口中单击键盘的向上箭头时,窗口中显示显示You had hitted the up keyYou had hitted the up keyv 2 2单击单击键时,窗口中显示键时,窗口中显示You had hitted the SHIFT keyYou had hitted the SHIFT keyv 3 3单击单击键时,窗口中显示键时,窗口中显示You had hitted the CTRL keyYou had hitted the CTRL key演示程序演示程序2 2 鼠标在运用程序中的运用鼠标在运用程序中的运用v鼠标是一种定位输入设备,有单击、鼠标是一种定位输入设备,有单击、双击和拖动功能双击和拖动功能vWINDOWSWINDOWS中经过光标指示当前鼠标位中经过光标指示当前鼠标位置系统预定义光标方式见表置系统预定义光标方式见表4 44 4v运用程序加载光标资源:运用程序加载光标资源:v 在定义窗口类或者在程序中调用在定义窗口类或者在程序中调用LoadCursor(hThisInst,lpszCursorLoadCursor(hThisInst,lpszCursorname)name)鼠标在运用程序中的运用鼠标在运用程序中的运用v 鼠标音讯的产生:鼠标音讯的产生:v WindowsWindows操作系统经过鼠标设备驱操作系统经过鼠标设备驱动程序接纳鼠标输入。
鼠标设备驱动程序接纳鼠标输入鼠标设备驱动程序在启动动程序在启动WindowsWindows时装入v 在窗口内有鼠标事件发生在窗口内有鼠标事件发生v 窗口接纳一个鼠标事件窗口接纳一个鼠标事件v 产生用户区鼠标音讯产生用户区鼠标音讯v常用的鼠标音讯:表常用的鼠标音讯:表4 45 5v 鼠标在运用程序中的运用鼠标在运用程序中的运用v 鼠标音讯的构成:鼠标音讯的构成:v 经过经过lParamlParam和和wParamwParam,可以确定,可以确定鼠标的位置和鼠标键的形状鼠标的位置和鼠标键的形状v lParam:lParam:包含鼠标坐标位置包含鼠标坐标位置v 低位低位X;X;高位高位Y Yv wParam:wParam:指示各种虚键形状的值指示各种虚键形状的值表表4-64-6鼠标在运用程序中的运用鼠标在运用程序中的运用v鼠标音讯的处置:鼠标音讯的处置:v 1.1.非用户区鼠标音讯非用户区鼠标音讯:v 当在一个窗口的用户区以外的地当在一个窗口的用户区以外的地方例如在窗框的菜单、滚动条、方例如在窗框的菜单、滚动条、工具条和标题条等处产生一个鼠工具条和标题条等处产生一个鼠标事件,就将产生一个非用户区鼠标事件,就将产生一个非用户区鼠标音讯。
标音讯v 非用户区鼠标音讯,不由运用程非用户区鼠标音讯,不由运用程序详细处置,而是送往序详细处置,而是送往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)&(wParam&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=CS_HEADRAW|CS_VERDARW|CS_DBLCLKSRDARW|CS_DBLCLKSv双击的时间间隔:默许双击的时间间隔:默许0.5s0.5svSetDoubleClickTimeSetDoubleClickTime重新设定重新设定鼠标在运用程序中的运用鼠标在运用程序中的运用v 窗口对鼠标音讯的捕获:窗口对鼠标音讯的捕获:v 1.SetCapture(hWnd)1.SetCapture(hWnd):可以使:可以使WindowsWindows发送的一切鼠标音讯均发送的一切鼠标音讯均定向到某一窗口,而不论鼠标的定向到某一窗口,而不论鼠标的光标处于何处光标处于何处v 2.ReleaseCapture()2.ReleaseCapture():释放:释放鼠标,以使其它窗口正常接纳鼠鼠标,以使其它窗口正常接纳鼠标音讯由于:一旦从窗口捕获标音讯由于:一旦从窗口捕获了鼠标,系统的键盘功能就暂时了鼠标,系统的键盘功能就暂时失效,其它窗口也无法得到鼠标失效,其它窗口也无法得到鼠标音讯。
音讯鼠标运用举例v例例4-34-3P65P65:鼠标输入示范程序:鼠标输入示范程序v 经过本程序,学习如何相应鼠标经过本程序,学习如何相应鼠标信息、改动光标外形等用法,如用信息、改动光标外形等用法,如用户在窗口的不同区域挪动鼠标时,户在窗口的不同区域挪动鼠标时,光标将显示不同的外形,如光标将显示不同的外形,如“十十字形光标、字形光标、“程度双箭头光标、程度双箭头光标、垂直双箭头垂直双箭头“光标、沙露光标、沙露“光光标等演示程序演示程序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 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 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 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 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);ParametersParametershWnd 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 window 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 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 scrolled 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 value 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 be 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 device 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 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 buffer.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 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.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 null 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 by removing the rectangle from rectangle by removing the rectangle from the update region of the specified the update region of the specified window window hWnd hWnd in Handle to the window whose update in Handle to the window whose update region is to be modified.If this region is to be modified.If this parameter is NULL,the system parameter is NULL,the system invalidates and redraws all windows and invalidates and redraws all windows and sends the WM_ERASEBKGND and WM_NCPAINT sends the WM_ERASEBKGND and WM_NCPAINT messages to the window procedure before messages to the window procedure before the function returns.the function returns.lpRect lpRect in Pointer to a RECT structure that in Pointer to a RECT structure that contains the client coordinates of the contains the client coordinates of the rectangle to be removed from the update rectangle to be removed from the update region.If this parameter is NULL,the region.If this parameter is NULL,the entire client area is removed.entire client area is removed.例题和习题中涉及的函数。