第四章VC編程中關(guān)于鍵盤和鼠標(biāo)消息的響應(yīng)-ppt課件_第1頁
第四章VC編程中關(guān)于鍵盤和鼠標(biāo)消息的響應(yīng)-ppt課件_第2頁
第四章VC編程中關(guān)于鍵盤和鼠標(biāo)消息的響應(yīng)-ppt課件_第3頁
第四章VC編程中關(guān)于鍵盤和鼠標(biāo)消息的響應(yīng)-ppt課件_第4頁
第四章VC編程中關(guān)于鍵盤和鼠標(biāo)消息的響應(yīng)-ppt課件_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Visual C+ Visual C+ 與面向?qū)ο蟪绦蛟O(shè)計(jì)與面向?qū)ο蟪绦蛟O(shè)計(jì)第第4 4章章 VC+VC+編程中關(guān)于鍵盤編程中關(guān)于鍵盤與鼠標(biāo)消息的響應(yīng)與鼠標(biāo)消息的響應(yīng)德州學(xué)院德州學(xué)院 計(jì)算機(jī)系計(jì)算機(jī)系主講教師主講教師 韓金姝韓金姝 本章主要內(nèi)容本章主要內(nèi)容v 鍵盤在應(yīng)用程序中的應(yīng)用鍵盤在應(yīng)用程序中的應(yīng)用v 鍵盤操作應(yīng)用舉例鍵盤操作應(yīng)用舉例v 鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v 鼠標(biāo)應(yīng)用舉例鼠標(biāo)應(yīng)用舉例v 例題和習(xí)題中涉及的函數(shù)例題和習(xí)題中涉及的函數(shù) 鍵盤在應(yīng)用程序中的應(yīng)用鍵盤在應(yīng)用程序中的應(yīng)用v鍵盤消息的生成鍵盤消息的生成用戶按鍵用戶按鍵鍵盤中斷處理程序?qū)λ鶕翩I編碼掃描碼鍵盤中斷處理

2、程序?qū)λ鶕翩I編碼掃描碼-虛虛擬碼,以實(shí)現(xiàn)設(shè)備無關(guān)性)擬碼,以實(shí)現(xiàn)設(shè)備無關(guān)性)調(diào)用調(diào)用Windows的的USER.EXE程序生成鍵盤消息程序生成鍵盤消息到消息隊(duì)列等候處理到消息隊(duì)列等候處理 鍵盤在應(yīng)用程序中的應(yīng)用鍵盤在應(yīng)用程序中的應(yīng)用v 鍵盤消息分類鍵盤消息分類v按鍵消息按鍵消息v字符消息字符消息v 輸入焦點(diǎn)和插字符輸入焦點(diǎn)和插字符按鍵消息按鍵消息v 按鍵消息分類按鍵消息分類v系統(tǒng)按鍵消息:系統(tǒng)按鍵消息:v AltAlt鍵鍵+ +相關(guān)輸入鍵組合產(chǎn)生的消息。由相關(guān)輸入鍵組合產(chǎn)生的消息。由WindowsWindows系統(tǒng)內(nèi)部處理。系統(tǒng)內(nèi)部處理。WM_SYSKEYDOWN/ WM_SYSKEYDOWN

3、/ WM_SYSKEYUPWM_SYSKEYUPv非系統(tǒng)按鍵消息非系統(tǒng)按鍵消息: :v 不使用不使用AltAlt鍵組合的消息。鍵組合的消息。WM_KEYDOWN/ WM_KEYDOWN/ WM_KEYUPWM_KEYUP按鍵消息按鍵消息v 按鍵消息的構(gòu)成:按鍵消息的構(gòu)成:v 包含掃描碼、虛擬碼以及其他與擊鍵包含掃描碼、虛擬碼以及其他與擊鍵有關(guān)的消息。放在有關(guān)的消息。放在wParamwParam和和lParamlParam中中v v 虛擬碼是與設(shè)備無關(guān)的鍵盤編碼,其虛擬碼是與設(shè)備無關(guān)的鍵盤編碼,其值存放在按鍵消息的值存放在按鍵消息的wParamwParam參數(shù)中表參數(shù)中表4-1)4-1)v lP

