第四講:鼠標_第1頁
第四講:鼠標_第2頁
第四講:鼠標_第3頁
第四講:鼠標_第4頁
第四講:鼠標_第5頁
已閱讀5頁,還剩21頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、.第四講:鼠標只要鼠標跨越窗口,或者在某窗口中按下鼠標按鈕,那么窗口過程函數(shù)就會收到鼠標消息,而不管該窗口是否為活動窗口和是否擁有輸入焦點窗口。鼠標消息:WM_LBUTTONDOWNWM_MBUTTONDOWNWM_RBUTTONDOWNWM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLK1. lParam 是相對于客戶區(qū)左上角的坐標,其中X 坐標 :LOWORD(lParam)Y 坐標 :HIWORD(lParam)2. wParam 是 Shift 鍵和 Ctrl 鍵或鼠標按

2、鈕的狀態(tài),若wParam & MK_SHIFT0wParam & MK_CONTROL0wParam & MK_LBUTTON0wParam & MK_MBUTTON0wParam & MK_RBUTTON0WM_MOUSEMOVE表示在產生相應的鼠標消息時,也按下了鼠標雙擊消息Shift 鍵和 Ctrl 鍵或鼠標按鈕。如果希望窗口過程函數(shù)能接受鼠標雙擊消息, 那么在注冊窗口類時, 窗口風格應為: wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;重點:鼠標消息:WM_LBUTTONDOWNWM _MBUTTONDOWNWM_RBUT

3、TONDOWNWM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVE子窗口風格: WS_CHILDWINDOW | WS_VISIBLE取程序實例句柄: (HINSTANCE)GetWindowLong (hwnd, GWL_HINSTANCE);函數(shù) MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ; 移動窗口和改變窗口大小尺寸,產生 WM_SIZE 消息

4、。存取預留在窗口額外字節(jié)的函數(shù):SetWindowLong (hwnd, 0, 0) ;GetWindowLong (hwnd, 0,0);.設置窗口捕獲鼠標函數(shù):SetCapture(hwnd) ;一旦窗口 hwnd被設置了捕獲鼠標,不管鼠標光標是否在窗口 hwnd的邊界之內,窗口 hwnd都接受鼠標輸入。釋放窗口捕獲鼠標函數(shù):ReleaseCapture();WM_MOUSEMOVE 消息:每當鼠標移動時,窗口接收 WM_MOUSEMOVE 消息,系統(tǒng)此時自動把鼠標光標形狀切換到在窗口類中定義的鼠標光標形狀,如wndclass.hCursor = LoadCursor (NULL, IDC

5、_ARROW) ;/*-CONNECT.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 (C

6、onnect) ;HWNDhwnd ;MSGmsg ;WNDCLASSwndclass ;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) ;wndcla

7、ss.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 (Connect-the-Poin

8、ts 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 ;LRESULTCALLBACKWndProc(

9、HWNDhwnd,UINTmessage, WPARAMwParam,LPARAM lParam)static POINT ptMAXPOINTS ;static intiCount ;HDChdc ;inti, j ;PAINTSTRUCTps ;switch (message)case WM_LBUTTONDOWN:iCount = 0 ;InvalidateRect (hwnd, NULL, TRUE) ;return 0 ;case WM_MOUSEMOVE:if (wParam & MK_LBUTTON & iCount 1000)ptiCount.x = LOWORD (lPara

10、m) ;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, NULL, FALSE) ;return 0 ;case WM_PAINT:hdc = BeginPain

11、t (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 (LoadCursor (NULL, IDC_ARROW) ;EndPaint (hwnd, &ps) ;return 0 ;case WM_

12、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 (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINST

13、ANCE hPrevInstance,PSTRszCmdLine, int iCmdShow)static TCHAR szAppName = TEXT (Checker1) ;HWNDhwnd ;MSGmsg ;WNDCLASSwndclass ;wndclass.style= CS_HREDRAW | CS_VREDRAW ;wndclass.lpfnWndProc= WndProc ;wndclass.cbClsExtra= 0 ;wndclass.cbWndExtra= 0 ;wndclass.hInstance= hInstance ;wndclass.hIcon= LoadIcon

14、 (NULL, IDI_APPLICATION) ;wndclass.hCursor= LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ;if (!RegisterClass (&wndclass)MessageBox (NULL, TEXT (Program requires Windows NT!),szAppName,

15、 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, hInstance, NULL) ;ShowWindow (hwnd, iCmdShow) ;UpdateWindow (hwnd) ;while (GetMessage (&msg, NULL, 0, 0)TranslateMes

16、sage (&msg) ;DispatchMessage (&msg) ;.return msg.wParam ;LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage, WPARAMwParam,LPARAM lParam)static BOOL fStateDIVISIONSDIVISIONS ;static intcxBlock, cyBlock ;HDChdc ;intx, y ;PAINTSTRUCT ps ;RECTrect ;switch (message)case WM_SIZE :cxBlock = LOWORD (lParam) / DIVI

17、SIONS ;cyBlock = HIWORD (lParam) / DIVISIONS ;return 0 ;case WM_LBUTTONDOWN :x = LOWORD (lParam) / cxBlock ;y = HIWORD (lParam) / cyBlock ;if (x DIVISIONS & y DIVISIONS)fState xy = 1 ;rect.left= x * cxBlock ;rect.top= y * cyBlock ;rect.right= (x + 1) * cxBlock ;rect.bottom = (y + 1) * cyBlock ;Inval

18、idateRect (hwnd, &rect, FALSE) ;elseMessageBeep (0) ;return 0 ;case WM_PAINT :hdc = BeginPaint (hwnd, &ps) ;for (x = 0 ; x DIVISIONS ; x+).for (y = 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,

19、NULL) ;LineTo(hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;MoveToEx (hdc,x* cxBlock, (y+1) * cyBlock, NULL) ;LineTo(hdc, (x+1) * cxBlock,y* cyBlock) ;EndPaint (hwnd, &ps) ;return 0 ;case WM_DESTROY :PostQuitMessage (0) ;return 0 ;return DefWindowProc (hwnd, message, wParam, lParam) ;/*-CHECKER3.C - Mouse

20、 Hit-Test Demo Program No. 3(c) Charles Petzold, 1998-*/#include #define DIVISIONS 5LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;TCHAR szChildClass = TEXT (Checker3_Child) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevIn

