//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LocatorWnd.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LocatorWnd.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LocatorWnd.h" #include "..\LGLib\Comm\Lgsm.h" #include "..\LgLib\Version.h" #define LOCATOR_REGION_D2P 100.0 CLocatorRegion::CLocatorRegion() { // clear region handle m_hRgn = NULL; // set default color m_nColor = RGB(0,0,0); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::CLocatorRegion" ); } CLocatorRegion::CLocatorRegion( const CLocatorRegion& region ) : CLgArray( (CLgArray&)region ) { // clear region handle m_hRgn = NULL; // copy color data m_nColor = region.m_nColor; // update region handle UpdateRgn(); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::CLocatorRegion( const CLocatorRegion& )" ); } CLocatorRegion::~CLocatorRegion() { RemoveAll(); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::~CLocatorRegion" ); } BOOL CLocatorRegion::Add( CLgDPoint& data ) { BOOL bResult = CLgArray::Add( data ); UpdateRgn(); return bResult; } BOOL CLocatorRegion::Remove( INT nIndex ) { BOOL bResult = CLgArray::Remove( nIndex ); UpdateRgn(); return bResult; } BOOL CLocatorRegion::RemoveAll() { BOOL bResult = CLgArray::RemoveAll(); UpdateRgn(); return bResult; } BOOL CLocatorRegion::Load( HANDLE hFile ) { RemoveAll(); DWORD nPointCount; // read point count DWORD dwRead = 0; if( !::ReadFile( hFile, &nPointCount, sizeof(nPointCount), &dwRead, NULL ) || sizeof(nPointCount) != dwRead ) { ::CloseHandle( hFile ); return FALSE; } // read points for( DWORD j = 0; j < nPointCount; j++ ) { CLgDPoint point; // read point from file if( !::ReadFile( hFile, &point, sizeof(point), &dwRead, NULL ) || sizeof(point) != dwRead ) { ::CloseHandle( hFile ); return FALSE; } // add into array if( !Add( point ) ) { ::CloseHandle( hFile ); return FALSE; } } // write color information if( !::ReadFile( hFile, &m_nColor, sizeof(m_nColor), &dwRead, NULL ) || sizeof(m_nColor) != dwRead ) { ::CloseHandle( hFile ); return FALSE; } return TRUE; } BOOL CLocatorRegion::Store( HANDLE hFile ) { INT nPointCount = GetCount(); // write point count DWORD dwWritten = 0; if( !::WriteFile( hFile, &nPointCount, sizeof(nPointCount), &dwWritten, NULL ) || sizeof(nPointCount) != dwWritten ) { ::CloseHandle( hFile ); return FALSE; } // write points for( DWORD i = 0; i < nPointCount; i++ ) { CLgDPoint &point = GetAt(i); if( !::WriteFile( hFile, &point, sizeof(point), &dwWritten, NULL ) || sizeof(point) != dwWritten ) { ::CloseHandle( hFile ); return TRUE; } } // write color information if( !::WriteFile( hFile, &m_nColor, sizeof(m_nColor), &dwWritten, NULL ) || sizeof(m_nColor) != dwWritten ) { ::CloseHandle( hFile ); return FALSE; } return TRUE; } BOOL CLocatorRegion::PtInRegion( CLgDPoint &dp ) { if( !m_hRgn ) return FALSE; return ::PtInRegion( m_hRgn, LOCATOR_REGION_D2P * dp.x, LOCATOR_REGION_D2P * dp.y ); } BOOL CLocatorRegion::UpdateRgn() { // delet old region if( m_hRgn ) { ::DeleteObject( m_hRgn ); m_hRgn = NULL; } // check amount of points if( !GetCount() ) return TRUE; // allocate storage for new point LPPOINT pPoint = new POINT[ GetCount() ]; if( !pPoint ) return FALSE; for( INT i = 0; i < GetCount(); i++ ) { pPoint[i].x = LOCATOR_REGION_D2P * GetAt( i ).x; pPoint[i].y = LOCATOR_REGION_D2P * GetAt( i ).y; } m_hRgn = ::CreatePolygonRgn( pPoint, GetCount(), ALTERNATE ); delete pPoint; return TRUE; } ///////////////////////////////////////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////////////////////////////// INT CLocatorRegionArray::PtInRegion( CLgDPoint &dp ) { for( INT i = 0; i < GetCount(); i++ ) if( GetAt(i).PtInRegion( dp ) ) return i; return INVALID_INDEX; } BOOL CLocatorRegionArray::Load( LPCTSTR pFile ) { RemoveAll(); // open file HANDLE hFile = ::CreateFile( pFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; DWORD nRegionCount; // read regions count DWORD dwRead = 0; if( !::ReadFile( hFile, &nRegionCount, sizeof(nRegionCount), &dwRead, NULL ) || sizeof(nRegionCount) != dwRead ) { ::CloseHandle( hFile ); return FALSE; } // store regions for( DWORD i = 0; i < nRegionCount; i++ ) { CLocatorRegion region; // read points if( !region.Load( hFile ) ) { ::CloseHandle( hFile ); return FALSE; } // add into regions array if( !Add( region ) ) { ::CloseHandle( hFile ); return TRUE; } } ::CloseHandle( hFile ); return TRUE; } BOOL CLocatorRegionArray::Store( LPCTSTR pFile ) { // open file HANDLE hFile = ::CreateFile( pFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL ); if( hFile == INVALID_HANDLE_VALUE ) return FALSE; DWORD nRegionCount = GetCount(); // write regions count DWORD dwWritten = 0; if( !::WriteFile( hFile, &nRegionCount, sizeof(nRegionCount), &dwWritten, NULL ) || sizeof(nRegionCount) != dwWritten ) { ::CloseHandle( hFile ); return FALSE; } // store regions for( DWORD i = 0; i < nRegionCount; i++ ) { CLocatorRegion ®ion = GetAt(i); // write points if( !region.Store( hFile ) ) { ::CloseHandle( hFile ); return FALSE; } } ::CloseHandle( hFile ); return TRUE; } int CLocatorRegionArray::DBL_CMP(const double& a, const double& b, const double quality ) { if(fabs(a - b) <= fabs(quality)) return 0; if(a > b) return 1; return -1; } BOOL CLocatorRegionArray::CrossProduct( LPVERTEX ln0, LPVERTEX ln1, LPVERTEX pCross, BOOL fCheck0, BOOL fCheck1) { double A0, A1, B0, B1, X, Y; if(DBL_CMP(ln0[0].x, ln0[1].x) == 0) { A0 = DBL_MAX; B0 = ln0[0].x; } else { A0 = (ln0[0].y - ln0[1].y) / (ln0[0].x - ln0[1].x); B0 = ln0[0].y - A0 * ln0[0].x; } if(DBL_CMP(ln1[0].x , ln1[1].x) == 0) { A1 = DBL_MAX; B1 = ln1[0].x; } else { A1 = (ln1[0].y - ln1[1].y) / (ln1[0].x - ln1[1].x); B1 = ln1[0].y - A1 * ln1[0].x; } if(A0 == DBL_MAX) { if(A1 == DBL_MAX) return FALSE; X = B0; Y = A1 * X + B1; } else { if(A1 == DBL_MAX) X = B1; else { if(DBL_CMP( A1, A0 ) != 0) X = (B0 - B1) / (A1 - A0); else return FALSE; } Y = A0 * X + B0; } double y = Y, x = X; if(fCheck0) { double x0 = ln0[0].x, x1 = ln0[1].x; double y0 = ln0[0].y, y1 = ln0[1].y; if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1)) return FALSE; if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1)) return FALSE; if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1)) return FALSE; if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1)) return FALSE; } if(fCheck1) { double x0 = ln1[0].x, x1 = ln1[1].x; double y0 = ln1[0].y, y1 = ln1[1].y; if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1)) return FALSE; if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1)) return FALSE; if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1)) return FALSE; if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1)) return FALSE; } pCross->x = X; pCross->y = Y; return TRUE; } BOOL CLocatorRegionArray::PtInLine(LPVERTEX ln, VERTEX& p) { double A, B; if(DBL_CMP(ln[0].x, ln[1].x) == 0) { A = DBL_MAX; B = ln[0].x; } else { A = (ln[0].y - ln[1].y) / (ln[0].x - ln[1].x); B = ln[0].y - A * ln[0].x; } if(A == DBL_MAX) { if( DBL_CMP(p.x, B) == 0) return TRUE; } else { if(DBL_CMP(p.x * A + B, p.y) == 0) return TRUE; } return FALSE; } int CLocatorRegionArray::comparePoints(const void *arg1, const void *arg2) { if(((LPVERTEX)arg1)->r > ((LPVERTEX)arg2)->r) return +1; if(((LPVERTEX)arg1)->r < ((LPVERTEX)arg2)->r) return -1; return 0; } void CLocatorRegionArray::AddPointToSegment(LPSEGMENTS pSegments, INT nIndex, LPVERTEX pPoint) { LPVERTEX pNew = new VERTEX[pSegments[nIndex].nCount + 1]; ::CopyMemory(pNew, pSegments[nIndex].pPoints, sizeof(VERTEX) * pSegments[nIndex].nCount); ::CopyMemory(pNew + pSegments[nIndex].nCount, pPoint, sizeof(VERTEX)); if(pSegments[nIndex].pPoints) delete pSegments[nIndex].pPoints; pSegments[nIndex].pPoints = pNew; pSegments[nIndex].nCount++; } void CLocatorRegionArray::StickSegments(LPSEGMENTS pSegments, INT nIndex) { if(pSegments[nIndex].nCount) { qsort(pSegments[nIndex].pPoints, pSegments[nIndex].nCount, sizeof(VERTEX), comparePoints); LPVERTEX pTemp = new VERTEX[pSegments[nIndex].nCount]; LPVERTEX pPoints = pSegments[nIndex].pPoints; int f, nT = 0; pTemp[nT++] = pPoints[0]; f = pPoints[0].f; for(int i = 1; i < pSegments[nIndex].nCount;) { f += pPoints[i].f; if(f == 0) { pTemp[nT++] = pPoints[i++]; if(i < pSegments[nIndex].nCount) { pTemp[nT++] = pPoints[i]; f = pPoints[i++].f; } } else i++; } if(pSegments[nIndex].pPoints) delete[] pSegments[nIndex].pPoints; pSegments[nIndex].pPoints = new VERTEX[nT]; ::CopyMemory(pSegments[nIndex].pPoints, pTemp, sizeof(VERTEX) * nT); pSegments[nIndex].nCount = nT; if(pTemp) delete[] pTemp; } } int CLocatorRegionArray::compareSegments(const void *arg1, const void *arg2) { if(((LPSEGMENT)arg1)->nIndex > ((LPSEGMENT)arg2)->nIndex) return +1; if(((LPSEGMENT)arg1)->nIndex < ((LPSEGMENT)arg2)->nIndex) return -1; if(((LPSEGMENT)arg1)->pPoints[0] > ((LPSEGMENT)arg2)->pPoints[0]) return +1; if(((LPSEGMENT)arg1)->pPoints[0] < ((LPSEGMENT)arg2)->pPoints[0]) return -1; return 0; } BOOL CLocatorRegionArray::ProduceHiddenZones() { const int MAX_RAY_COUNT = 360; const double TWO_PI = 6.2831853071796; // 2 * PI, 18/04/2001 VERTEX pLines[MAX_RAY_COUNT]; SEGMENTS pSegments[MAX_RAY_COUNT >> 1]; double double_range = 2.0; int ray_count = MAX_RAY_COUNT; for(int idx = 0; idx < (ray_count >> 1); idx++) { double a = TWO_PI * idx / ray_count; pLines[(idx << 1) + 0 ].x = cos(a) * double_range; pLines[(idx << 1) + 0 ].y = -sin(a) * double_range; pLines[(idx << 1) + 1 ].x = -cos(a) * double_range; pLines[(idx << 1) + 1 ].y = sin(a) * double_range; pSegments[idx].nCount = 0; pSegments[idx].pPoints = NULL; } for(int i = 0; i < GetCount(); i++) { CLocatorRegion& region = GetAt(i); if(region.GetCount() > 2 && region.GetColor() == 0) { LPVERTEX pPoints = new VERTEX[region.GetCount() + 1]; for(int j = 0; j < region.GetCount(); j++) { pPoints[j].x = ((double)region[j].x); pPoints[j].y = ((double)region[j].y); } pPoints[region.GetCount()].x = (double)region[0].x; pPoints[region.GetCount()].y = (double)region[0].y; for(j = 0; j <= region.GetCount(); j++) for(int v = 0; v < (ray_count >> 1); v++) if(PtInLine(pLines + (v << 1), pPoints[j] ) ) { for(int g = -1; g < 2; g += 2) for(int f = -1; f < 2; f += 2) { CLgDPoint p( pPoints[j].x + f, pPoints[j].y + g ); if(!region.PtInRegion(p)) { pPoints[j].x += (double)f * 1e-5; pPoints[j].y += (double)g * 1e-5; goto NEXT; } } NEXT:; } for(j = 0; j < (ray_count >> 1); j ++ ) { INT nCCount = 0; VERTEX pCPoints[MAX_RAY_COUNT * 10]; for(int m = 0; m < region.GetCount(); m++) { VERTEX cp; if(CrossProduct(pPoints + m, pLines + (j << 1), &cp)) { cp.n = m; cp.r = sqrt(pow(cp.x - pLines[j << 1].x, 2) + pow(cp.y - pLines[j << 1].y, 2)); if(nCCount > 0 && DBL_CMP(pCPoints[nCCount-1].x, cp.x) == 0 && DBL_CMP(pCPoints[nCCount-1].y, cp.y) == 0) { int i1 = (m + 1) % region.GetCount(); int i2 = (m - 1 + region.GetCount()) % region.GetCount(); VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y }, { pPoints[i2].x, pPoints[i2].y } }; LPVERTEX l0 = pLines + (j << 1); if(!CrossProduct( pLines + (j << 1), line, &cp, FALSE)) pCPoints[nCCount++] = cp; } else pCPoints[nCCount++] = cp; } } // end of for(int m = 0; m < pPolygon->GetCount(); m++) if(nCCount > 0 && DBL_CMP( pCPoints[nCCount-1].x, pCPoints[0].x) == 0 && DBL_CMP( pCPoints[nCCount-1].y, pCPoints[0].y) == 0) { int i1 = (pCPoints[0].n + 1) % region.GetCount(); int i2 = (pCPoints[0].n - 1 + region.GetCount()) % region.GetCount(); VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y }, { pPoints[i2].x, pPoints[i2].y } }; LPVERTEX l0 = pLines + (j << 1); VERTEX cp; if(CrossProduct( pLines + (j << 1), line, &cp, FALSE)) nCCount--; } if(nCCount % 2 == 0) { qsort(pCPoints, nCCount, sizeof(VERTEX), comparePoints); for(m = 0; m < nCCount; m++) { pCPoints[m].f = ( m % 2 ) ? -1 : +1; AddPointToSegment( pSegments, j, pCPoints + m ); } } } // end of for(j = 0; j < (ray_count >> 1); j ++ ) delete[] pPoints; // Added delete[] 22/04/2001 } // end of if(pPolygon->GetCount() > 2 && pPolygon->GetType() == 0) } // end of for(int i = 0; i < m_nCount; i++) INT sc = 0; for(INT j = 0; j < (ray_count >> 1); j ++) { StickSegments( pSegments, j ); sc += pSegments[j].nCount; } LPSEGMENT pSegment = new SEGMENT[sc]; ::FillMemory(pSegment, sizeof(SEGMENT)*sc, 0xFF); INT cn = 0; for(j = 0; j < (ray_count >> 1); j ++) { LPVERTEX pPoints = pSegments[j].pPoints; if(pSegments[j].nCount % 2) { // CMessageBox::MessageBox(NULL, "Produced odd number of points", NULL, MB_OK); } else { // convert zero shift to radians for(int m = 0; m < pSegments[j].nCount; m += 2) { pPoints[m].r -= double_range;//*scale; pPoints[m+1].r -= double_range;//*scale; if(pPoints[m].r < 0.0 && pPoints[m+1].r > 0.0) { pSegment[cn].nIndex = j; pSegment[cn].pPoints[0] = 0; pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r); pSegment[cn].nIndex = j + (ray_count >> 1); pSegment[cn].pPoints[0] = 0; pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r); } else { if(pPoints[m].r < 0 || pPoints[m+1].r < 0) { if(pPoints[m].r > pPoints[m+1].r) { pSegment[cn].nIndex = j; pSegment[cn].pPoints[0] = (INT)(-pPoints[m].r); pSegment[cn++].pPoints[1] = (INT)(-pPoints[m + 1].r); } else { pSegment[cn].nIndex = j; pSegment[cn].pPoints[0] = (INT)(-pPoints[m + 1].r); pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r); } } else if(pPoints[m].r < pPoints[m+1].r) { pSegment[cn].nIndex = j + (ray_count >> 1); pSegment[cn].pPoints[0] = (INT)(pPoints[m].r); pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r); } else { pSegment[cn].nIndex = j + (ray_count >> 1); pSegment[cn].pPoints[0] = (INT)(pPoints[m + 1].r); pSegment[cn++].pPoints[1] = (INT)(pPoints[m].r); } } // end of else } // end of for(int m = 0; m < pSegments[j].nCount; m += 2) } // end of else delete [] pPoints; pSegments[j].pPoints = NULL; } // end of for(j = 0; j < (ray_count >> 1); j ++) qsort(pSegment, sc, sizeof(SEGMENT), compareSegments); FILE *file = fopen("Hidden.txt", "wt"); if( file ) { for(j = 0; j < sc; j++) if(pSegment[j].nIndex != 0xFFFFFFFF) { INT nMin = pSegment[j].pPoints[0]; INT nMax = pSegment[j].pPoints[1]; if( nMin <= LGS_MAX_DETECTOR_RANGE ) { if( nMax > LGS_MAX_DETECTOR_RANGE ) nMax = LGS_MAX_DETECTOR_RANGE; fprintf(file, "%d %d %d\n", pSegment[j].nIndex, nMin, nMax); } // fprintf(file, "%d %d %d\n", pSegment[j].nIndex, pSegment[j].pPoints[0], pSegment[j].pPoints[1]); } fclose(file); } delete[] pSegment; return TRUE; } ///////////////////////////////////////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////////////////////////////// CLocatorWnd::CLocatorWnd() { m_bEditMode = FALSE; m_eEditMode = EDIT_MODE_POINT; m_bEditStart = FALSE; m_hMemDC = NULL; m_hMemBitmap = NULL; m_hBackgroundDC = NULL; m_hBackgroundBitmap = NULL; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLocatorWnd::CLocatorWnd" ); } CLocatorWnd::~CLocatorWnd() { DestroyWindow(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLocatorWnd::~CLocatorWnd" ); } BOOL CLocatorWnd::Create( HWND hParentWnd, INT x, INT y, INT cx, INT cy ) { HINSTANCE hInstance = (HINSTANCE)GetWindowLong( GWL_HINSTANCE ); LPCTSTR pClassName = _T("LaserGuardLocatorWindowClass"); // register window class if( !GetClassInfoEx( hInstance, pClassName, NULL ) ) { if( !RegisterClass( pClassName, hInstance ) ) return FALSE; } // create memory DC if( !m_hMemDC ) { HDC hDC = ::GetDC( ::GetDesktopWindow() ); if( hDC ) { m_hMemDC = ::CreateCompatibleDC( hDC ); ::ReleaseDC( ::GetDesktopWindow(), hDC ); } } // create back DC if( !m_hBackgroundDC ) { HDC hDC = ::GetDC( ::GetDesktopWindow() ); if( hDC ) { m_hBackgroundDC = ::CreateCompatibleDC( hDC ); ::ReleaseDC( ::GetDesktopWindow(), hDC ); } } // create window if( !CGenericWnd::Create( pClassName, NULL, WS_CHILD | WS_VISIBLE, x, y, cx, cy, hParentWnd, NULL, hInstance ) ) return FALSE; // show window ShowWindow( SW_SHOW ); UpdateWindow(); return TRUE; } BOOL CLocatorWnd::ClearRegions() { // remove region from array m_RegionArray.RemoveAll(); // redraw editor InvalidateRect( NULL, FALSE ); return TRUE; } void CLocatorWnd::SetBackground( HBITMAP hBitmap, BOOL bCopy ) { // delete previus bitmap if( m_hBackgroundBitmap ) { ::DeleteObject( m_hBackgroundBitmap ); m_hBackgroundBitmap = NULL; } // take copy or original image m_hBackgroundBitmap = bCopy ? (HBITMAP)::CopyImage( hBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ) : hBitmap; // redraw according to new background InvalidateRect( NULL, FALSE ); } void CLocatorWnd::OnPaint( HDC hDC ) { HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hMemDC, m_hMemBitmap ); // perfrom drawing OnDraw( m_hMemDC ); // get client rectangle RECT rect; GetClientRect( &rect ); // copy from memory to dc ::BitBlt( hDC, 0, 0, rect.right, rect.bottom, m_hMemDC, 0, 0, SRCCOPY ); // deselect bitmap in the context ::SelectObject( m_hMemDC, hBitmap ); } void CLocatorWnd::OnDraw( HDC hDC ) { // get client rectangle RECT rect; GetClientRect( &rect ); // draw background if( m_hBackgroundBitmap ) { DIBSECTION DibSection; if( GetObject( m_hBackgroundBitmap, sizeof(DibSection), &DibSection ) ) { HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hBackgroundDC, m_hBackgroundBitmap ); // copy from memory to dc ::StretchBlt( hDC, 0, 0, rect.right, rect.bottom, m_hBackgroundDC, 0, 0, DibSection.dsBm.bmWidth, DibSection.dsBm.bmHeight, SRCCOPY ); ::SelectObject( m_hBackgroundDC, hBitmap ); } } else { ::Ellipse( hDC, rect.left, rect.top, rect.right, rect.bottom ); } // draw regions for( INT i = 0; i < m_RegionArray.GetCount(); i++ ) { CLocatorRegion& region = m_RegionArray[i]; // draw edit points LPPOINT pPoint = (LPPOINT)malloc( sizeof(POINT)* region.GetCount() ); if( !pPoint ) return; // fill point array for( INT j = 0; j < region.GetCount(); j++ ) { CLgDPoint& dp = region[j]; pPoint[j] = LocatorToClient( dp ); } // create pen & brush HPEN hPen = ::CreatePen( PS_SOLID, 1, region.GetColor() ); HPEN hPenOld = (HPEN)::SelectObject( hDC, hPen ); HBRUSH hBrush = ::CreateHatchBrush( HS_DIAGCROSS, region.GetColor() ); HBRUSH hBrushOld = (HBRUSH)::SelectObject( hDC, hBrush ); // change fill mode to transparent INT nBkMode = ::SetBkMode( hDC, TRANSPARENT ); // draw polygon Polygon( hDC, pPoint, region.GetCount() ); // change fill mode back ::SetBkMode( hDC, nBkMode ); // delete pen & brush ::SelectObject( hDC, hBrushOld ); ::DeleteObject( hBrush ); ::SelectObject( hDC, hPenOld ); ::DeleteObject( hPen ); // delete point array free( pPoint ); } // draw editing region if( m_bEditMode ) { // draw edit points LPPOINT pPoint = (LPPOINT)malloc( sizeof(POINT)* m_EditRegion.GetCount() ); if( !pPoint ) return; // fill point array for( INT j = 0; j < m_EditRegion.GetCount(); j++ ) { CLgDPoint& dp = m_EditRegion[j]; pPoint[j] = LocatorToClient( dp ); } // create pen HPEN hPen = ::CreatePen( PS_SOLID, 3, m_EditRegion.GetColor() ); HPEN hPenOld = (HPEN)::SelectObject( hDC, hPen ); // draw polygon ::Polyline( hDC, pPoint, m_EditRegion.GetCount() ); // draw small ellipces if( m_eEditMode == EDIT_MODE_LINE ) { for( j = 0; j < m_EditRegion.GetCount(); j++ ) { ::Ellipse( hDC, pPoint[j].x - 1, pPoint[j].y - 1, pPoint[j].x + 1, pPoint[j].y + 1 ); } } // delete pen ::SelectObject( hDC, hPenOld ); ::DeleteObject( hPen ); // delete point array free( pPoint ); } } LRESULT CLocatorWnd::OnDestroy( WPARAM wParam, LPARAM lParam ) { // delete memory device context if( m_hMemDC ) { ::DeleteDC( m_hMemDC ); m_hMemDC = NULL; } // delete memory device context if( m_hMemBitmap ) { ::DeleteObject( m_hMemBitmap ); m_hMemBitmap = NULL; } // delete background device context if( m_hBackgroundDC ) { ::DeleteDC( m_hBackgroundDC ); m_hBackgroundDC = NULL; } // delete background device context if( m_hBackgroundBitmap ) { ::DeleteObject( m_hBackgroundBitmap ); m_hBackgroundBitmap = NULL; } return CGenericWnd::OnDestroy( wParam, lParam ); } LRESULT CLocatorWnd::OnSize( WPARAM wParam, LPARAM lParam ) { SIZE size = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) }; BOOL bCreateNew = !m_hMemBitmap; // check bitmaps sizes if( m_hMemBitmap ) { DIBSECTION DibSection; if( GetObject( m_hMemBitmap, sizeof(DibSection), &DibSection ) ) { if( DibSection.dsBm.bmWidth != size.cx || DibSection.dsBm.bmHeight != size.cy ) bCreateNew = TRUE; } } // create new bitmap if( bCreateNew ) { // delete old bitmap if( m_hMemBitmap ) { ::DeleteObject( m_hMemBitmap ); m_hMemBitmap = NULL; } // create new one HDC hDC = GetDC(); if( hDC ) { m_hMemBitmap = ::CreateCompatibleBitmap( hDC, size.cx, size.cy ); ReleaseDC( hDC ); } } return CGenericWnd::OnLButtonDown( wParam, lParam ); } void CLocatorWnd::SetEditMode( BOOL bEditMode, COLORREF nEditColor, EDIT_MODE eEditMode ) { m_bEditMode = bEditMode; m_eEditMode = eEditMode; m_EditRegion.SetColor( nEditColor ); // set cursor shape SetClassLong( GetSafeHwnd(), GCL_HCURSOR, (LONG)::LoadCursor( NULL, bEditMode ? IDC_CROSS : IDC_ARROW ) ); } BOOL CLocatorWnd::GetEditMode( COLORREF *pEditColor ) { if( *pEditColor ) *pEditColor = m_EditRegion.GetColor(); return m_bEditMode; } POINT CLocatorWnd::LocatorToClient( CLgDPoint& dp ) { // get sizes RECT rect; GetClientRect( &rect ); SIZE s = { rect.right, rect.bottom }; POINT p = { s.cx * ( 1.0 + dp.x ) / 2.0, s.cy * ( 1.0 + dp.y ) / 2.0 }; return p; } CLgDPoint CLocatorWnd::ClientToLocator( POINT& p ) { // get sizes RECT rect; GetClientRect( &rect ); SIZE s = { rect.right, rect.bottom }; return CLgDPoint( p.x * 2.0 / s.cx - 1.0, p.y * 2.0 / s.cy - 1.0 ); } LRESULT CLocatorWnd::OnLButtonDown( WPARAM wParam, LPARAM lParam ) { POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) }; // if window in edit mode && mouse was previosly pressed if( m_bEditMode ) { switch( m_eEditMode ) { case EDIT_MODE_POINT: // mark that mouse cuptured m_bEditStart = TRUE; // clear array m_EditRegion.RemoveAll(); // capture mouse control ::SetCapture( GetSafeHwnd() ); break; case EDIT_MODE_LINE: if( m_bEditStart ) { // translate into client coordinates POINT fpoint = LocatorToClient( m_EditRegion[0] ); if( abs( fpoint.x - point.x ) <= 2 && abs( fpoint.y - point.y ) <= 2 ) { // mark that draw finished m_bEditStart = FALSE; // plase region into region list m_RegionArray.Add( m_EditRegion ); // empty region m_EditRegion.RemoveAll(); // release mouse control ReleaseCapture(); } else { CLgDPoint dp = ClientToLocator( point ); // place point into array m_EditRegion.Add( dp ); } } else { // mark that draw started m_bEditStart = TRUE; // add point into array CLgDPoint dp = ClientToLocator( point ); // place point into array m_EditRegion.Add( dp ); // capture mouse control ::SetCapture( GetSafeHwnd() ); } // redraw editor InvalidateRect( NULL, FALSE ); break; } } return CGenericWnd::OnLButtonDown( wParam, lParam ); } LRESULT CLocatorWnd::OnMouseMove( WPARAM wParam, LPARAM lParam ) { POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) }; // if window in edit mode && mouse was previosly pressed if( m_bEditMode ) { switch( m_eEditMode ) { case EDIT_MODE_POINT: if( m_bEditStart ) { CLgDPoint dp = ClientToLocator( point ); // place point into array m_EditRegion.Add( dp ); // redraw editor InvalidateRect( NULL, FALSE ); } break; case EDIT_MODE_LINE: // to do nothing break; } } return CGenericWnd::OnMouseMove( wParam, lParam ); } LRESULT CLocatorWnd::OnLButtonUp( WPARAM wParam, LPARAM lParam ) { POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) }; // if window in edit mode && mouse was previosly pressed if( m_bEditMode ) { switch( m_eEditMode ) { case EDIT_MODE_POINT: if( m_bEditStart ) { m_bEditStart = FALSE; // plase region into region list m_RegionArray.Add( m_EditRegion ); // empty region m_EditRegion.RemoveAll(); // relase mouse control ReleaseCapture(); // redraw editor InvalidateRect( NULL, FALSE ); } break; case EDIT_MODE_LINE: // to do nothing break; } } return CGenericWnd::OnLButtonUp( wParam, lParam ); } LRESULT CLocatorWnd::OnRButtonUp( WPARAM wParam, LPARAM lParam ) { POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) }; // if window in edit mode && mouse was previosly pressed if( m_bEditMode ) { CLgDPoint dp = ClientToLocator( point ); // find region INT nIndex = m_RegionArray.PtInRegion( dp ); // remove region from array m_RegionArray.Remove( nIndex ); // redraw editor InvalidateRect( NULL, FALSE ); } return CGenericWnd::OnRButtonUp( wParam, lParam ); }