Programming Windows with MFC - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Programming Windows with MFC

Description:

The press or release of a mouse button. The double click ... Creates a caret from a bitmap. CreateCaret. Description. Function. Simple Rules. Create and destroy ... – PowerPoint PPT presentation

Number of Views:233
Avg rating:3.0/5.0
Slides: 39
Provided by: Jos421
Category:

less

Transcript and Presenter's Notes

Title: Programming Windows with MFC


1
Programming Windows with MFC
2
Chapter 3
  • The Mouse and the Keyboard

3
Getting 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

4
Client-Area Mouse Messages
5
Number of Mouse Buttons
  • Int nButtonCount GetSystemMetrics(SM_CMOUSEBUT
    TONS)
  • 0
  • No mouse installed

6
Message-Map Macros and Message Handlers for
Client-Area Mouse Messages
7
Mouse Message Handlers Prototype
  • afx_msg void OnMsgName
  • (UINT nFlags, CPoint point)

8
The nFlags Parameter
9
The TicTac Application
10
CWndMessageBox
  • 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

11
Message Box Types
12
Nonclient-Area Mouse Messages
13
Message-Map Macros and Message Handlers for
Nonclient-Area Mouse Messages
14
Message 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

15
Commonly Used Hit-Test Codes
16
The WM_NCHITTEST Message
  • Before a window receives a client-area or
    nonclient-area mouse message
  • The cursors screen coordinates

17
The WM_MOUSELEAVE and WM_MOUSEOVER Messages
  • WM_MOUSELEAVE
  • The cursor leaves a window
  • WM_MOUSEOVER
  • The cursor hovers over a window

18
TrackMouseEvent
  • To determine when the mouse cursor leaves or
    hovers
  • Accept a pointer parameter
  • TRACKMOUSEEVENT structure
  • Winuser.h

19
The 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

20
Capturing 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 ()
21
Mouse Capturing in Action
22
Getting Input from the Keyboard
  • WM_KEYDOWN
  • WM_KEYUP
  • WM_CHAR

23
The 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

24
Keystorke Messages
  • WM_KEYDOWN
  • WM_KEYUP
  • WM_SYSKEYDOWN
  • WM_SYSKEYUP
  • Alt and F10 key

25
Keystorke 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

26
Virtual Key Codes
  • Windows identifies keys with the virtual key
    codes
  • Winuser.h
  • the letters and the numerals ANSI codes
  • ? WM_CHAR messages better way

27
Shift 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))

28
Character 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

29
The Caret
  • The point where the next character will be
    inserted
  • Per-thread resource

30
CWnd Caret Handling Functions
31
Simple 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

32
The Caret
  • void CMainWindowOnSetFocus (CWnd pWnd)
  • CreateSolidCaret (2, m_cyChar)
  • SetCaretPos (m_ptCaretPos)
  • ShowCaret ()
  • void CMainWindowOnKillFocus (CWnd pWnd)
  • HideCaret ()
  • m_ptCaretPos GetCaretPos ()
  • DestroyCaret ()

33
The VisualKB Application
34
VisualKB.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)

35
VisualKB.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 ()

36
VisualKB.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

37
VisualKB.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)
Write a Comment
User Comments (0)
About PowerShow.com