21、stance, PSTR szCmdLine, int iCmdShow)static TCHAR szAppName = TEXT (Checker3) ;HWNDhwnd ;.MSGmsg ;WNDCLASSwndclass ;wndclass.style= CS_HREDRAW | CS_VREDRAW ;wndclass.lpfnWndProc= WndProc ;wndclass.cbClsExtra= 0 ;wndclass.cbWndExtra= 0 ;wndclass.hInstance= hInstance ;wndclass.hIcon= LoadIcon (NULL, I

22、DI_APPLICATION) ;wndclass.hCursor= LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ;if (!RegisterClass (&wndclass)MessageBox (NULL, TEXT (Program requires Windows NT!),szAppName, MB_ICONE

23、RROR) ;return 0 ;wndclass.lpfnWndProc= ChildWndProc ;wndclass.cbWndExtra= sizeof (long) ;wndclass.hIcon= NULL ;wndclass.lpszClassName = szChildClass ;RegisterClass (&wndclass) ;hwnd = CreateWindow (szAppName, TEXT (Checker3 Mouse Hit-Test Demo),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USE

24、DEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, 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 HWND

25、hwndChildDIVISIONSDIVISIONS ;intcxBlock, cyBlock, x, y ;switch (message)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_HINSTANC

26、E),NULL) ;return 0 ;case WM_SIZE :cxBlock = LOWORD (lParam) / 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

27、 0 ;case WM_DESTROY :PostQuitMessage (0) ;return 0 ;return DefWindowProc (hwnd, message, wParam, lParam) ;LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,.WPARAM wParam, LPARAM lParam)HDChdc ;PAINTSTRUCT ps ;RECTrect ;switch (message)case WM_CREATE :SetWindowLong (hwnd, 0, 0) ;/ on/off flagre

28、turn 0 ;case WM_LBUTTONDOWN :SetWindowLong (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)

29、 ;LineTo(hdc, rect.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) ;相關函數(shù)SetPixel.The SetPixel function sets the pixel at the specified coordinates to the specified color.COLORRE

30、F SetPixel(HDChdc,/ handle to DCintX,/ x-coordinate of pixelintY,/ y-coordinate of pixelCOLORREF crColor/ pixel color);Parametershdcin Handle to the device context.Xin Specifies the x-coordinate, in logical units, of the point to be set.Yin Specifies the y-coordinate, in logical units, of the point

31、to be set.crColorin 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 thefunction sets the pixel to. This value maydiffer from the color specified by crColor ; that occu

