Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 The mouse and the Keyboard. Getting input from the Mouse.

Similar presentations


Presentation on theme: "Chapter 3 The mouse and the Keyboard. Getting input from the Mouse."— Presentation transcript:

1 Chapter 3 The mouse and the Keyboard

2 Getting input from the Mouse

3 3 Client-Area Mouse Messages Message WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCL K WM_MBUTTONDOW N WM_MBUTTONUP WM_MBUTTONDBLC LK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONDBLC LK WM_MOUSEMOVE Sent When The left mouse button is pressed The left mouse button is released The left mouse button is double-clicked The middle mouse button is pressed The middle mouse button is released The middle mouse button is double-clicked The right mouse button is pressed The right mouse button is released The right mouse button is double-clicked The cursor is moved over the client area

4 Client-Area Mouse Messages Messages for Mouse Events WM_LBUTTOND OWN WM_LBUTTONU P WM_MOUSEMO VE When moving When clicking Left button Clicking and Dragging Double clicking WM_MOUSEMO VE WM_LBUTTOND OWN WM_MOUSEMO VE WM_LBUTTONU P WM_MOUSEMO VE WM_LBUTTOND OWN WM_LBUTTONU P WM_LBUTTOND BLCLK WM_LBUTTONU P

5 5 Client-Area Mouse Messages Message-map macros and Message handlers Message WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCLK WM_MBUTTONDOWN WM_MBUTTONUP WM_MBUTTONDBLCLK WM_RBUTTONDOWN WM_RBUTTONUP WM_RBUTTONDBLCLK WM_MOUSEMOVE Message-Map Macro ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_LBUTTONDBLCLK () ON_WM_MBUTTONDOWN() ON_WM_MBUTTONUP() ON_WM_MBUTTONDBLCL K() ON_WM_RBUTTONDOWN() ON_WM_RBUTTONUP() ON_WM_RBUTTONDBLCL K() ON_WM_MOUSEMOVE() Handling function OnLButtonDown OnLButtonUp OnLButtonDblClk OnMButtonDown OnMButtonUp OnMButtonDblClk OnRButtonDown OnRButtonUp OnRButtonDblClk OnMouseMove

6 How to add the event handler Using “Properties” window of CMainWindow

7 7 Mouse Message Handler Prototype of the Handler function: –nFlags Status of the mouse buttons and the Shift and Ct 기 key at the time when the message was generated afx_msg void On##### (UINT nFlags, CPoint point) ; Bit MaskMeaning MK_CONTROLCtrl key is pressed MK_SHIFTShift key is pressed MK_LBUTTONMouse left button is pressed MK_MBUTTONMouse middle button is pressed MK_RBUTTONMouse right button is pressed

