第四讲:鼠标_第1页
第四讲:鼠标_第2页
第四讲:鼠标_第3页
第四讲:鼠标_第4页
第四讲:鼠标_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.第四讲:鼠标只要鼠标跨越窗口,或者在某窗口中按下鼠标按钮,那么窗口过程函数就会收到鼠标消息,而不管该窗口是否为活动窗口和是否拥有输入焦点窗口。鼠标消息:wm_lbuttondownwm_mbuttondownwm_rbuttondownwm_lbuttonupwm_rbuttonupwm_mbuttonupwm_lbuttondblclkwm_mbuttondblclkwm_rbuttondblclk1. lparam 是相对于客户区左上角的坐标,其中x 坐标 :loword(lparam)y 坐标 :hiword(lparam)2. wparam 是 shift 键和 ctrl 键或鼠标按

2、钮的状态,若wparam & mk_shift0wparam & mk_control0wparam & mk_lbutton0wparam & mk_mbutton0wparam & mk_rbutton0wm_mousemove表示在产生相应的鼠标消息时,也按下了鼠标双击消息shift 键和 ctrl 键或鼠标按钮。如果希望窗口过程函数能接受鼠标双击消息, 那么在注册窗口类时, 窗口风格应为: 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);函数 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

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) ;相关函数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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论