// msg1.cpp

/* An MFC program that processes mouse messages and
   outputs through the WM_PAINT message */

#include <afxwin.h>
#include <string.h>
#include "msg1.h"

char str[80] = "Sample Output";
int x = 1, y = 1;  //current window location


// Our CMainWin class definition

// CMainWin's constructor--creates the window
CMainWin::CMainWin()
{
  RECT r;

  r.left = r.top = 10;
  r.right = 500; r.bottom = 150;
  Create(NULL, "Processing mouse and WM_PAINT messages", 
                                     WS_OVERLAPPEDWINDOW, r);
}

// CMainWin's message map
BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
  ON_WM_CHAR()
  ON_WM_PAINT()
  ON_WM_LBUTTONDOWN()
  ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()


// CMainWin's message-processing handler functions--

// WM_CHAR message-processing ftn.
void CMainWin::OnChar(UINT ch, UINT count, UINT flags)
{
  x=y=1;
  wsprintf(str, "%c", ch);
  InvalidateRect(NULL);    // Force a WM_PAINT msg
}

// WM_PAINT message-processing ftn.
void CMainWin::OnPaint()
{
   CPaintDC dc(this);  // get a DC for the invoking window--(CWnd* = this)...
         // constructor performs a BeginPaint(); destructor an EndPaint()
   dc.TextOut(x, y, str, strlen(str));  //redisplay last character

// CDC* pDC;    Do something like this if it's not WM_PAINT... 
// pDC = GetDC();
// pDC->TextOut(x,y,str,lstrlen(str));

// CClientDC dc(this);   Or something like this: 
// dc.TextOut(x, y, str, lstrlen(str));
}

// Left mouse button-processing message ftn.
void CMainWin::OnLButtonDown(UINT flags, CPoint loc)
{
  wsprintf(str, "Left button is down");
  x = loc.x;
  y = loc.y;
  InvalidateRect(NULL);
}

// Right mouse button-processing message ftn.
void CMainWin::OnRButtonDown(UINT flags, CPoint loc)
{
  wsprintf(str, "Right button is down");
  x = loc.x;
  y = loc.y;
  InvalidateRect(NULL);
}

// Our CApp class function definitions
// Initialize the application
BOOL CApp::InitInstance()
{
  m_pMainWnd = new CMainWin;
  m_pMainWnd->ShowWindow(m_nCmdShow);
  m_pMainWnd->UpdateWindow();
  return TRUE;
}


// Instantiate the application
CApp App;