




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、.第四講:鼠標(biāo)l 只要鼠標(biāo)跨越窗口,或者在某窗口中按下鼠標(biāo)按鈕,那么窗口過程函數(shù)就會收到鼠標(biāo)消息,而不管該窗口是否為活動窗口和是否擁有輸入焦點(diǎn)窗口。l 鼠標(biāo)消息:1. lParam是相對于客戶區(qū)左上角的坐標(biāo),其中X坐標(biāo):LOWORD(lParam)Y坐標(biāo):HIWORD(lParam)2. wParam是Shift鍵和Ctrl鍵或鼠標(biāo)按鈕的狀態(tài),若wParam & MK_SHIFT0wParam & MK_CONTROL0wParam & MK_LBUTTON0wParam & MK_MBUTTON0wParam & MK_RBUTTON0表示在產(chǎn)生相應(yīng)的鼠標(biāo)消息時(shí),也按下了Shift鍵和Ctr
2、l鍵或鼠標(biāo)按鈕。WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN WM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVEl 鼠標(biāo)雙擊消息如果希望窗口過程函數(shù)能接受鼠標(biāo)雙擊消息,那么在注冊窗口類時(shí),窗口風(fēng)格應(yīng)為:wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;l 重點(diǎn):鼠標(biāo)消息:WM_LBUTTONDOWNWM _MBUTTONDOWN WM_RBUTTOND
3、OWNWM_LBUTTONUPWM_RBUTTONUP WM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVE子窗口風(fēng)格:WS_CHILDWINDOW | WS_VISIBLE取程序?qū)嵗浔?HINSTANCE)GetWindowLong (hwnd, GWL_HINSTANCE);函數(shù)MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ;移動窗口和改變窗口大小尺寸,產(chǎn)生WM_SIZE消息。存取預(yù)留在窗口額
4、外字節(jié)的函數(shù):SetWindowLong (hwnd, 0, 0) ;GetWindowLong (hwnd, 0,0);設(shè)置窗口捕獲鼠標(biāo)函數(shù):SetCapture(hwnd);一旦窗口hwnd被設(shè)置了捕獲鼠標(biāo),不管鼠標(biāo)光標(biāo)是否在窗口hwnd的邊界之內(nèi),窗口hwnd都接受鼠標(biāo)輸入。釋放窗口捕獲鼠標(biāo)函數(shù):ReleaseCapture();WM_MOUSEMOVE消息:每當(dāng)鼠標(biāo)移動時(shí),窗口接收WM_MOUSEMOVE消息,系統(tǒng)此時(shí)自動把鼠標(biāo)光標(biāo)形狀切換到在窗口類中定義的鼠標(biāo)光標(biāo)形狀,如wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;/*- CONN
5、ECT.C - Connect-the-Dots Mouse Demo Program (c) Charles Petzold, 1998 -*/#include #define MAXPOINTS 1000LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Connect) ; HWN
6、D hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW)
7、; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, TEXT (Con
8、nect-the-Points Mouse Demo), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ;
9、LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static POINT ptMAXPOINTS ; static int iCount ; HDC hdc ; int i, j ; PAINTSTRUCT ps ; switch (message) case WM_LBUTTONDOWN: iCount = 0 ; InvalidateRect (hwnd, NULL, TRUE) ; return 0 ; case WM_MOUSEMOVE: if (wParam & MK_L
10、BUTTON & iCount 1000) ptiCount .x = LOWORD (lParam) ; ptiCount+.y = HIWORD (lParam) ; hdc = GetDC (hwnd) ; /SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0) ; SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), RGB(255,0,0) ; ReleaseDC (hwnd, hdc) ; return 0 ; case WM_LBUTTONUP: InvalidateRect (hwnd
11、, NULL, FALSE) ; return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; SetCursor (LoadCursor (NULL, IDC_WAIT) ; ShowCursor (TRUE) ; for (i = 0 ; i iCount - 1 ; i+) for (j = i + 1 ; j iCount ; j+) MoveToEx (hdc, pti.x, pti.y, NULL) ; LineTo (hdc, ptj.x, ptj.y) ; ShowCursor (FALSE) ; SetCursor (Loa
12、dCursor (NULL, IDC_ARROW) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;/*- CHECKER1.C - Mouse Hit-Test Demo Program No. 1 (c) Charles Petzold, 1998 -*/#include #define DIVISIONS 5LRESULT CALLBACK WndProc (
13、HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Checker1) ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbCls
14、Extra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppN
15、ame ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; hwnd = CreateWindow (szAppName, TEXT (Checker1 Mouse Hit-Test Demo), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstanc
16、e, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ;LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static BOOL fStateDIVISIONSDIVISIONS ; static int
17、 cxBlock, cyBlock ; HDC hdc ; int x, y ; PAINTSTRUCT ps ; RECT rect ; switch (message) case WM_SIZE : cxBlock = LOWORD (lParam) / DIVISIONS ; cyBlock = HIWORD (lParam) / DIVISIONS ; return 0 ; case WM_LBUTTONDOWN : x = LOWORD (lParam) / cxBlock ; y = HIWORD (lParam) / cyBlock ; if (x DIVISIONS & y D
18、IVISIONS) fState xy = 1 ; rect.left = x * cxBlock ; rect.top = y * cyBlock ; rect.right = (x + 1) * cxBlock ; rect.bottom = (y + 1) * cyBlock ; InvalidateRect (hwnd, &rect, FALSE) ; else MessageBeep (0) ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; for (x = 0 ; x DIVISIONS ; x+) for (y
19、 = 0 ; y DIVISIONS ; y+) Rectangle (hdc, x * cxBlock, y * cyBlock, (x + 1) * cxBlock, (y + 1) * cyBlock) ; if (fState xy) MoveToEx (hdc, x * cxBlock, y * cyBlock, NULL) ; LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ; MoveToEx (hdc, x * cxBlock, (y+1) * cyBlock, NULL) ; LineTo (hdc, (x+1) * cxBloc
20、k, y * cyBlock) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;/*- CHECKER3.C - Mouse Hit-Test Demo Program No. 3 (c) Charles Petzold, 1998 -*/#include #define DIVISIONS 5LRESULT CALLBACK WndProc (HWND, UIN
21、T, WPARAM, LPARAM) ;LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;TCHAR szChildClass = TEXT (Checker3_Child) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (Checker3) ; HWND hwnd ; MSG msg ; WNDCLASS wndcla
22、ss ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH
23、) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (Program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0 ; wndclass.lpfnWndProc = ChildWndProc ; wndclass.cbWndExtra = sizeof (long) ;
24、 wndclass.hIcon = NULL ; wndclass.lpszClassName = szChildClass ; RegisterClass (&wndclass) ; hwnd = CreateWindow (szAppName, TEXT (Checker3 Mouse Hit-Test Demo), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow
25、) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (&msg) ; return msg.wParam ;LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static HWND hwndChildDIVISIONSDIVISIONS ; int cxBlock, cyBlock, x, y ; switch (message
26、) case WM_CREATE : for (x = 0 ; x DIVISIONS ; x+) for (y = 0 ; y DIVISIONS ; y+) hwndChildxy = CreateWindow (szChildClass, NULL, WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) (y 8 | x), (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL) ; return 0 ; case WM_SIZE : cxBlock = LOWORD (lPar
27、am) / DIVISIONS ; cyBlock = HIWORD (lParam) / DIVISIONS ; for (x = 0 ; x DIVISIONS ; x+) for (y = 0 ; y DIVISIONS ; y+) MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ; return 0 ; case WM_LBUTTONDOWN : MessageBeep (0) ; return 0 ; case WM_DESTROY : PostQuitMessage (0) ; r
28、eturn 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) case WM_CREATE : SetWindowLong (hwnd, 0, 0) ; / on/off flag return 0 ; case WM_LBUTTONDOWN : S
29、etWindowLong (hwnd, 0, 1 GetWindowLong (hwnd, 0) ; InvalidateRect (hwnd, NULL, FALSE) ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; Rectangle (hdc, 0, 0, rect.right, rect.bottom) ; if (GetWindowLong (hwnd, 0) MoveToEx (hdc, 0, 0, NULL) ; LineTo (hdc, rect.
30、right, rect.bottom) ; MoveToEx (hdc, 0, rect.bottom, NULL) ; LineTo (hdc, rect.right, 0) ; EndPaint (hwnd, &ps) ; return 0 ; return DefWindowProc (hwnd, message, wParam, lParam) ;l 相關(guān)函數(shù)SetPixelThe SetPixel function sets the pixel at the specified coordinates to the specified color. COLORREF SetPixel
31、( HDC hdc, / handle to DC int X, / x-coordinate of pixel int Y, / y-coordinate of pixel COLORREF crColor / pixel color);Parametershdc in Handle to the device context. X in Specifies the x-coordinate, in logical units, of the point to be set. Y in Specifies the y-coordinate, in logical units, of the
32、point to be set. crColor in Specifies the color to be used to paint the point. To create a COLORREF color value, use the RGB macro. Return ValuesIf the function succeeds, the return value is the RGB value that the function sets the pixel to. This value may differ from the color specified by crColor;
33、 that occurs when an exact match for the specified color cannot be found.If the function fails, the return value is 1. SetCursorThe SetCursor function sets the cursor shape. HCURSOR SetCursor( HCURSOR hCursor / handle to cursor);ParametershCursor in Handle to the cursor. The cursor must have been cr
34、eated by the CreateCursor function or loaded by the LoadCursor or LoadImage function. If this parameter is NULL, the cursor is removed from the screen. Return ValuesThe return value is the handle to the previous cursor, if there was one. If there was no previous cursor, the return value is NULL. Loa
35、dCursorThe LoadCursor function loads the specified cursor resource from the executable (.EXE) file associated with an application instance. HCURSOR LoadCursor( HINSTANCE hInstance, / handle to application instance LPCTSTR lpCursorName / name or resource identifier);LoadCursor (NULL, IDC_WAIT)IDC_ARR
36、OW,IDC_CROSS,IDC_WAITMoveToExThe MoveToEx function updates the current position to the specified point and optionally returns the previous position. BOOL MoveToEx( HDC hdc, / handle to device context int X, / x-coordinate of new current position int Y, / y-coordinate of new current position LPPOINT
37、lpPoint / old current position);Parametershdc in Handle to a device context. X in Specifies the x-coordinate of the new position, in logical units. Y in Specifies the y-coordinate of the new position, in logical units. lpPoint out Pointer to a POINT structure that receives the previous current posit
38、ion. If this parameter is a NULL pointer, the previous position is not returned. Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. LineToThe LineTo function draws a line from the current position up to, but not including, the specifie
39、d point. BOOL LineTo( HDC hdc, / device context handle int nXEnd, / x-coordinate of ending point int nYEnd / y-coordinate of ending point);Parametershdc in Handle to a device context. nXEnd in Specifies the x-coordinate of the lines ending point. nYEnd in Specifies the y-coordinate of the lines endi
40、ng point. Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. RectangleThe Rectangle function draws a rectangle. The rectangle is outlined by using the current pen and filled by using the current brush. BOOL Rectangle( HDC hdc, / handle
41、 to DC int nLeftRect, / x-coord of upper-left corner of rectangle int nTopRect, / y-coord of upper-left corner of rectangle int nRightRect, / x-coord of lower-right corner of rectangle int nBottomRect / y-coord of lower-right corner of rectangle);Parametershdc in Handle to the device context. nLeftR
42、ect in Specifies the logical x-coordinate of the upper-left corner of the rectangle. nTopRect in Specifies the logical y-coordinate of the upper-left corner of the rectangle. nRightRect in Specifies the logical x-coordinate of the lower-right corner of the rectangle. nBottomRect in Specifies the log
43、ical y-coordinate of the lower-right corner of the rectangle. CreateSolidBrushThe CreateSolidBrush function creates a logical brush that has the specified solid color. HBRUSH CreateSolidBrush( COLORREF crColor / brush color value);ParameterscrColor in Specifies the color of the brush. To create a CO
44、LORREF color value, use the RGB macro. CreatePenThe CreatePen function creates a logical pen that has the specified style, width, and color. The pen can subsequently be selected into a device context and used to draw lines and curves. HPEN CreatePen( int fnPenStyle, / pen style int nWidth, / pen width COLORREF crColor / pen color);ParametersfnPenStyle in Specifies the pen style. It can be any one of the following values. ValueMeaningPS_SOLIDThe pen is solid.PS_DASHThe pen is dashed. This style is valid only when the pen width is one or less in device units.PS_DOTThe pen is dotted.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 法律合規(guī)培訓(xùn)師考試試卷及答案
- 2025年出版物發(fā)行零售項(xiàng)目合作計(jì)劃書
- 服飾品牌數(shù)字化運(yùn)營師筆試試題及答案
- 2025年陽江市江城區(qū)招聘教師考試筆試試題【答案】
- 邵陽市武岡市事業(yè)單位選調(diào)筆試真題2024
- 2025年環(huán)保節(jié)能型冷卻塔項(xiàng)目發(fā)展計(jì)劃
- 2025年四氟丙醇項(xiàng)目發(fā)展計(jì)劃
- 2025年液體管道運(yùn)輸服務(wù)項(xiàng)目合作計(jì)劃書
- 項(xiàng)目實(shí)施方案(一)
- 小升初英語預(yù)測試題及答案
- 鄉(xiāng)鎮(zhèn)農(nóng)村公墓管理制度
- 2025年1月遼寧省普通高中學(xué)業(yè)水平合格性考試生物試題(原卷版)
- 二年級下二升三數(shù)學(xué)暑假作業(yè)(北師大)
- 2025上海農(nóng)商銀行校園招聘筆試歷年典型考題及考點(diǎn)剖析附帶答案詳解
- 電梯安全風(fēng)險(xiǎn)管控清單
- 高支模木模板的選材與加工
- 體育嘉年華活動方案
- 鐵路路基路基標(biāo)準(zhǔn)橫斷面13課件
- 遼寧省勞動合同模板
- 預(yù)算編制的目標(biāo)與指標(biāo)
- 2025年中國寫字樓市場深度分析與投資發(fā)展前景趨勢研究報(bào)告
評論
0/150
提交評論