32、rs when an exact match for the specified color cannot be found.If the function fails, the return value is1.SetCursorThe SetCursor function sets the cursor shape.HCURSOR SetCursor(HCURSOR hCursor/ handle to cursor);ParametershCursorin Handle to the cursor. The cursor must have been created by the Cre

33、ateCursor 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.LoadCursorThe LoadCurs

34、or function loads the specified cursor resource from the executable (.EXE) file associated with an application instance.HCURSOR LoadCursor(HINSTANCE hInstance ,/ handle to application instanceLPCTSTR lpCursorName / name or resource identifier);LoadCursor (NULL, IDC_WAIT)IDC_ARROW ,IDC_CROSS , IDC_W

35、AITMoveToExThe MoveToExfunction updates the current position to the specified point and optionally returns the previous position.BOOL MoveToEx(HDChdc,/ handle to device contextintX,/ x-coordinate of new current positionintY,/ y-coordinate of new current positionLPPOINT lpPoint/ old current position)

36、;Parametershdcin Handle to a device context.Xin Specifies the x-coordinate of the new position, in logical units.Yin Specifies the y-coordinate of the new position, in logical units.lpPointout Pointer to a POINTstructure that receives the previous current position. If this.parameter is a NULL pointe

37、r, 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 specified point.BOOL LineTo(HDChdc,/ device conte

38、xt handleintnXEnd,/ x-coordinate of ending pointintnYEnd / y-coordinate of ending point);Parametershdcin Handle to a device context.nXEndin Specifies the x-coordinate of the lines ending point.nYEndin Specifies the y-coordinate of the lines ending point.Return ValuesIf the function succeeds, the ret

39、urn 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(HDChdc,/ handle to DCintnLeftRect ,/ x-coord of upper-left corner of rectang

40、leintnTopRect,/ y-coord of upper-left corner of rectangleintnRightRect ,/ x-coord of lower-right corner of nBottomRect / y-coord of lower-right corner of rectangle );Parametershdcin Handle to the device context.nLeftRectin Specifies the logical x-coordinate of the upper-left corner of

41、the rectangle.nTopRectin Specifies the logical y-coordinate of the upper-left corner of the rectangle.nRightRectin Specifies the logical x-coordinate of the lower-right corner of the rectangle.nBottomRectin Specifies the logical y-coordinate of the lower-right corner of the rectangle.CreateSolidBrus

42、hThe CreateSolidBrush function creates a logical brush that has the specified solid color.HBRUSH CreateSolidBrush(COLORREF crColor/ brush color value);ParameterscrColorin Specifies the color of the brush. To create a COLORREF color value, use the RGB macro.CreatePenThe CreatePen function creates a l

43、ogical pen that has the specified style, width, and color. The pen can subsequently be selected into a devicecontext and used to draw lines and curves.HPEN CreatePen(intfnPenStyle ,/ pen styleintnWidth,/ pen widthCOLORREF crColor/ pen color);.ParametersfnPenStylein Specifies the pen style. It can be

44、 any one of the following values.ValueMeaningPS_SOLIDThe pen is solid.PS_DASHThe pen is dashed. This style is validonlywhen the penwidth is one or less in device units.PS_DOTThe pen is dotted. This style is validonlywhen the penwidth is one or less in device units.PS_DASHDOTThe pen has alternating d

45、ashes and dots. This style is validonly when the pen width is one or less in device units.PS_DASHDOTDOTThe pen has alternating dashes and double dots. This styleis valid only when the pen width is one or less in deviceunits.PS_NULLThe pen is invisible.PS_INSIDEFRAMEThe pen is solid. When this pen is

46、 used in any GDI drawingfunction that takes a bounding rectangle, the dimensions ofthe figure are shrunk so that it fits entirely in the boundingrectangle, taking into account the width ofthe pen. Thisapplies only to geometric pens.nWidthin Specifies the width of the pen, in logical units. IfnWidth

47、is zero, the pen is a singlepixel wide, regardless of the current transformation.CreatePen returns a pen with the specified width bit with the PS_SOLID style if you specify a width greater than one for thefollowing styles: PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT.crColorin Specifies a color refere

48、nce for the pen color. To generate aCOLORREFstructure,use the RGB macro.Return ValuesIf the function succeeds, the return value is a handle that identifies a logical pen.If the function fails, the return value is NULL.Windows NT/2000: To get extended error information, callGetLastError.RemarksAfter an application creates a logical pen, it can select that pen intoa device context by calling the SelectObject function. After a pen is selected into a device context, it c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論