4、aram lParam不同位數(shù)的含義不同位數(shù)的含義( (表表4-2)4-2) 字符消息字符消息v WinMain WinMain函數(shù)的消息循環(huán)中函數(shù)的消息循環(huán)中TranslateMessageTranslateMessage函數(shù)把按鍵消息函數(shù)把按鍵消息轉(zhuǎn)化為字符消息,當(dāng)鍵盤驅(qū)動(dòng)程序轉(zhuǎn)化為字符消息,當(dāng)鍵盤驅(qū)動(dòng)程序把鍵盤字符映射成把鍵盤字符映射成ASCIIASCII碼后,產(chǎn)生碼后,產(chǎn)生字符消息。字符消息。( (表表4-3)4-3) 輸入焦點(diǎn)和插字符輸入焦點(diǎn)和插字符v 輸入焦點(diǎn)輸入焦點(diǎn)v 應(yīng)用程序的眾多窗口共享鍵盤,僅有應(yīng)用程序的眾多窗口共享鍵盤,僅有一個(gè)窗口過程能接收鍵盤消息,稱為有一個(gè)窗口過程能

5、接收鍵盤消息,稱為有輸入焦點(diǎn)的窗口。窗口函數(shù)通過捕獲輸入焦點(diǎn)的窗口。窗口函數(shù)通過捕獲WM_SETFOCUS/ WM_KILLFOCUSWM_SETFOCUS/ WM_KILLFOCUS消息以確定消息以確定當(dāng)前窗口是否具有輸入焦點(diǎn)。當(dāng)前窗口是否具有輸入焦點(diǎn)。v 插字符插字符v WindowsWindows操作系統(tǒng)用插字符指示當(dāng)前操作系統(tǒng)用插字符指示當(dāng)前正文位置。插字符是應(yīng)用程序共享的系正文位置。插字符是應(yīng)用程序共享的系統(tǒng)資源。統(tǒng)資源。v 只有擁有只有擁有“輸入焦點(diǎn)的窗口才能輸入焦點(diǎn)的窗口才能擁有插字符。擁有插字符。 鍵盤操作應(yīng)用舉例鍵盤操作應(yīng)用舉例v 例例4 41 1P51P51):鍵盤消息處理

6、程序):鍵盤消息處理程序v 單擊一個(gè)鍵后,在窗口中依次顯示單擊一個(gè)鍵后,在窗口中依次顯示出:按鍵消息、參數(shù)出:按鍵消息、參數(shù)wParamwParam的值,若的值,若為字符消息時(shí),還顯示出相應(yīng)字母、為字符消息時(shí),還顯示出相應(yīng)字母、重復(fù)記位數(shù)、重復(fù)記位數(shù)、OEMOEM掃描碼、擴(kuò)展鍵標(biāo)掃描碼、擴(kuò)展鍵標(biāo)志、志、AltAlt鍵按下標(biāo)志、按鍵的先前狀鍵按下標(biāo)志、按鍵的先前狀態(tài)和轉(zhuǎn)換狀態(tài)等內(nèi)容。態(tài)和轉(zhuǎn)換狀態(tài)等內(nèi)容。演示程序演示程序1 1鍵盤操作應(yīng)用舉例鍵盤操作應(yīng)用舉例v 習(xí)題習(xí)題4.24.2P69):P69):v 設(shè)計(jì)一個(gè)窗口,在該窗口中練習(xí)鍵盤設(shè)計(jì)一個(gè)窗口,在該窗口中練習(xí)鍵盤的響應(yīng),要求如下:的響應(yīng),要求

