Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture


Imagine the user has located a position or an item on a document and presses one of the
mouse buttons. While the button is pressed and is down, a button-down message is sent,
depending on the button that was pressed.

If the left mouse button was pressed, an ON_WM_LBUTTONDOWN message is sent.
The syntax of this message is:

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

If the right mouse button was pressed, an ON_WM_RBUTTONDOWN message is sent.
Its syntax is:

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

The first argument, nFlags, specifies what button is down or what keyboard key and what
mouse button are down. It is a constant integer that can have one of the following values:

Value Description
MK_CONTROL A Ctrl key is held down
MK_LBUTTON The left mouse button is down
MK_MBUTTON The middle mouse button is down
MK_RBUTTON The right mouse button is down
MK_SHIFT A Shift key is held down

The point argument specifies the measure from the left and the top borders of the window
to the mouse pointer.

Practical Learning: Sending a Mouse Down Message



  1. To experiment with the mouse down effect, change the program as follows:


#include <afxwin.h>

class CMainFrame : public CFrameWnd
{
public:
CMainFrame ();

protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

DECLARE_MESSAGE_MAP()
};

CMainFrame::CMainFrame()
{
// Create the window's frame
Create(NULL, "Windows Application", WS_OVERLAPPEDWINDOW,
CRect(120, 100, 700, 480), NULL);
}

class CExerciseApp: public CWinApp
{
public:
BOOL InitInstance();
Free download pdf