// FrHndDlg.cpp : implementation file // #include "stdafx.h" #include "MagGlass.h" #include "FrHndDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CFreeHandDlg dialog CFreeHandDlg::CFreeHandDlg(CWnd* pParent /*=NULL*/) : CDialog(CFreeHandDlg::IDD, pParent) { //{{AFX_DATA_INIT(CFreeHandDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_nStart = 0; m_nPoints = 0; m_hFreeHandRgn = NULL; m_pPaintWnd = NULL; memset(m_pPoints, 0, sizeof(m_pPoints)); } CFreeHandDlg::~CFreeHandDlg() { if (m_hFreeHandRgn) { ::DeleteRgn(m_hFreeHandRgn); m_hFreeHandRgn = NULL; } } void CFreeHandDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CFreeHandDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CFreeHandDlg, CDialog) //{{AFX_MSG_MAP(CFreeHandDlg) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_WM_PAINT() ON_BN_CLICKED(IDM_CLEAR, OnClear) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFreeHandDlg message handlers BOOL CFreeHandDlg::OnInitDialog() { CDialog::OnInitDialog(); RECT PaintWndRect; m_pPaintWnd = GetDlgItem(IDC_PAINT); if (!m_pPaintWnd) return FALSE; m_pPaintWnd->GetClientRect(&m_Rect); m_pPaintWnd->GetWindowRect(&PaintWndRect); m_RectPoint.x = PaintWndRect.left; m_RectPoint.y = PaintWndRect.top; ScreenToClient(&m_RectPoint); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CFreeHandDlg::OnLButtonDown(UINT nFlags, CPoint point) { POINT TmpPoint; TmpPoint.x = point.x - m_RectPoint.x; TmpPoint.y = point.y - m_RectPoint.y; if (PtInRect(&m_Rect, TmpPoint) & (m_nStart<1)) { m_PrvPoint.x = TmpPoint.x; m_PrvPoint.y = TmpPoint.y; m_nStart = 1; m_pPoints[m_nPoints].x = TmpPoint.x; m_pPoints[m_nPoints].y = TmpPoint.y; m_nPoints++; } SetCapture(); CDialog::OnLButtonDown(nFlags, point); } void CFreeHandDlg::OnMouseMove(UINT nFlags, CPoint point) { CDC* pDC; POINT TmpPoint; TmpPoint.x = point.x - m_RectPoint.x; TmpPoint.y = point.y - m_RectPoint.y; if (m_nStart == 1) { if (PtInRect(&m_Rect,TmpPoint) && m_nPoints < 1000) { m_pPoints[m_nPoints].x = TmpPoint.x; m_pPoints[m_nPoints].y = TmpPoint.y; m_nPoints++; pDC = m_pPaintWnd->GetDC(); if (pDC) { pDC->MoveTo(m_PrvPoint.x, m_PrvPoint.y); pDC->LineTo(TmpPoint.x, TmpPoint.y); m_pPaintWnd->ReleaseDC(pDC); m_PrvPoint.x = TmpPoint.x; m_PrvPoint.y = TmpPoint.y; } } } CDialog::OnMouseMove(nFlags, point); } void CFreeHandDlg::OnLButtonUp(UINT nFlags, CPoint point) { if (m_nStart == 1) m_nStart = 2; ReleaseCapture(); CDialog::OnLButtonUp(nFlags, point); } void CFreeHandDlg::OnPaint() { CDC* pDC = NULL; pDC = m_pPaintWnd->GetDC(); if (pDC) { if (m_nPoints) { m_pPaintWnd->InvalidateRect(NULL, TRUE); pDC->Polyline(m_pPoints, m_nPoints); } m_pPaintWnd->ReleaseDC(pDC); } } void CFreeHandDlg::OnClear() { memset(m_pPoints, 0, sizeof(m_pPoints)); m_nPoints = 0; m_nStart = 0; m_pPaintWnd->InvalidateRect(NULL, TRUE); m_pPaintWnd->UpdateWindow(); } void CFreeHandDlg::OnOK() { CDialog::OnOK(); if (m_hFreeHandRgn) { ::DeleteRgn (m_hFreeHandRgn); m_hFreeHandRgn = NULL; } m_hFreeHandRgn = CreatePolygonRgn(m_pPoints, m_nPoints, WINDING); if (!m_hFreeHandRgn) { AfxMessageBox("Error While creating region!"); } }