310 likes | 619 Views
Introduction to Mouse Commands . Mouse Commands are Windows Messages.Device drivers process the appropriate interrupts, and place the results into the raw input queue.All commands in this queue have the WM_identifiers. . Introduction continued. Win 32 has a dedicated thread monitors these message
E N D
1. Mouse and Keyboard Commands( Mouse )
Chapter Three
CSE791 Advanced Windows Programming.
May 30,2001
by
Robert Gowans
James Amyot
2. Introduction to Mouse Commands Mouse Commands are Windows Messages.
Device drivers process the appropriate interrupts, and place the results into the raw input queue.
All commands in this queue have the WM_identifiers.
3. Introduction continued Win 32 has a dedicated thread monitors these messages and diverts them to the appropriate message thread message queue, where they are then used by the receiving application.
4. Message Handler Model
5. Getting Input from the mouse: Windows uses more then 20 different messages to obtain input from a mouse.
These messages fall into two broad categories. Client and Non Client area mouse messages.
6. Client Area mouse messages: Reports events that happen within the window’s client area. WM_LBUTTONDOWN The left button is pressed
WM_LBUTTONUP The left button is released
WM_LBUTTONBLCLK The left button is doubled clicked
WM_MBUTTONDOWN The middle button is pressed
WM_MBUTTONUP The middle button is released
WM_MBUTTONBLCLK The middle button is doubled clicked
WM_RBUTTONDOWN The right button is pressed
WM_RBUTTONUP The right button is released
WM_RBUTTONBLCLK The right button is doubled clicked
WM_MOUSEMOVE The cursor has moved over the windows client area
7. FYI A developer can use the windows function ::GetSystemMetrics (SM_CMOUSEBUTTONS) to find out how many buttons the mouse has.
WM_xBUTTONUP messages will not be caught if the mouse outside the client area when the button is released
8. Prototyping Client area messages:
afx_msg void OnMsgName (UNIT nFlags, Cpoint point )
point identifies the location of the cursor with respect to the upper left hand coordinates of the client area. This would be the location where the button was pressed or released, or the latest location if the message was WM_MouseMove.
nFlag specifies the state of the mouse buttons and the shift/Ctrl keys at the time of the event.
9. nFlag parameter list:
MK_LBUTTON The left mouse button was pressed.
MK_MBUTTON The middle mouse button was pressed.
MK_RBUTTON The right mouse button was pressed.
MK_CONTROL The control mouse button was pressed.
MK_SHIFT The shift mouse button was pressed.
10. Tic Tac Toe class CMainWindow : public CWnd
{
protected:
static const CRect m_rcSquares[9]; // Grid coordinates
int m_nGameGrid[9]; // Grid contents
int m_nNextChar; // Next character (EX or OH)
int GetRectID (CPoint point);
void DrawBoard (CDC* pDC);
void DrawX (CDC* pDC, int nPos);
void DrawO (CDC* pDC, int nPos);
void ResetGame ();
void CheckForGameOver ();
int IsWinner ();
BOOL IsDraw ();
public:
CMainWindow ();
protected:
virtual void PostNcDestroy ();
afx_msg void OnPaint ();
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk (UINT nFlags, CPoint point);
afx_msg void OnRButtonDown (UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP ()
};
11. #include <afxwin.h>
#include "TicTac.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
//CMYAPP creates a new CMainWindow object in the
//InitInstance function above. This creates the game windows and controls the game /////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CWnd)
ON_WM_PAINT ()
ON_WM_LBUTTONDOWN ()
ON_WM_LBUTTONDBLCLK ()
ON_WM_RBUTTONDOWN ()
END_MESSAGE_MAP ()
const CRect CMainWindow::m_rcSquares[9] = {
CRect ( 16, 16, 112, 112),
CRect (128, 16, 224, 112),
CRect (240, 16, 336, 112),
CRect ( 16, 128, 112, 224),
CRect (128, 128, 224, 224),
CRect (240, 128, 336, 224),
CRect ( 16, 240, 112, 336),
CRect (128, 240, 224, 336),
CRect (240, 240, 336, 336)
};
12. CMainWindow::CMainWindow ()
{
m_nNextChar = EX;
::ZeroMemory (m_nGameGrid, 9 * sizeof (int));
// Register a WNDCLASS.
CString strWndClass = AfxRegisterWndClass (
CS_DBLCLKS, // Class style
AfxGetApp ()->LoadStandardCursor (IDC_ARROW), // Class cursor
(HBRUSH) (COLOR_3DFACE + 1), // Background brush
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO) // Class icon
);
// Create a window.
CreateEx (0, strWndClass, _T ("Tic-Tac-Toe"),
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL);
// Size the window.
CRect rect (0, 0, 352, 352);
CalcWindowRect (&rect);
SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
13. void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
{
// Do nothing if it's O's turn, if the click occurred outside the
// tic-tac-toe grid, or if a nonempty square was clicked.
if (m_nNextChar != EX)
return;
int nPos = GetRectID (point);
if ((nPos == -1) || (m_nGameGrid[nPos] != 0))
return;
// Add an X to the game grid and toggle m_nNextChar.
m_nGameGrid[nPos] = EX;
m_nNextChar = OH;
//
// Draw an X on the screen and see if either player has won.
//
CClientDC dc (this);
DrawX (&dc, nPos);
CheckForGameOver ();
}
14. void CMainWindow::OnLButtonDblClk (UINT nFlags, CPoint point)
{
//
// Reset the game if one of the thick black lines defining the game
// grid is double-clicked with the left mouse button.
//
CClientDC dc (this);
if (dc.GetPixel (point) == RGB (0, 0, 0))
ResetGame ();
}
Messageboxes are used to show the outcome of each Game
Messageboxes are used to display information and to obtain information
Types of messageboxes are MB_ABORTRETYRIGNORE, MB_OK, MB_OKCANCEL, MB_RETYYCANCEL, MB_YESNO, MB_YESNOCANCEL
16. Non-Client Area Mouse Messages When the mouse is clicked inside or moved over a window’s non-client area, Windows send a non-client-area mouse message.
Most applications do not process non-client area messages, they let Windows respond to them instead.
17. Non-Client Area mouse messages: Reports events that happen outside the window’s client area WM_NCLBUTTONDOWN The left button is pressed
WM_NCLBUTTONUP The left button is released
WM_NCLBUTTONBLCLK The left button is doubled clicked
WM_NCMBUTTONDOWN The middle button is pressed
WM_NCMBUTTONUP The middle button is released
WM_NCMBUTTONBLCLK The middle button is doubled clicked
WM_NCRBUTTONDOWN The right button is pressed
WM_NCRBUTTONUP The right button is released
WM_NCRBUTTONBLCLK The right button is doubled clicked
WM_NCMOUSEMOVE The cursor has moved over the windows client area
18. Prototyping Non-Client area messages: afx_msg void OnMsgName
(UNIT nHitTest, Cpoint point )
point identifies the location in the non-client area where the button was pressed or released, or the latest location if the message was WM_MouseMove.
nHitTest specifies the hit-test coded that identifies where the non-client area event occurred.
19. Commonly Used Hit-Test Codes HTCAPTION The Title bar
HTCLOSE The close button
HTGROWBOX The restore button (same as HTSIZE)
HTSCROLL The horizontal scroll bar
HTMENU The menu bar
HTREDUCE The Minimize button
HTSIZE The restore button
HTSYSMENU The system menu box
HTVSCROLL The windows vertical scroll bar
HTZOOM The maximize button
20. WM_NCHITTEST Message Before a window receives a mouse message it receives a WM_NCHITTEST message accompanied by the screen coordinates.
Windows then processes these messages and determines where the event occurred, it then generates the appropriate client or non-client message.
21. WM_MOUSELEAVE & WM_MOUSEHOVER Messages Developer use the WM_MOUSEMOVE messages to determine when cursor moves through the window.
Win98, NT 4.0, and Win2000 have the ::TrackMouseEvent function to determine when a mouse leaves a window or just hovers motionlessly over it.
An application can register to receive WM_MOUSELEAVE and WM_MOUSEHOVER messages.
TrackMouseEvents must be call again after one of the event has been generated.
22. The Mouse Wheel Mouse Wheel allow user to scroll without accessing a scroll bar.
MFC’s CscrollView class provides a default handler for these messages that automatically scroll the window that is in focus.
MFC’s ON_WM_MOUSEWHEEL macro maps WM_MOUSEWHEEL messages to the message handler OnMouseWheel.
If the wheel is clicked instead of rotated the messages generated a typical the same as that of the middle mouse button.
23. Mouse Wheel Prototype bool OnMouseWheel
( UNIT nflag, short zDelta, Cpoint point)
Nflag and point are identical to the ONxButtonDown
zDelta is the distance that the wheel has been rotated
24. Capturing the Mouse
Windows provide a method of capturing the mouse upon receiving the ButtonDown command and continuing no mater where the mouse goes.
The mouse is captured with CWND::SetCapture and released with CWND::ReleaseCapture.
::GetCapture returns a pointer to the window that has the captured mouse.
25. MouseCap Example: class CMainWindow : public CFrameWnd
{
protected:
BOOL m_bTracking; // TRUE if rubber banding
BOOL m_bCaptureEnabled; // TRUE if capture enabled
CPoint m_ptFrom; // "From" point for rubber banding
CPoint m_ptTo; // "To" point for rubber banding
void InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo);
public:
CMainWindow ();
protected:
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
afx_msg void OnLButtonUp (UINT nFlags, CPoint point);
afx_msg void OnMouseMove (UINT nFlags, CPoint point);
afx_msg void OnNcLButtonDown (UINT nHitTest, CPoint point);
DECLARE_MESSAGE_MAP ()
};
26. // CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_LBUTTONDOWN ()
ON_WM_LBUTTONUP ()
ON_WM_MOUSEMOVE ()
ON_WM_NCLBUTTONDOWN ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
m_bTracking = FALSE;
m_bCaptureEnabled = TRUE;
// Register a WNDCLASS.
CString strWndClass = AfxRegisterWndClass ( 0,
AfxGetApp ()->LoadStandardCursor (IDC_CROSS),
(HBRUSH) (COLOR_WINDOW + 1),
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO)
); // Create a window.
Create (strWndClass, _T ("Mouse Capture Demo (Capture Enabled)"));
}
27. void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
{
//
// Record the anchor point and set the tracking flag.
//
m_ptFrom = point;
m_ptTo = point;
m_bTracking = TRUE;
//
// If capture is enabled, capture the mouse.
//
if (m_bCaptureEnabled)
SetCapture ();
}
28. void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
{
//
// If the mouse is moved while we're "tracking" (that is, while a
// line is being rubber-banded), erase the old rubber-band line and
// draw a new one.
//
if (m_bTracking) {
CClientDC dc (this);
InvertLine (&dc, m_ptFrom, m_ptTo);
InvertLine (&dc, m_ptFrom, point);
m_ptTo = point;
}
}
29. void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
{
// If the left mouse button is released while we're tracking, release
// the mouse if it's currently captured, erase the last rubber-band
// line, and draw a thick red line in its place.
if (m_bTracking) {
m_bTracking = FALSE;
if (GetCapture () == this)
::ReleaseCapture ();
CClientDC dc (this);
InvertLine (&dc, m_ptFrom, m_ptTo);
CPen pen (PS_SOLID, 4, RGB (0, 0, 255));
dc.SelectObject (&pen);
dc.MoveTo (m_ptFrom);
dc.LineTo (point);
}
}
30. void CMainWindow::OnNcLButtonDown (UINT nHitTest, CPoint point)
{
//
// When the window's title bar is clicked with the left mouse button,
// toggle the capture flag on or off and update the window title.
//
if (nHitTest == HTCAPTION) {
m_bCaptureEnabled = m_bCaptureEnabled ? FALSE : TRUE;
SetWindowText (m_bCaptureEnabled ?
_T ("Mouse Capture Demo (Capture Enabled)") :
_T ("Mouse Capture Demo (Capture Disabled)"));
}
CFrameWnd::OnNcLButtonDown (nHitTest, point);
}