7、如下:v (1 1單擊鍵盤的向上箭頭時(shí),窗口中單擊鍵盤的向上箭頭時(shí),窗口中顯示顯示You had hitted the up key”You had hitted the up key”v (2 2單擊單擊鍵時(shí),窗口中顯示鍵時(shí),窗口中顯示You had hitted the SHIFT key”You had hitted the SHIFT key”v (3 3單擊單擊鍵時(shí),窗口中顯示鍵時(shí),窗口中顯示You had hitted the CTRL key”You had hitted the CTRL key”演示程序演示程序2 2 鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v鼠標(biāo)是一種定

8、位輸入設(shè)備,有單擊、鼠標(biāo)是一種定位輸入設(shè)備,有單擊、雙擊和拖動(dòng)功能。雙擊和拖動(dòng)功能。vWINDOWSWINDOWS中通過光標(biāo)指示當(dāng)前鼠標(biāo)位中通過光標(biāo)指示當(dāng)前鼠標(biāo)位置。系統(tǒng)預(yù)定義光標(biāo)形式見表置。系統(tǒng)預(yù)定義光標(biāo)形式見表4 44 4v應(yīng)用程序加載光標(biāo)資源:應(yīng)用程序加載光標(biāo)資源:v 在定義窗口類或者在程序中調(diào)用在定義窗口類或者在程序中調(diào)用LoadCursor(hThisInst,lpszCursorLoadCursor(hThisInst,lpszCursorname)name)鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v 鼠標(biāo)消息的產(chǎn)生:鼠標(biāo)消息的產(chǎn)生:v WindowsWindows操作系統(tǒng)通過

9、鼠標(biāo)設(shè)備驅(qū)操作系統(tǒng)通過鼠標(biāo)設(shè)備驅(qū)動(dòng)程序接收鼠標(biāo)輸入。鼠標(biāo)設(shè)備驅(qū)動(dòng)程序接收鼠標(biāo)輸入。鼠標(biāo)設(shè)備驅(qū)動(dòng)程序在啟動(dòng)動(dòng)程序在啟動(dòng)WindowsWindows時(shí)裝入。時(shí)裝入。v 在窗口內(nèi)有鼠標(biāo)事件發(fā)生在窗口內(nèi)有鼠標(biāo)事件發(fā)生v 窗口接收一個(gè)鼠標(biāo)事件窗口接收一個(gè)鼠標(biāo)事件v 產(chǎn)生用戶區(qū)鼠標(biāo)消息產(chǎn)生用戶區(qū)鼠標(biāo)消息v常用的鼠標(biāo)消息:表常用的鼠標(biāo)消息:表4 45 5v 鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v 鼠標(biāo)消息的構(gòu)成:鼠標(biāo)消息的構(gòu)成:v 通過通過lParamlParam和和wParamwParam,可以確定,可以確定鼠標(biāo)的位置和鼠標(biāo)鍵的狀態(tài)。鼠標(biāo)的位置和鼠標(biāo)鍵的狀態(tài)。v lParam:lParam:包含鼠

10、標(biāo)坐標(biāo)位置。包含鼠標(biāo)坐標(biāo)位置。v 低位低位X;X;高位高位Y Y。v wParam:wParam:指示各種虛鍵狀態(tài)的值。指示各種虛鍵狀態(tài)的值。表表4-64-6。鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v鼠標(biāo)消息的處理:鼠標(biāo)消息的處理:v 1.1.非用戶區(qū)鼠標(biāo)消息非用戶區(qū)鼠標(biāo)消息: :v 當(dāng)在一個(gè)窗口的用戶區(qū)以外的地當(dāng)在一個(gè)窗口的用戶區(qū)以外的地方例如在窗框的菜單、滾動(dòng)條、方例如在窗框的菜單、滾動(dòng)條、工具條和標(biāo)題條等處產(chǎn)生一個(gè)鼠工具條和標(biāo)題條等處產(chǎn)生一個(gè)鼠標(biāo)事件,就將產(chǎn)生一個(gè)非用戶區(qū)鼠標(biāo)事件,就將產(chǎn)生一個(gè)非用戶區(qū)鼠標(biāo)消息。標(biāo)消息。v 非用戶區(qū)鼠標(biāo)消息,不由應(yīng)用程非用戶區(qū)鼠標(biāo)消息,不由應(yīng)用程序具

11、體處理,而是送往序具體處理,而是送往DefWindowProc( )DefWindowProc( )。鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v鼠標(biāo)消息的處理:鼠標(biāo)消息的處理:v 2. 2. 用戶區(qū)鼠標(biāo)消息用戶區(qū)鼠標(biāo)消息: :v 處理時(shí)要考慮,鼠標(biāo)左鍵按下同處理時(shí)要考慮,鼠標(biāo)左鍵按下同時(shí)檢測時(shí)檢測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;鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v鼠標(biāo)雙擊消息的處理:鼠標(biāo)雙擊消息的處理:v若要使窗口函數(shù)能接收鼠標(biāo)雙擊消若要使窗口函數(shù)能接收鼠標(biāo)雙擊消息,在注冊窗口時(shí)必須注明該窗口息,在注冊窗口時(shí)必須注明該窗口類具有類具有CS_DBLCLKSCS_DBLCLKS屬性。否則是兩屬性。否則是兩次雙擊次雙擊vwndclass.style=CS_HEADRAW|CS_VEwndclass.style=

13、CS_HEADRAW|CS_VERDARW|CS_DBLCLKSRDARW|CS_DBLCLKSv雙擊的時(shí)間間隔:默認(rèn)雙擊的時(shí)間間隔:默認(rèn)0.5s0.5svSetDoubleClickTimeSetDoubleClickTime()重新設(shè)定()重新設(shè)定鼠標(biāo)在應(yīng)用程序中的應(yīng)用鼠標(biāo)在應(yīng)用程序中的應(yīng)用v 窗口對(duì)鼠標(biāo)消息的捕獲:窗口對(duì)鼠標(biāo)消息的捕獲:v 1.SetCapture(hWnd)1.SetCapture(hWnd):可以使:可以使WindowsWindows發(fā)送的所有鼠標(biāo)消息均發(fā)送的所有鼠標(biāo)消息均定向到某一窗口,而不管鼠標(biāo)的定向到某一窗口,而不管鼠標(biāo)的光標(biāo)處于何處。光標(biāo)處于何處。v 2. R

14、eleaseCapture( )2. ReleaseCapture( ):釋放:釋放鼠標(biāo),以使其它窗口正常接收鼠鼠標(biāo),以使其它窗口正常接收鼠標(biāo)消息。因?yàn)椋阂坏拇翱诓东@標(biāo)消息。因?yàn)椋阂坏拇翱诓东@了鼠標(biāo),系統(tǒng)的鍵盤功能就暫時(shí)了鼠標(biāo),系統(tǒng)的鍵盤功能就暫時(shí)失效,其它窗口也無法得到鼠標(biāo)失效,其它窗口也無法得到鼠標(biāo)消息。消息。鼠標(biāo)應(yīng)用舉例v例例4-34-3P65P65):鼠標(biāo)輸入示范程序):鼠標(biāo)輸入示范程序v 通過本程序,學(xué)習(xí)如何相應(yīng)鼠標(biāo)通過本程序,學(xué)習(xí)如何相應(yīng)鼠標(biāo)信息、改變光標(biāo)形狀等用法,如用信息、改變光標(biāo)形狀等用法,如用戶在窗口的不同區(qū)域移動(dòng)鼠標(biāo)時(shí),戶在窗口的不同區(qū)域移動(dòng)鼠標(biāo)時(shí),光標(biāo)將顯示不同的形

15、狀,如光標(biāo)將顯示不同的形狀,如“十十字形光標(biāo)、字形光標(biāo)、“水平雙箭頭光標(biāo)、水平雙箭頭光標(biāo)、”垂直雙箭頭垂直雙箭頭“光標(biāo)、光標(biāo)、”沙露沙露“光標(biāo)光標(biāo)等。等。演示程序演示程序3 3 例題和習(xí)題中涉及的函數(shù)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. 例題和習(xí)題中涉及的函數(shù)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. 例題和習(xí)題中涉及的函數(shù)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論