Title: Programming Windows with MFC
1Programming Windows with MFC
2Chapter 3
- The Mouse and the Keyboard
3Getting Input from the Mouse
- Client-area mouse messages
- Nonclient-area mouse messages
- Event
- The press or release of a mouse button
- The double click of a mouse button
- The movement of the mouse
4Client-Area Mouse Messages
5Number of Mouse Buttons
- Int nButtonCount GetSystemMetrics(SM_CMOUSEBUT
TONS) - 0
- No mouse installed
6Message-Map Macros and Message Handlers for
Client-Area Mouse Messages
7Mouse Message Handlers Prototype
- afx_msg void OnMsgName
- (UINT nFlags, CPoint point)
8The nFlags Parameter
9The TicTac Application
10CWndMessageBox
- int MessageBox
- (LPCTSTR lpszText,
- LPCTSTR lpszCaption NULL,
- UINT nType MB_OK)
- lpszText
- the text in the body of the message box
- lpszCaption
- the caption for the message box's title bar
- nType
- one or more bit flags defining the message box's
style
11Message Box Types
12Nonclient-Area Mouse Messages
13Message-Map Macros and Message Handlers for
Nonclient-Area Mouse Messages
14Message Handlers for Nonclient-area Messages
- afx_msg void OnMsgName
- (UINT nHitTest, CPoint point)
- point.x, point.y
- screen coordination
- nHitTest
- where the event occurred in the windows
nonclient area
15Commonly Used Hit-Test Codes
16The WM_NCHITTEST Message
- Before a window receives a client-area or
nonclient-area mouse message - The cursors screen coordinates
17The WM_MOUSELEAVE and WM_MOUSEOVER Messages
- WM_MOUSELEAVE
- The cursor leaves a window
- WM_MOUSEOVER
- The cursor hovers over a window
18TrackMouseEvent
- To determine when the mouse cursor leaves or
hovers - Accept a pointer parameter
- TRACKMOUSEEVENT structure
- Winuser.h
19The Mouse Wheel
- Message
- WM_MOUSEWHEEL
- Message-Map Macro
- ON_WM_MOUSEWHEEL
- Handling Function
- OnMouseWheel
- BOOL OnMouseWheel (UINT nFlags,
- short zDelta, CPoint point)
- zDelta
- the distance the wheel was rotated
20Capturing the Mouse
- CWndSetCapture
- ReleaseCapture
// In CMainWindow's message map
ON_WM_LBUTTONDOWN () ON_WM_LBUTTONUP () void
CMainWindowOnLButtonDown (UINT nFlags, CPoint
point) SetCapture () void
CMainWindowOnLButtonUp (UINT nFlags, CPoint
point) ReleaseCapture ()
21Mouse Capturing in Action
22Getting Input from the Keyboard
- WM_KEYDOWN
- WM_KEYUP
- WM_CHAR
23The Input Focus
- Keyboard messages
- Directed to the window with the input focus
- No more than one window has the input focus
- WM_SETFOCUS/ WM_KILLFOCUS
24Keystorke Messages
- WM_KEYDOWN
- WM_KEYUP
- WM_SYSKEYDOWN
- WM_SYSKEYUP
- Alt and F10 key
25Keystorke Message Handlers
- afx_msg void OnMsgName
- (UINT nChar, UINT nRepCnt, UINT nflags)
- nChar
- the virtual key code of the key that was pressed
or released. - nRepCnt
- the repeat countthe number of keystrokes
encoded in the message - nFlags
- the key's scan code and zero or more of the bit
flags
26Virtual Key Codes
- Windows identifies keys with the virtual key
codes - Winuser.h
- the letters and the numerals ANSI codes
- ? WM_CHAR messages better way
27Shift States and Toggles
- GetKeyState(VK_SHIFT)
- -returns a negative value if the Shift key
- GetKeyState(VK_CONTROL)
- -return a negative value if the Ctrl key is held
down - Ctrl-Left
- if ((nCharVK_LEFT) GetKeyState(VK_CONTROL)
lt0)) -
28Character Messages
- ON_WM_CHAR ()
-
- void CMainWindowOnChar (UINT nChar, UINT
nRepCnt, UINT nFlags) -
- if ( ((nChargt_T(A))(nCharlt_T(Z)))
- ((nChargt_T(a))(nCharlt_T (z))))
- // Display the character
- else if (nChar VK_RETURN)
- // Process the Enter key
-
-
29The Caret
- The point where the next character will be
inserted - Per-thread resource
30CWnd Caret Handling Functions
31Simple Rules
- Create and destroy
- CreateCaret, CreateSolidCaret, CreateGrayCaret
- DestroyCaret
- Show and hide
- ShowCaret
- HideCaret
- Outside an OnPaint handler
- Should hide the caret to avoid corrupting
- Moving
- SetCaretPos
- GetCaretPos
32The Caret
- void CMainWindowOnSetFocus (CWnd pWnd)
-
- CreateSolidCaret (2, m_cyChar)
- SetCaretPos (m_ptCaretPos)
- ShowCaret ()
-
- void CMainWindowOnKillFocus (CWnd pWnd)
-
- HideCaret ()
- m_ptCaretPos GetCaretPos ()
- DestroyCaret ()
-
33The VisualKB Application
34VisualKB.cpp
- CMainWindowCMainWindow ()
-
- // Load the arrow cursor
- // and the I-beam cursor
- // and save their handles.
- m_hCursorArrow AfxGetApp()-gt
- LoadStandardCursor (IDC_ARROW)
- m_hCursorIBeam AfxGetApp ()-gt
- LoadStandardCursor (IDC_IBEAM)
35VisualKB.cpp
- void CMainWindowOnSetFocus (CWnd pWnd)
-
- // Show the caret when the VisualKB
- // window receives the input focus.
- CreateSolidCaret (max (2, GetSystemMetrics
(SM_CXBORDER)), m_cyChar) - SetCaretPos (m_ptCaretPos)
- ShowCaret ()
-
- void CMainWindowOnKillFocus (CWnd pWnd)
-
- // Hide the caret when the VisualKB
- // window loses the input focus.
- HideCaret ()
- m_ptCaretPos GetCaretPos ()
- DestroyCaret ()
-
36VisualKB.cpp
- void CMainWindowOnKeyDown (UINT nChar, UINT
nRepCnt, UINT nFlags) -
- switch (nChar)
- case VK_LEFT
- m_nTextPos-- PositionCaret ()
- break
- case VK_RIGHT
- m_nTextPos PositionCaret ()
- break
- case VK_HOME
- m_nTextPos 0 PositionCaret ()
- break
- case VK_END
- m_nTextPos m_strInputText.GetLength ()
- PositionCaret ()
- break
-
37VisualKB.cpp
- void CMainWindowOnChar (UINT nChar, UINT
nRepCnt, UINT nFlags) -
- ShowMessage (_T ("WM_CHAR"), nChar, nRepCnt,
nFlags) - CClientDC dc (this)
- switch (nChar)
- case VK_ESCAPE
- case VK_RETURN
- case VK_BACK
- default
- if ((nChar gt 0) (nChar lt 31)) return
- if (m_nTextPos m_strInputText.GetLength ())
- m_strInputText nChar
- m_nTextPos
- else
- m_strInputText.SetAt (m_nTextPos, nChar)
-
-
- // Update the contents of the text box.
- HideCaret ()
38(No Transcript)