Mouse - worked example
key : purple - original code / blue - new code
Step 1 :
Create a new project called Mouse using the MFC AppWizard(exe). Select Single Document Application at Step 1 of the Wizard.
Step 2 :
In the CMouseView, header file declare the following :
class
CMouseView : public CView
{
private:
CRect m_rectEllipse;
int m_nColor;
protected: // create from serialization only
CMouseView();
DECLARE_DYNCREATE(CMouseView)
Step 3 :
Use the Class Wizard to insert the OnLButtonDown() funtion (press "CTRL+W" to call the Class Wizard). In the CMouseView class / ID add a function to the WM_LBUTTONDOWN message. Click the add code button to progress to the next step.
Step 4 :
In the view class CMouseView, within the OnLButtonDown() function, insert the following code :
void
CMouseView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_rectEllipse.PtInRect(point)) {
if (m_nColor == GRAY_BRUSH) {m_nColor = WHITE_BRUSH;
}
else {
m_nColor = GRAY_BRUSH;
}
InvalidateRect(m_rectEllipse);
}
}
Step 5 :
In the view class CMouseView, within the constructor, insert the following code :
CMouseView::CMouseView()
: m_rectEllipse(0,
0, 200, 200)
{
m_nColor = GRAY_BRUSH;
}
Step 6 :
In the view class CMouseView, within the OnDraw() function, insert the following code :
void
CMouseView::OnDraw(CDC* pDC)
{
pDC->SelectStockObject(m_nColor);
pDC->Ellipse(m_rectEllipse);
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu