




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第四讲:鼠标l 只要鼠标跨越窗口,或者在某窗口中按下鼠标按钮,那么窗口过程函数就会收到鼠标消息,而不管该窗口是否为活动窗口和是否拥有输入焦点窗口。l 鼠标消息:1. lParam是相对于客户区左上角的坐标,其中X坐标:LOWORD(lParam)Y坐标:HIWORD(lParam)2. wParam是Shift键和Ctrl键或鼠标按钮的状态,若wParam & MK_SHIFT0wParam & MK_CONTROL0wParam & MK_LBUTTON0wParam & MK_MBUTTON0wParam & MK_RBUTTON0表示在产生相应的鼠标消息时,也按下了Shift键和Ctrl
2、键或鼠标按钮。WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN WM_LBUTTONUPWM_RBUTTONUPWM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVEl 鼠标双击消息如果希望窗口过程函数能接受鼠标双击消息,那么在注册窗口类时,窗口风格应为:wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;l 重点:鼠标消息:WM_LBUTTONDOWNWM _MBUTTONDOWN WM_RBUTTONDO
3、WNWM_LBUTTONUPWM_RBUTTONUP WM_MBUTTONUPWM_LBUTTONDBLCLKWM_MBUTTONDBLCLKWM_RBUTTONDBLCLKWM_MOUSEMOVE子窗口风格:WS_CHILDWINDOW | WS_VISIBLE取程序实例句柄:(HINSTANCE)GetWindowLong (hwnd, GWL_HINSTANCE);函数MoveWindow (hwndChildxy, x * cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE) ;移动窗口和改变窗口大小尺寸,产生WM_SIZE消息。存取预留在窗口额外
4、字节的函数:SetWindowLong (hwnd, 0, 0) ;GetWindowLong (hwnd, 0,0);推荐精选设置窗口捕获鼠标函数:SetCapture(hwnd);一旦窗口hwnd被设置了捕获鼠标,不管鼠标光标是否在窗口hwnd的边界之内,窗口hwnd都接受鼠标输入。释放窗口捕获鼠标函数:ReleaseCapture();WM_MOUSEMOVE消息:每当鼠标移动时,窗口接收WM_MOUSEMOVE消息,系统此时自动把鼠标光标形状切换到在窗口类中定义的鼠标光标形状,如wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;/*- C
5、ONNECT.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) ;
6、HWND 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_ARRO
7、W) ; 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, T
8、EXT (Connect-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.
9、wParam ;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 (wPara
10、m & MK_LBUTTON & 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: Invalida
11、teRect (hwnd, 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) ; Se
12、tCursor (LoadCursor (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 5推荐精选LRESULT CA
13、LLBACK WndProc (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
14、; wndclass.cbClsExtra = 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.lpszCl
15、assName = szAppName ; 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
16、, 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 BOOL fStateDIVISIONSDI
17、VISIONS ; static int 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 ; i
18、f (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 ; InvalidateRect (hwnd, &rect, FALSE) ; else MessageBeep (0) ; return 0 ; case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; for (x = 0 ; x DI
19、VISIONS ; 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, NULL) ; LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ; MoveToEx (hdc, x * cxBlock, (y+1) * cyBlock, NULL) ; Lin
20、eTo (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 Hit-Test Demo Program No. 3 (c) Charles Petzold, 1998 -*/#include #define DIVISIONS 5LRESULT CAL
21、LBACK WndProc (HWND, UINT, 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 ; M
22、SG 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) ; wnd
23、class.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 ; wndclass.lpfnWndProc = ChildWndProc ; wndclass
24、.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_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)
25、 ; 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 hwndChildDIVISIONSDIVISIONS ; int cxBlock
26、, 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_HINSTANCE), NULL) ; return 0 ; case
27、 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 0 ; case WM_
28、DESTROY : PostQuitMessage (0) ; return 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 fl
29、ag return 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 (
30、hdc, 0, 0, NULL) ; 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) ;l 相关函数SetPixelThe SetPixel function sets the pixel at the specified coordinates to t
31、he specified color. COLORREF SetPixel( 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 th
32、e y-coordinate, in logical units, of the 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 di
33、ffer from the color specified by crColor; 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
34、 the cursor. The cursor must have been created 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 previ
35、ous cursor, the return value is NULL. 推荐精选LoadCursorThe 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 i
36、dentifier);LoadCursor (NULL, IDC_WAIT)IDC_ARROW,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, /
37、y-coordinate of new current position LPPOINT 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 struc
38、ture that receives the previous current position. 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 curren
39、t position up to, but not including, the specified 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. nYEn
40、d in Specifies the y-coordinate of the lines ending 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
41、 current brush. BOOL Rectangle( HDC hdc, / handle 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);Para
42、metershdc in Handle to the device context. nLeftRect 推荐精选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 corn
43、er of the rectangle. nBottomRect in Specifies the logical 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);ParameterscrColo
44、r in Specifies the color of the brush. To create a COLORREF 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025网络安全行业劳动合同模板
- 2025采购销售货物合同书
- 租赁园区大棚合同范本
- 2025携手共进合作开发土地合同模板
- 2025风电场EMC合同模板
- 民间抵押汽车合同范本
- 车辆采购合同范本
- 爷孙房屋购买合同范本
- 道闸租赁合同范本
- 定制车库维修合同范本
- 福建省福州市联盟校2023-2024学年高一下学期期末考试英语试题(解析版)
- 2025文化和旅游部直属事业单位招聘社会人员29人模拟试卷附答案详解
- 2024-2025学年重庆市万州区八年级(下)期末语文试卷
- 2025年乒乓球二级裁判考试题及答案
- 血标本采集考试试题附有答案
- 2025年公共安全生产试题及答案
- 员工工资及考勤管理制度
- 浙江省温州市龙湾区2024-2025学年七年级下学期学业水平期末检测数学试题
- 废料出售管理办法
- 企业干部退出管理办法
- 河南选调生管理暂行办法
评论
0/150
提交评论