Visual C++ Programming

Download Report

Transcript Visual C++ Programming

Chapter 3
The mouse and the Keyboard
Getting input from the Mouse
Client-Area Mouse Messages
• Client-Area Mouse Messages
Message
Sent When
WM_LBUTTONDOWN
The left mouse button is pressed
WM_LBUTTONUP
The left mouse button is released
WM_LBUTTONDBLCLK
The left mouse button is double-clicked
WM_MBUTTONDOWN
The middle mouse button is pressed
WM_MBUTTONUP
The middle mouse button is released
WM_MBUTTONDBLCLK
The middle mouse button is double-clicked
WM_RBUTTONDOWN
The right mouse button is pressed
WM_RBUTTONUP
The right mouse button is released
WM_RBUTTONDBLCLK
The right mouse button is double-clicked
WM_MOUSEMOVE
The cursor is moved over the client area
3
Client-Area Mouse Messages
• Messages for Mouse Events
When moving
When clicking
Left button
Clicking and
Dragging
Double clicking
WM_MOUSEMOVE
WM_LBUTTONDOWN
WM_LBUTTONDOWN
WM_LBUTTONDOWN
WM_MOUSEMOVE
WM_LBUTTONUP
WM_MOUSEMOVE
WM_LBUTTONUP
WM_MOUSEMOVE
WM_MOUSEMOVE
WM_LBUTTONDBLCLK
WM_MOUSEMOVE
WM_MOUSEMOVE
WM_LBUTTONUP
WM_MOUSEMOVE
WM_LBUTTONUP
Client-Area Mouse Messages
• Message-map macros and Message handlers
Message
Message-Map Macro
Handling function
WM_LBUTTONDOWN
ON_WM_LBUTTONDOWN()
OnLButtonDown
WM_LBUTTONUP
ON_WM_LBUTTONUP()
OnLButtonUp
WM_LBUTTONDBLCLK
ON_WM_LBUTTONDBLCLK()
OnLButtonDblClk
WM_MBUTTONDOWN
ON_WM_MBUTTONDOWN()
OnMButtonDown
WM_MBUTTONUP
ON_WM_MBUTTONUP()
OnMButtonUp
WM_MBUTTONDBLCLK
ON_WM_MBUTTONDBLCLK()
OnMButtonDblClk
WM_RBUTTONDOWN
ON_WM_RBUTTONDOWN()
OnRButtonDown
WM_RBUTTONUP
ON_WM_RBUTTONUP()
OnRButtonUp
WM_RBUTTONDBLCLK
ON_WM_RBUTTONDBLCLK()
OnRButtonDblClk
WM_MOUSEMOVE
ON_WM_MOUSEMOVE()
OnMouseMove
5
How to add the event handler
• Using “Properties” window of CMainWindow
Mouse Message Handler
• Prototype of the Handler function:
afx_msg void On##### (UINT nFlags, CPoint point) ;
– nFlags
• Status of the mouse buttons and the Shift and Ct기 key at
the time when the message was generated
Bit Mask
Meaning
MK_CONTROL
Ctrl key is pressed
MK_SHIFT
Shift key is pressed
MK_LBUTTON
Mouse left button is pressed
MK_MBUTTON
Mouse middle button is pressed
MK_RBUTTON
Mouse right button is pressed
7
Mouse Message Handler
• Prototype of the Handler function:
afx_msg void On##### (UINT nFlags, CPoint point) ;
– nFlags
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);
8
Mouse Message Handler
• Prototype of the Handler function:
afx_msg void On##### (UINT nFlags, CPoint point) ;
– point
• Location of the cursor
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);
9
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
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
•
•
Set the position as the ending point
Drawing the line
•
•
Set the position as the ending point
Drawing the line
2. When dragging the mouse
3. When the mouse left button is released
Any problem?
• What happen when moving the mouse outside
the window’s client area while dragging?
Capturing the mouse
• Mouse Capture:
– Receiving mouse messages no matter where the
mouse goes on the screen while dragging
• Related function:
Function
Meaning
SetCapture()
Starting Mouse Capturing
ReleaseCapture()
Ending mouse Capturing
GetCapture()
Returns CWnd pointer that owns the capture
13
Nonclient-Area Mouse Messages
• When the mouse is clicked or moved in a
window’s nonclient area:
Message
Sent When
WM_NCLBUTTONDOWN
The left mouse button is pressed.
WM_NCLBUTTONUP
The left mouse button is released.
WM_NCLBUTTONDBLCLK
The left mouse button is double-clicked.
WM_NCMBUTTONDOWN
The middle mouse button is pressed.
WM_NCMBUTTONUP
The middle mouse button is released.
WM_NCMBUTTONDBLCLK
The middle mouse button is double-clicked.
WM_NCRBUTTONDOWN
The right mouse button is pressed.
WM_NCRBUTTONUP
The right mouse button is released.
WM_NCRBUTTONDBLCLK
The right mouse button is double-clicked.
WM_NCMOUSEMOVE
The cursor is moved over the window's nonclient area.
14
Nonclient-Area Mouse handlers
• Massage-map Macros and Handlers
Message
Message-Map Macro
Handling Function
WM_NCLBUTTONDOWN
ON_WM_NCLBUTTONDOWN
OnNcLButtonDown
WM_NCLBUTTONUP
ON_WM_NCLBUTTONUP
OnNcLButtonUp
WM_NCLBUTTONDBLCLK
ON_WM_NCLBUTTONDBLCLK
OnNcLButtonDblClk
WM_NCMBUTTONDOWN
ON_WM_NCMBUTTONDOWN
OnNcMButtonDown
WM_NCMBUTTONUP
ON_WM_NCMBUTTONUP
OnNcMButtonUp
WM_NCMBUTTONDBLCLK
ON_WM_NCMBUTTONDBLCLK
OnNcMButtonDblClk
WM_NCRBUTTONDOWN
ON_WM_NCRBUTTONDOWN
OnNcRButtonDown
WM_NCRBUTTONUP
ON_WM_NCRBUTTONUP
OnNcRButtonUp
WM_NCRBUTTONDBLCLK
ON_WM_NCRBUTTONDBLCLK
OnNcRButtonDblClk
WM_NCMOUSEMOVE
ON_WM_NCMOUSEMOVE
OnNcMouseMove
15
Nonclient-Area Mouse handlers
• Prototype of the handling function
afx_msg void OnNc* (UINT nHitTest, CPoint point) ;
– 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.
16
Nonclient-Area Mouse Handlers
• nHitTest
Value
Corresponding Location
HTCAPTION
The title bar
HTCLOSE
The close button
HTGROWBOX
The restore button (same as HTSIZE)
HTHSCROLL
The window's horizontal scroll bar
HTMENU
The menu bar
HTREDUCE
The minimize button
HTSIZE
The restore button (same as HTGROWBOX)
HTSYSMENU
The system menu box
HTVSCROLL
The window's vertical scroll bar
HTZOOM
The maximize button
17