8 8 Mouse Message Handler Prototype of the Handler function: –nFlags afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { if(nFlags & MK_SHIFT){ // if shift key is pressed } if(nFlags & MK_RBUTTON){ // if right button is pressed at the same time } CWnd ::OnLButtonDown(nFlags, point); } void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { if(nFlags & MK_SHIFT){ // if shift key is pressed } if(nFlags & MK_RBUTTON){ // if right button is pressed at the same time } CWnd ::OnLButtonDown(nFlags, point); }

9 9 Mouse Message Handler Prototype of the Handler function: –point Location of the cursor afx_msg void On##### (UINT nFlags, CPoint point) ; void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { CClientDC dc(this); CPoint pt = point; dc.Rectangle(pt.x-100, pt.y+100, pt.x+100, pt.y-100); CWnd ::OnLButtonDown(nFlags, point); } void CChildView::OnLButtonDown(UINT nFlags, CPoint point) { CClientDC dc(this); CPoint pt = point; dc.Rectangle(pt.x-100, pt.y+100, pt.x+100, pt.y-100); CWnd ::OnLButtonDown(nFlags, point); }

10 Coding practice: Line drawing 1 Draw lines by using mouse Key Idea: –Remembering the beginning and ending position 1.When the mouse left button is down Set the position as the beginning point 2.When the mouse left button is released Set the position as the ending point Drawing the line

11 Coding practice: Line drawing 2 Draw Lines by using Mouse Show the lines even when dragging the mouse (rubber band effect) Key Idea: –Remembering the beginning and ending position 1.When the mouse left button is down Set the position as the beginning point 2.When dragging the mouse Set the position as the ending point Drawing the line 3.When the mouse left button is released Set the position as the ending point Drawing the line

12 Any problem? What happen when moving the mouse outside the window’s client area while dragging?

13 13 Capturing the mouse Mouse Capture: –Receiving mouse messages no matter where the mouse goes on the screen while dragging Related function: FunctionMeaning SetCapture()Starting Mouse Capturing ReleaseCapture()Ending mouse Capturing GetCapture()Returns CWnd pointer that owns the capture

14 14 Nonclient-Area Mouse Messages When the mouse is clicked or moved in a window’s nonclient area: MessageSent When WM_NCLBUTTONDOWNThe left mouse button is pressed. WM_NCLBUTTONUPThe left mouse button is released. WM_NCLBUTTONDBLCLKThe left mouse button is double-clicked. WM_NCMBUTTONDOWNThe middle mouse button is pressed. WM_NCMBUTTONUPThe middle mouse button is released. WM_NCMBUTTONDBLCLKThe middle mouse button is double-clicked. WM_NCRBUTTONDOWNThe right mouse button is pressed. WM_NCRBUTTONUPThe right mouse button is released. WM_NCRBUTTONDBLCLKThe right mouse button is double-clicked. WM_NCMOUSEMOVEThe cursor is moved over the window's nonclient area.

15 15 Nonclient-Area Mouse handlers Massage-map Macros and Handlers MessageMessage-Map MacroHandling Function WM_NCLBUTTONDOWNON_WM_NCLBUTTONDOWNOnNcLButtonDown WM_NCLBUTTONUPON_WM_NCLBUTTONUPOnNcLButtonUp WM_NCLBUTTONDBLCLKON_WM_NCLBUTTONDBLCLKOnNcLButtonDblClk WM_NCMBUTTONDOWNON_WM_NCMBUTTONDOWNOnNcMButtonDown WM_NCMBUTTONUPON_WM_NCMBUTTONUPOnNcMButtonUp WM_NCMBUTTONDBLCLKON_WM_NCMBUTTONDBLCLKOnNcMButtonDblClk WM_NCRBUTTONDOWNON_WM_NCRBUTTONDOWNOnNcRButtonDown WM_NCRBUTTONUPON_WM_NCRBUTTONUPOnNcRButtonUp WM_NCRBUTTONDBLCLKON_WM_NCRBUTTONDBLCLKOnNcRButtonDblClk WM_NCMOUSEMOVEON_WM_NCMOUSEMOVEOnNcMouseMove

16 16 Nonclient-Area Mouse handlers Prototype of the handling function –nHitTest A hit test code indicates where the event occurred. –point Location at which the event occurred (in screen coordinate) –Use CWnd::ScreenToClient() function to convert screen coord to client coord. afx_msg void OnNc* (UINT nHitTest, CPoint point) ;

17 17 Nonclient-Area Mouse Handlers nHitTest ValueCorresponding Location HTCAPTIONThe title bar HTCLOSEThe close button HTGROWBOXThe restore button (same as HTSIZE) HTHSCROLLThe window's horizontal scroll bar HTMENUThe menu bar HTREDUCEThe minimize button HTSIZEThe restore button (same as HTGROWBOX) HTSYSMENUThe system menu box HTVSCROLLThe window's vertical scroll bar HTZOOMThe maximize button


Download ppt "Chapter 3 The mouse and the Keyboard. Getting input from the Mouse."

Similar presentations


Ads by Google