MFC Tutorial Part 3 – Paint Brush application

This MFC Tutorial part 3 deals with the task of creating a simple paint brush application. This program will draw a line when the left mouse button is clicked and dragged. This will stop drawing the line when the left mouse button is released.
This MFC Tutorial application creates a window by using the class CFrameWnd, and also uses Message map. When the left mouse button is clicked, the application stores the window co-ordinates in the variable called m_StartPoint of type CPoint. When the mouse button is released it keeps the mouse up co-ordinates in the variable m_EndPoint of the same CPoint type.

The CPoint in this MFC Tutorial is a class which has a data member of type struct POINT and some other member functions. This struct POINT has two members which store the x and y co-ordinates of the particular point. This MFC Tutorial Application moves to the m_StartPoint and then draws a line till m_EndPoint. The function CClientDC::MoveTo is used for moving to a particular co-ordinate and CClientDC::LineTo is used for drawing the line.
This leaves us with the discussion about CClientDC. There is a concept in windows programming, called Device Context in windows. This is used in conjunction with the outputs. This device context (DC) can direct the outputs to the screen or to the printer. The root class for device contexts is CDC. The classes like CClientDC, CPaintDC, CMetaDC are all derived from this CDC class. The applications have to write their formatted outputs to the device context and the DC will take care of writing it to the screen or to the printer.

Steps for creating the MFC Tutorial 3 (MFC paint brush application):

Step1:
Create a new project of type Win32 application. In the second screen of the wizard, select the first option. Do not allow the wizard to add any files.?

Step 2:?
After the project is created, click on Menu –>Project –> Add to Project –>New and select a Source File(.cpp) option and give a name?to the file.

Step 3:
Copy and paste the code below into the newly created source file.?

//MFC_Tutorial_3.CPP - MFC Tutorial Part 3 from CoderSource.net

#include <afxwin.h>

class MFC_Tutorial_Window :public CFrameWnd
{
CPoint m_StartPoint, m_EndPoint;
public:
MFC_Tutorial_Window()
{
Create(NULL,"MFC Tutorial Part 2 CoderSource Window");
}
void OnLButtonDown(UINT nFlags, CPoint point);
void OnLButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler
ON_WM_LBUTTONUP() //Macro to map the left button click to the handler
END_MESSAGE_MAP()

void MFC_Tutorial_Window::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CFrameWnd::OnLButtonDown(nFlags, point);
m_StartPoint = point;
}

void MFC_Tutorial_Window::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CFrameWnd::OnLButtonDown(nFlags, point);
m_EndPoint = point;
CClientDC dc(this);
dc.MoveTo(m_StartPoint);
dc.LineTo(m_EndPoint);
}

class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public:
BOOL InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};

MyApp theApp;

//End of MFC_Tutorial_3.CPP

There are some disadvantages in this application.
1. If the application window is resized, maximized, minimized all the lines drawn are gone.
2. Also if we drag the mouse like drawing a curve, it will still draw a straight line.
These problems will be solved by using a device context class called CMetaFileDC.