MFC Bitmaps Example

#include <afxwin.h>
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
afx_msg void OnLButtonDown( UINT, CPoint );
afx_msg void OnLButtonUp( UINT, CPoint );
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
int x,y;
int sx,sy;
CDC memdc;//declared memory device context
int maxX,maxY;//used to hold the size of the screen
CBitmap mbitmap;//creates bitmap object
CBrush mbrush;//creates cbrush object
};


BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}

CMainFrame::CMainFrame()
{
Create(NULL, "Bitmaps example");
sx=0;
sy=0;
//get size of screen
maxX=GetSystemMetrics(SM_CXSCREEN);
maxY=GetSystemMetrics(SM_CYSCREEN);
CClientDC dc(this);//get current device context
memdc.CreateCompatibleDC(&dc);//create memory device context
mbitmap.CreateCompatibleBitmap(&dc,maxX,maxY);//create bitmap compatible with screen
memdc.SelectObject(&mbitmap);//select screen bitmap into virtual screen device context
mbrush.CreateSolidBrush(RGB(255,255,255));//create brush for virtual screen background
memdc.SelectObject(&mbrush);//select brush into virtual screen device content
memdc.PatBlt(0,0,maxX,maxY,PATCOPY);//copy bitpattern on device using current brush
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;


afx_msg void CMainFrame::OnLButtonDown(UINT flags,CPoint pt)
{
x=pt.x;
y=pt.y;
}

//draw line to virtual device context and invalidates windows sending WM_PAINT message
afx_msg void CMainFrame::OnLButtonUp(UINT flags,CPoint pt)
{
sx=pt.x;
sy=pt.y;
memdc.LineTo(pt.x,pt.y);
memdc.MoveTo(pt.x,pt.y);
Invalidate();
}

//dope virtual screen to physical screen
afx_msg void CMainFrame::OnPaint()
{
CPaintDC dc(this);
dc.BitBlt(0,0,maxX,maxY,&memdc,0,0,SRCCOPY);
}